March 06, 2013
Ruby views value types differently then static-typed languages. Traditionally values like Integers, Chars, and Booleans are treated differently then the reference-types. For example in Objective-C in order to pass an NSInteger
by reference or store it in a NSArray
you have to box the value in a reference-type like NSNumber
. Or in C# since value-type variables can’t store null
if your logic requires a variable to store null
or an Integer
you have to use the reference-type Nullable<Integer>
instead.
This isn’t needed in Ruby since the language doesn’t have the notion of value-types, instead everything is a reference to an object on the heap. This allows classes like Integer
to have methods for developer ease such as .even?
, .times
, and .upto()
. Also having the value-types as objects provides the (albeit minimal) benefit of not requiring that the value be boxed and unboxed as it crosses between the realm of value and reference types.
So while the benefits might be minimal the difference is part of a greater mindset. Ruby is a language written for humans first and machines second. This might seem counter intuitive at first since a human will never execute the code but it makes writing the code more enjoyable.
Written by Jason Worley who lives and works in Indianapolis building useful things.