Ruby programming language



         


Ruby is an object-oriented programming language. It combines syntax inspired by Ada and Perl with Smalltalk-like object oriented features, and also shares some features with Python, Lisp and CLU. It was originally designed as an interpreted language, though in its JRuby implementation it may be compiled.

The language was created by Yukihiro "Matz" Matsumoto, who started working on Ruby on February 24, 1993 and released it to the public in 1995. He chose the name to reflect the language's Perl heritage. According to the author, he designed Ruby to follow the principle of least surprise (PoLS), meaning that the language should be free from the traps and inconsistencies that plague other languages. As of February 2004, the stable version is 1.8.1.

Ruby is purely object-oriented: every bit of data is an object, including types that are designated "primitive" in other languages such as integers. Every function is a method. Named values (variables) designate references to objects, not the objects themselves. Ruby supports inheritance with dynamic dispatch, mixins, and singleton methods (belonging to an instance rather than a class). Though Ruby does not support multiple inheritance, classes can import modules as mixins. Procedural syntax is possible, but anything done in Ruby procedurally (ie. outside of the scope of a particular object) is actually done to the Object class. Since this class is parent to every other class, the changes become visible to all classes and objects.

Ruby has also been described as a multi-paradigm programming language: It allows you to program procedurally (defining a function/variable outside a class makes it part of the root 'self' Object), object-orientated (everything is an object) or functionally (anonymous functions, closures, continuations, all expressions return a value, when no return statement is present, functions return the last value evaluated). It has rich support for introspection, reflection and metaprogramming.

Ruby has two main implementations, the official Ruby interpreter, which is the most widely used and JRuby, a Java-based implementation. The standard interpreter has been ported to many platforms, including Unix, Microsoft Windows, DOS, Mac OS X, OS/2, Amiga, and many more. The Ruby distribution also includes IRB, an interactive command-line interpreter which can be used to quickly test out code.

From the Ruby FAQ: "If you like Perl, you will like Ruby and be right at home with its syntax. If you like Smalltalk, you will like Ruby and be right at home with its semantics. If you like Python, you may or may not be put off by the huge difference in design philosophy between Python and Ruby/Perl."

Ruby is distributed disjointly under the free and open source licences GPL and Ruby License .

[Top]

Features

Ruby currently lacks support for Unicode, though it has partial support of UTF-8.

[Top]

Examples

Here are some basic examples of Ruby code:

# everything, including literals, is an object, so this works: -199.abs # 199 "ruby is cool".length # 12 "Rick".index("c") # 2 "Nice Day Isn't It?".split(//).uniq.sort.join # " '?DINaceinsty"
[Top]

Arrays and hashes

constructing and using an array:

a = [ 1, 'hi', 3.14, 1, 2, [4, 5] ] a[2] # 3.14 a.reverse # [ [4, 5], 2, 1, 3.14, "hi", 1] a.flatten.uniq # [1, "hi", 3.14, 2, 4, 5]

constructing and using a hash:

h = {'water' => 'wet', 'fire' => 'hot'} puts h['fire'] h.each_pair do |key, value| puts "#{key} is #{value}" end # water is wet # fire is hot h.delete_if { |k,v| k == 'water' } # deletes 'water' => 'wet'
[Top]

Blocks and iterators

Two different ways of making blocks:

{ puts "Hello, World!" } do puts "Hello, World" end

passing a block as a parameter (closure concept):

def func name yield name end # call the function with a name and a block func("John") do |name| puts "Hello, #{name}" end # prints "Hello, John"

iterating through an array using closures:

a = [1, 'hi', 3.14] a.each { |item| puts item } # prints each element 3.upto(6) { |num| puts num } # prints the numbers 3 to 6

Iterators are part of almost every built-in class:

IO.readlines('file.txt') do |line| # .. process each line here end

Using map to calculate squares from 1 to 10:

(1..10).to_a.map { |x| x*x }

=> [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

[Top]

Classes

The following code defines a class named Person in which the initialize method is a constructor (it is called when creating a new object), and two methods: one overloading the <=> comparison operator (so Array#sort can sort by age) and the to_s (so Kernel#puts knows how to format the output) method. The attr_reader is an example of metaprogramming in ruby, it defines read/write methods for instance variables (in this case only read methods). The code then creates an array called group with three elements and prints it sorted by age in reverse order.

class Person def initialize(name, age) @name, @age = name, age end def <=>(person) @age <=> person.age end def to_s "#{@name} (#{@age})" end attr_reader :name, :age end group = [ Person.new("John", 20), Person.new("Markus", 63), Person.new("Ash", 16) ] puts group.sort.reverse

This code generates the following output:

Markus (63) John (20) Ash (16)

More Ruby code is available in the form of sample algorithm implementations in the following articles:

[Top]




  View Live Article   This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License