Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is ruby strongly or weakly typed?

Is ruby strongly or weakly typed ?

Presumably the same is true for Javascript.

like image 962
weepy Avatar asked Feb 06 '09 13:02

weepy


People also ask

Is Ruby dynamically typed or statically typed?

Ruby is a dynamic language, which means that types are checked when the code is run. If you try to call a method on an object that does not exist, the compiler will not complain, you'll only find out about that error when the code is executed and you get a NoMethodError .

Is Ruby a statically typed language?

Ruby is a dynamically typed language, which means the interpreter tries to infer the data type of variables and object properties at runtime.

What is strongly typed vs weakly typed?

Strong versus weak is about HOW SERIOUS DO YOU GET while checking the types. You can say that weak typing is relaxed typing, and strong typing is strict typing. Unlike dynamic vs static, the strength of the typing system is a spectrum. JavaScript has very weak typing.

Which languages are strongly typed?

Examples of strongly typed languages in existence include Java, Ruby, Smalltalk and Python. In the case of Java, typing errors are detected during compilation Other programming languages, like Ruby, detect typing errors during the runtime.


1 Answers

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"

like image 181
Pop Catalin Avatar answered Sep 19 '22 18:09

Pop Catalin