Is ruby strongly or weakly typed?
Asked Answered
L

7

35

Is ruby strongly or weakly typed ?

Presumably the same is true for Javascript.

Lewanna answered 6/2, 2009 at 13:12 Comment(2)
Why do you think they'd be the same - because Rails developers use both Ruby and Javascript?Sudbury
"strongly-typed" and "weakly-typed" are meaningless phrasesUnmoving
D
50

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"

Dall answered 6/2, 2009 at 14:10 Comment(14)
Static typing does not imply strong typing. For example, C is statically typed because the compiler knows the type of every variable, but it is not strongly typed because memory is just memory and can be treated any which way by the program.Lashondalashonde
You may want to answer whether Javascript is strongly or weakly typed.Sudbury
@Andrew Grimm, Java is weakly typed, no one prevents you to add a string to an integer and get results or an array of int to string and get an array of chars ... or some other crazy combination. It's true that you can't create run time errors like you can in C, but if you want to classify the languages further java script is a safe weak typed language, while C is an unsafe weak typed language. In JavaScript you will only get bad results, while in C you will crash.Dall
Sorry for digging an old discussion, but are there any checks which are performed at run time before involving any operation? When you write the code like "abc" + [1,2,3] it is just a matter of whether the plus method on String could handle an instance of Array.Anti
"Java is weakly typed, no one prevents you to add a string to an integer and get results": This is not weak typing but rather polymorphism. There are different + operations / methods and dispatching is based on the type of the operands. This is different from taking a strings of bits representing an array of floats and reinterpreting it as a some struct (as you can do in C and C++).Duplessismornay
@Giorgio, JavaScript is type safe but still weakly typed, anything is a bool potentially, an array, a string, a number, yet you can't corrupt memory. C is also weakly typed but NOT type safe like JavaScript, because you can corrupt memory due to type mismatches.Dall
@PopCatalin: I'm not sure what you meant by "when a function accesses a string like and array of floats". Did you mean to write "when a function accesses a value like an array of floats"? Or maybe "when a function accesses a string or an array of floats"?Leandraleandre
I have heard many variations on explanations about strong typing vs weak typing, but this is the first time I hear a view point like "Java is weakly typed while Ruby is strongly typed". I dislike the argument that if a language can perform 1 + "one" its weakly typed. Some people argue "if an object has its type information (either at compile time or run time, among the many different types it can be)", it's strongly typed. Which type, isinstance etc in Python and class in Ruby can give. I like it better, but it also makes a lot of languages widely considered to be weakly typed strong!Louis
@Louis what is 1 + "one": "1one" or 2 or some garbled memory (C/C++ when you force this)? In Stronger typed languages this is an error. Strong typing isn't just about run-time type information (which Js has, but about what is allowed or disallowed using typing information as constraints). Strong typing = stronger type constraints, weak typing = Weaker type constraints. Typing is a dial, you can have something in between. The weakest typed being machine code, while there's no limit of how strong typed a language can be.See Scala for example.Dall
@PopCatalin I'm not contesting your argument, certainly that's one definition. I say why I dislike the argument. 1 + "one" results in whatever according to the various coercion rules of the language. For instance there are languages which allow + operator to be overloaded to make oneSpecificThing + aDifferentButSpecificThing possible. It's not a weakness in type system, rather flexibility of the language. In Java, the type of neither the value nor the variable is changed when you do string + int.Louis
Furthermore you contradict somewhat. If strong-ness is a dial then avoid generalized statements like Java is weakly typed and Ruby is strongly typed.Louis
@PopCatalin: memory safety is not type safety — JavaScript is mostly strongly typed for a lot of stuff works on a weakly-typed bases (think automatic coercions); also your answer really confusingly makes it seem like strong-weak vs static-dynamic are somehow on the same scale (re "Weak typed means that no checking is done", which is just false) — they are not. In fact, there are effectively 2 distinct types of weak to start with, and a language can have 1 or both, so there are at least 12 different combinations available for languages to "pick from".Dellora
This answer is based on a wrong definition of weak typing. Weak typing does not mean no type checking is done. It means that the language tries to silently auto-convert the object into another type to allow the operation to be performed.Cabinet
@Cabinet That's type coercion. It does not matter if the result is an error or a result jumble. If the type system is not used to enforce correct behavior then the language is weakly typed (regardless of how it does it).Dall
I
35

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.

Inocenciainoculable answered 6/2, 2009 at 21:46 Comment(0)
E
14

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.

Eric answered 6/2, 2009 at 14:6 Comment(1)
I would say they are both dynamic, both weak, but JS has more aggressive implicit type coercions.Whitener
C
8

IMHO Ruby is strongly but dynamically typed.

Crosslegged answered 6/2, 2009 at 13:44 Comment(0)
F
4

I would consider these languages duck typed.

Faerie answered 6/2, 2009 at 13:39 Comment(0)
I
1

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.

Interest answered 6/2, 2009 at 13:16 Comment(1)
Seems to be contradicted by: rubyfleebie.com/ruby-is-dynamically-and-strongly-typed Curiouser and Curiouser ?Lewanna
D
1

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.

Dottiedottle answered 10/1, 2019 at 15:30 Comment(1)
Just a little correction, PHP has had support for strict types since v7, so somewhere around 2015 I believe - and even before that all good IDEs were able to use meta data in comments to do the static analysis of code.Rumsey

© 2022 - 2024 — McMap. All rights reserved.