views:

643

answers:

6

Is ruby strongly or weakly typed ?

Presumably the same is true for Javascript.

A: 

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.

Andrew Hare
Seems to be contradicted by:http://www.rubyfleebie.com/ruby-is-dynamically-and-strongly-typed/Curiouser and Curiouser?
weepy
A: 

I would consider these languages duck typed.

JPrescottSanders
+6  A: 

IMHO it is strongly but dynamicaly typed

Dev er dev
+5  A: 

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.

cg
+15  A: 

Ruby is "strong typed".

strong typed means an object's type (not in oop sense, in general sense) is checked before an operation requiring a certain type is executed on it.

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

on the other hand static typed means static strong typed, the type checking is performed at compile time as opposed to runtime.

dynamic typed means dynamic strong typed, meaning type checking is done at runtime.

Pop Catalin
Thank you for the clarification. Helped. :). 1 up for you.
Chirantan
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.
Justice
@Justice, I guess you have a point ...
Pop Catalin
A: 

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.

Mase