Friday, October 14, 2011

Primary types in Ruby

Character
A character in Ruby truly is an integer. It is not a type of its own, as in Pascal, and is not the same as a string of length 1. This is slated to change in ruby1.9 so that a character constant will be much the same as a string. As of the time of this writing, this has not happened yet. Consider the following code fragment:
>> a = "0123"
=> "0123"
>> a[0]
=> 48

But in ruby 1.9, output will be 0 rather than 48. But in case of older versions, you can do following to get char rather than integer or its ascii values:
Conver to character
a[0].chr

Or Try a[0,1] instead of a[0].

Boolean
There is no Boolean type such as many languages have. TRueClass and FalseClass are distinct classes, and their only instantiations are TRue and false.
Some may be used to thinking that a false value may be represented as a zero, a null string, a null character, or various other things. But in Ruby, all of these are true; in fact, everything is true except false and nil.

No comments:

Post a Comment