RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Ruby—A Diamond of a Programming Language, Part 2

Get ready to dive deeper into the power and elegance of the language lauded as a possible contender to replace current programming languages.

uby is an object-oriented, metaprogramming language that has garnered a lot of attention in software development circles as of late. Metaprogramming is a means of writing software programs that write or manipulate other programs, thereby making coding faster and more reliable. Part 1 examined Ruby setup, general syntax, and object-oriented aspects. This article, part 2, looks at some other powerful features built into the Ruby programming language. Again, Ruby is based on and has adopted features from many languages including Lisp, Smalltalk, Python and Perl. So in many ways, Ruby is a "best of breed" language that has been assembled from the best and most powerful programming features found in its predecessors.

Numbers, Strings, and other "Standard" Types in Ruby
Everything in Ruby is an object. Maybe more precisely, everything in Ruby is treated as a full fledged object. So, the number 4, the fixed point number 3.14 and the string "Hi" are all objects in Ruby. Obviously, they are somewhat "special" objects since you don't have to use the new method to create one. Instead, you use the literal 4, for example, to create an instance of the object representing the number 4.

However, when most people learn a new programming language it is helpful to understand "standard" types. So in this section, numbers, strings, Booleans, and a few other base Ruby types are explored.

Essentially, numbers in Ruby are classified as either integers or floats. Integers are further subdivided. There are "regular sized" integers and really big integers. Because everything is an object, floats and integers (whether big or small) are defined by classes (see Figure 1). Numeric is the super class for all numbers. Float and Integer classes are subclasses of Numeric. Fixnum and Bignum are subtypes of Integer that define the "regular size" and bigger integer numbers.

Figure 1. Ruby Numbers: The Numeric class is the super class for all Ruby numbers. Concrete instances of numbers are either of type Bignum or Fixnum integers or Float.

Literals are used to represent instances of these classes. The code below in the interactive Ruby shell (irb) shows literal instances of Float, Fixnum and Bignum. Notice that methods (in this case the class method) can be called on the literal. Again, everything in Ruby is an object so you will find all sorts of methods for numeric instances.

irb(main):001:0> 3.class
=> Fixnum
irb(main):002:0> 3.4.class
=> Float
irb(main):003:0> 10000000000000000000.class
=> Bignum
There is some additional syntax for creating numbers as shown in the code listing below. The letter 'E' can be used to represent numbers in exponential notation. Numbers can be proceeded with 0 to represent a number in octal, 0x to represent a number in hex, and 0b to represent a number in binary form. For clarity, underscores can be used in numbers as separators. Be careful not to use a comma as a separator when writing literals. In some cases, this can actually produce an array, which will be discussed below. Finally, a question mark in front of a character (or control or meta character combination) creates a Fixnum instance corresponding to the ASCII character/escape sequence value for the character(s).
What's the real difference between Fixnum and Bignum instances? Fixnum integers can be stored in the machines word minus a bit (usually 16, 32, or 64 bits) while Bignum instances are integers that exceed that sized storage space. The good news for developers is that you don’t have to worry about the size of the integer (see the code sample below). Ruby automatically takes care of any conversion across the Fixnum/Bignum barrier for you!

irb(main):001:0> i=4
=> 4
irb(main):002:0> i.class
=> Fixnum
irb(main):003:0> i=i+100000000000000
=> 100000000000004
irb(main):004:0> i.class
=> Bignum
irb(main):005:0> i=i-100000000000000
=> 4
irb(main):006:0> i.class
=> Fixnum

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date