Is ruby strongly or weakly typed?

后端 未结 7 1323
Happy的楠姐
Happy的楠姐 2020-12-14 01:10

Is ruby strongly or weakly typed ?

Presumably the same is true for Javascript.

相关标签:
7条回答
  • 2020-12-14 02:12

    Ruby is "strong typed".

    Strong typing means an object's type (not in the OOP sense, but in a general sense) is checked before an operation requiring a certain type is executed on it.

    Weak typed means that no checking is done to ensure that the operation can succeed on the object. (For example, when a function accesses a string like and array of floats, if no type checking is done then the operation is allowed)

    Edit: It's been 6 years since this answer was posted and I think it warrants some extra clarifications:

    Over the years the notion that "type safety is a dial not an absolute" started to be used in favor of the binary meaning (yes/no)

    Ruby is "stronger" typed (with an "er") than most typical dynamic languages. The fact that ruby requires explicit statements for conversion IE: Array("foo"), "42".to_i, Float(23), brings the Ruby typing dial closer to the "Strong Typed" end of spectrum than the "weak typed".

    So I would say "Ruby is a stronger typed dynamic language than most common dynamic languages"

    0 讨论(0)
提交回复
热议问题