Is ruby strongly or weakly typed?

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

Is ruby strongly or weakly typed ?

Presumably the same is true for Javascript.

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

    Wikpedia labels it as "dynamic ('duck') typed".

    Regarding Pop's comment about it being "strong-typed" - I'm not sure his explanation actually fits with what goes on under the covers. The MRI doesn't really "check" to see if an operation can be performed on an object; it just sends the object the message, and if that object doesn't accept that message (either by a method declaration or by handling it in #method_missing) it barfs. If the runtime actually checked to make sure operations were possible, #method_missing wouldn't work.

    Also, it should be noted that since everything in Ruby is an object (and I do mean everything), I'm not sure what he said about "not in an oo-sense" is accurate. In Ruby, you're either an object or a message.

    0 讨论(0)
  • 2020-12-14 01:51

    While you can get into arguments about the definition of those term I'd say:

    Ruby dynamically and strongly typed while JavaScript is dynamically and weakly typed.

    0 讨论(0)
  • 2020-12-14 01:55

    I would consider these languages duck typed.

    0 讨论(0)
  • 2020-12-14 01:57

    I just stumbled-on this old thread but thought it proper that I could offer my opinion. (No, I'm not "hijacking" a zombie-thread.)

    My colloquial interpretation of the term "strongly typed™" specifically refers to "compile time." (Which is something that many languages today, including Ruby, "simply do not have.")

    For instance, a simple assignment statement such as a = b; could be judged by the compiler to be acceptable or not, based on its assessment of the "types" of a and b, and based on provisions for type-conversion (if applicable) provided by the programmers. If the statement was judged unacceptable, a compile-time error would be thrown and no "executable" would ever be produced.

    This notion, of course, is not compatible with the fundamental design precepts of languages such as Ruby, PHP, Perl, JavaScript, or a great many other languages that are in extremely-wide (and, extremely-successful) use today. (Mind you, I do not mean this as a "judgment" either for or against them. They are what they are, and they sure do bring home the bacon.)

    Because these languages do not have a "compile time," to my(!) colloquialism they cannot be called, "strongly typed." They are obliged to make decisions at runtime which, by their design, could not have been made sooner.


    (Also please note that I am specifically excluding from consideration the various "lint tools" that have emerged for this-or-that language in an effort to catch more bugs in advance. These are very useful, yes yes, but not the same thing.)

    (I am also purposely excluding various excellent(!) tools that generate source-code in the various target-languages in question ... for the same reasons.)

    And – I say once again – I am making a classification, not a judgment.

    0 讨论(0)
  • 2020-12-14 02:00

    IMHO Ruby is strongly but dynamically typed.

    0 讨论(0)
  • 2020-12-14 02:02

    The over-simplified answer is that both ruby and javascript are weakly typed.

    However this question is not quite as clear-cut as it may seem - see this wikipedia article for a more in-depth discussion on the difference between strongly and weakly typed languages.

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