Getting started with Ruby – Part 2 – Introduction & Philosophy

Hi again, this is part 2 of my series of articles to help beginners getting started with the Ruby programming language. At this point of time I would like to quote the definition of Ruby from the Ruby-Lang official website.


I know I had already quoted Ruby’s definition from wikipedia in my previous article ‘Getting started with Ruby – Part 1‘, but that was more of a technical definition. Here I wanted to draw your attention to Yukihiro Matsumoto‘s, popularly known as Matz’s philosophy behind creating Ruby, which is making programming as simple & natural as humanely possible. We will be witnessing that very soon.

NOTE : Before diving into programming, I would like to bring to your notice the fact that most of the information that I will be sharing here in my articles, would be my interpretation of publicly available information, and based on my experiences, I will try to be as expressive as possible, however I can make mistakes and would appreciate your help in pointing out the same if I do. All my examples have been ran and tested on Ruby 1.9.2-p0.

Ruby is a Pure Object Oriented Programming Language, by pure, it is implied that everything in Ruby is an object. Unlike programming languages like C++/Java/C# which are OOPLs but not pure since they have primitive data types which are not objects, in Ruby, everything is an object. However, that being said, we obviously do have types in Ruby, but these are not primitive data types, they are classes which originate from the Object class. In Ruby every class originates from the Object class. The following are the various data types in Ruby :

  • String – as the name suggests, these are a sequence of characters. Any sequence of characters enclosed between single or double quotes creates a string. Let us look at some examples :

    First example is pretty straight forward, in the second example I demonstrate what are known as escape sequences. Since the single quote (‘) is used to indicate the start & end of a string, if you wanted to use one within a string, you need to use the escape sequence using the backslash (\) to indicate the Ruby interpreter to replace it with that character and ignore it for the purpose of determining the start & end of the string. The third example is also demonstrates the use of certain special escape sequences, which when used in strings defined using double quotes (“) are parsed and replaced with appropriate value. In the example \t is replaced by the Ruby interpreter with a tab character. The fourth example demonstrates how you can evaluate expressions and embed their values within strings, again this is possible only for strings defined using double quotes (“).Single quoted strings are ordinary strings and have a very limited set of escape sequences, whereas Double quoted strings offer a lot more escape sequences like \t, \n, \r, \s, etc and also they allow expression interpolation, that is embedding results of expressions or variables within strings by enclosing them within #{…}Now I will take you through some extremely interesting examples :

    The first example might surprise you, however if you think about it logically, it does exactly what you would expect it to do. Since string literals, like everything else in Ruby are objects, multiplying it with a number makes that many copies of it.

    The following 3 examples demonstrate a short hand notation in Ruby to create strings, as you might be able to tell, %q is the equivalent to using single quotes and %Q is equivalent to using double quotes. The short hand notation allows you to use any special character as a delimiter which it uses for identifying the start and end of the expression that it will evaluate to create the string. Hence %Q.#{4*4}. == %Q{#{4*4}} == %Q@#{4*4}@ == %Q$#{4*4}$ == %Q(#{4*4}), etc etc. This is true for all short hand notations.

  • Numbers – Ruby has two class Fixnum & Bignum to represent numbers. As i’ve mentioned before, everything in Ruby is an object, and so are numbers! Fixnum are numbers within the range (-2^30) and (2^30 – 1), if the value of the number goes beyond this range, ruby transparently assigns it to an instance of Bignum so you as a programmer do not need to worry about that.
    examples :

    As you can see, Ruby allows underscores (_) within numbers and so you can make use of that to make your numbers more readable. Numbers with decimal place are known as floats, last two are examples of floats.

    The above snippet demonstrates how to perform arithmetic operations within Ruby. Here I would like to point out one important point. Since numbers in Ruby are objects, all arithmetic operations that we perform are actually methods that exist with the objects, although it is not very evident most of the time. The second example demonstrates how + is actually a method that we invoke using the object with value 1 and pass to it the object with value 2, which returns an object of value 3, the sum of 1 & 2.
    All standard arithmetic operations exist in Ruby along with a special (**) operation, which is exponent operation. In the above example 2**3 can be read as 2^3 and the result of that is 8.

I will cut short the article here since the article has grown quite long already. I will continue with other data types in the next article, after which I will take you through variables, symbols, methods, classes and more. I hope you like the article and it is easy to understand. If you have any questions or suggestions, please do let me know in the comments below.

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Leave a Reply