last update: 2009-05-01 04:55:14 GMT This book is a work in progress; feel free to contribute.

The Merb Open Source Book
Français | 한국어 | 日本語 | Português | Deutsch | 中文 | Español | Русский | Bosanski | Nederlands | العربية | Български | Italiano

Ruby Language

Ruby

Reference website: http://ruby-lang.org

Coding in Ruby makes me happy because it’s one of the shortest paths between my brain and a computer. I can think of something and I can express it very succinctly and typically fairly elegantly in Ruby without all the kind of extraneous fluff that you need in most other languages. - Dave Thomas, author of Programming Ruby

It would be a crime to start talking about the Merb framework without first discussing the very reason why Merb is so flexible, powerful, and fast: Ruby.

Origin

Yukihiro Matsumoto Ruby is an Open Source, dynamic, reflective, general-purpose, object-oriented programming language, written in the mid-1990s by Japanese software architect Yukihiro “Matz” Matsumoto-san ( まつもとゆきひろ).

Ruby focuses on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.

Matz borrowed ideas and idioms from some of his favorite programming languages (Perl, Smalltalk, Eiffel, Ada, and Lisp) to form a new language that balances functional programming with imperative programming.

The result is an attractive language which feels very natural. In the Ruby community, we often refer to the POLS (Principle of Least Surprise). The concept behind this principle is very simple: if you know a minimum of Ruby, you should not be surprised by how the language will react.

Adoption

According to the TIOBE index, Ruby ranks in the top 10 programming languages used worldwide. Much of the growth is attributed to the popularity of software written in Ruby, particularly the Ruby on Rails web framework.

Key Elements of the language

I wanted a scripting language that was more powerful than Perl, and more object-oriented than Python. - Matz

To learn more about the Ruby language, check the Official Ruby language website.

Code Examples

Print the string “Hello world” 5 times:

5.times do
  print "Hello world!"
end
# => "Hello world!Hello world!Hello world!Hello world!Hello world!"

Conditional statement:

access_allowed = true if DateTime.now > DateTime.parse("2008-12-01")

equivalent to:

if DateTime.now > DateTime.parse("2008-12-01")
  access_allowed = true
end

Ternary operator:

age_classification = age > 12 ? "adult" : "child"

equivalent to:

if age > 12
  age_classification = "adult"
else
  age_classification = "child"
end

Array:

drinks = ["Coke", "Pepsi", "Orangina", "DrPepper"]
#     => ["Coke", "Pepsi", "Orangina", "DrPepper"]
# Access the Array instance
drinks[0]     # => "Coke"
drinks.first  # => "Coke"
drinks.last   # => "DrPepper"
drinks[3]     # => "DrPepper"
drinks[-1]    # => "DrPepper"
drinks[drinks.length - 1] # => "DrPepper"

Check if an item exists in an Array instance:

haystack = ["Mac", "NT", "Irix", "Linux"]
needle   = "Windows"
haystack.include?(needle) # => false

Push an item into an Array instance:

haystack = ["Mac", "NT", "Irix", "Linux"]
needle   = "Windows"
haystack.push(needle)
# Or do it like this:
haystack << needle

Define a method:

def greet_visitor(visitor_name)
  "Hi #{visitor_name}!"
end

Merb and Ruby

Merb tries to stay as close as possible to the Ruby language itself. That’s why it’s important to understand what people call the “Ruby Way”.

During RubyConf 2008, Matz made a comment about Merb:

Merb has a bright future for the people who are not satisfied by the fixed ways in Rails. I think that Merb will give users more freedom in a Ruby-ish way of programming - Matz, author of the Ruby programming language