Ruby
Created by Yukihiro "Matz" Matsumoto in 1995, Ruby is a dynamical object-oriented programming language. It's built/inspired by Perl, Smalltalk, Eiffel, Ada, and Lisp.
Matz, speaking on the Ruby-Talk mailing list, May 12th, 2000.
Ruby is simple in appearance, but is very complex inside, just like our human body
Factoid
In Ruby everything is objects, so no primitives. Parentheses are also optional. Opening up for some interesting coding, but that doesn't mean that you have to avoid them.
Is this really that readable?
puts array.delete hash.fetch :foo
Snippet 1
Use the $
to create global variables, but don't clutter the namespace please.
Snippet 2
To create a new hash, all you need to type is {}
.
Snippet 3
Ruby uses nil
as a false value. So no need to do var.nil?
everywhere.
Snippet 4
puts
is used to print a string version of an object with a new line at the end. Short for "put string".
Snippet 5
To make Ruby even more powerful it handles closures. And with the use of f.call
you can call/invoke a block or function. Pretty sweet. For those unfamiliar with closures. A closure can pass it around like an object (to be called later). It also remembers the values of all the variables that were in scope when the function was created. It can access those variables when it is called even though they may no longer be in scope.
Snippet 6
The snippet class C
returns nil, and that is because everything in Ruby has a return value. You can even return multiple values.
Snippet 7
With the use of include
Ruby support single inheritance. Making it easy to mixin modules.
Snippet 8
x||=true
is one of several logical operators that Ruby supports. This one checks if x has been defined, and if not sets it to true.
Snippet 9
Fo.new(x)
calls the class Fo
initialize function, with the value of x
. There is no constructor in Ruby so you are free to return what value you like from the .new()
call.
Snippet 10
As mentioned earlier Ruby supports multiple return values, and it is easily done with return x,y
. The values are returned as they are read, and if you have more return values then what you have for assigning the rest will be dropped. Using the *
on the last variable will make an array and assign the rest of the return values to that array.
Snippet 11
RubyGems is the packet manager for Ruby started in November, 2003. Original a stand alone program, but as of Ruby 1.9 it is a part of the standard library.
Use the code snippet gem install
and the name of the package (for example 'sinatra') to install it.
Snippet 12
[1,2]<<[3,4]
The command '<<' pushes or append a object to the array. In our case the array [3,4] will be pushed 'into' the array [1,2], making it 'look' like this [1,2,[3,4]]. So it does not merge two arrays.
Snippet 13
x.map{|y|y+1}
The map function iterates over the array 'x' and invokes the block given, and returning a new array with the modified values. In our case it will iterate over the values in the array and increment them with one.
Snippet 14
x.map!{|y|y+1}
This snippet is similar to the previous one, but calls on the function map!. The '!'/exclamation mark tells the user that this function will modify the object calling it, often called destructive or dangerous functions.
Snippet 15
The snippet ARGV[0] ||false
checks if ARGV[0]
evaluates to true
, if not it will return the or
valuge, false. ARGV
is an array containing the arguments sent in at start time. ruby app.rb test
will give you 'test' in ARGV[0]
.
PS: Also check out this post about Ruby