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 Programming Language:
A dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.
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 from a technical standpoint. Here I wanted to draw your attention to Yukihiro Matsumoto‘s (1)Popularly known as Matz 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 the code, 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, and based on my experiences, I will try to be as accurate as possible, however there may be unintentional mistakes and I would appreciate your help in pointing out the same.
All examples have been run and tested on Ruby 1.9.2-p0
Ruby is a Pure Object Oriented Programming Language, by that, 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:
puts 'Hello' # => Hello
puts 'What\'s Up?' # => What's Up?
puts "A\tTab." # => A Tab.
puts "#{4 * 4}" # => 16
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 \'
.
The third example demonstrates the use of certain special escape sequences,
which when used in strings defined using double quotes "
are parsed and
replaced with special characters. In the example \t
is replaced 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 evaluation of
expressions or variables within strings by enclosing them within #{…}
.
Now I will take you through some extremely interesting examples:
puts 'Hello ' * 3 # => Hello Hello Hello
puts %q{Whats Up?} # => Whats Up?
puts %Q{A\tTab.} # => A Tab.
puts %Q;#{4*4}; # => 16
puts %Q(#{5*5}) # => 25
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 examples demonstrate a short hand notation in Ruby to create
strings. %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, are all
equivalent. 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.
Lets look at some examples:
puts 23 # => 23
puts 12_000_000_000 # => 12000000000
puts 3.14 # => 3.14
puts 1.0e5 # => 100000
As you can see, Ruby allows using underscores _
within numbers to make them
more readable. Numbers with decimals are floats.
puts 1 + 2 # => 3
puts 1.+(2) # => 3
puts 2 ** 3 # => 8
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, although it is not very obvious.
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, i.e. the sum of 1 & 2. All standard arithmetic
operations exist in Ruby along with a special **
operation, which is the
exponent operation. In the above example 2 ** 3
can be read as $2^3$
which
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.