ROR is OOP:

1. Method applied on object

1.1. Strings:
ruby> "123xyz".length 6 ruby> @a = "xyz" "xyz" ruby> @a.length 3
1.2 Array:
ruby> @a = ["mail","email"] ["mail", "email"] ruby> @a.length 2 ruby> @a[0] "mail" ruby> @a[0].length 4 ruby> @a[1] "email" ruby> @a[1].length 5 ruby>

2. classes and methods


2.1. Method without parameters:
Define a class Sulfur and the a new object @mercaptan:

ruby> class Sulfur ruby| def property ruby| puts "contains Sulfur atom." ruby| end ruby| end nil ruby> @mercaptan = Sulfur.new # ruby> @mercaptan.property contains Sulfur atom. nil ruby> Sulfur.new.property contains Sulfur atom. nil ruby>
2.2. Method with parameters, private instance
ruby> class Circle ruby| def surface(a) ruby| puts"The surface of a circle of radius #{a}= #{area(a)}" ruby| end ruby| def area(b) ruby| Math::PI*b*b ruby| end ruby| private:area ruby| end Circle ruby> @little_circle = Circle.new # ruby> @little_circle.surface(5) The surface of a circle of radius 5 is: 78.5398163397448 nil ruby> @big_circle = Circle.new # ruby> @big_circle.surface(10.50) The surface of a circle of radius 10.5 is: 346.360590058275 nil ruby>

2. Inheritence

ruby> class Hydrocarbon
ruby| def property
ruby| puts "Contains Hydrogen and Carbon atoms."
ruby| end
ruby| end
nil
ruby> class Aromatic @toluene = Aromatic.new
#
ruby> @toluene.property
Contains Hydrogen and Carbon atoms.
nil
ruby> @toluene.formula
Contains the cycle of Benzene.
nil
ruby>

The smaller class  (subclass)"Aromatic"inherits characteristics 
from the larger class "Hydrocarbon" (superclass)to which it belongs.
If all hydrocarbons contain hydrogen and 
carbon, then all aromatic do.

3. Using procedure method

The argument of a procedure is a function:
ruby> @diamond = proc{
ruby| puts "It is rare then expesive."
ruby| }
#
ruby> @diamond.call
It is rare then expesive.
nil
ruby> def execute(x)
ruby| puts "We call a defined procedure: diamond"
ruby| x.call
ruby| puts "That is introduced inside a function."
ruby| end
nil
ruby> execute @diamond
We call a defined procedure: diamond
It is rare then expesive.
That is introduced inside a function.
nil
ruby>

4. Using trap method

Let's observe the following outputs:
C:\ruby\samples\RubySrc-1.8.6\sample>ruby eval.rb
ruby> exit

C:\ruby\samples\RubySrc-1.8.6\sample>ruby eval.rb
ruby> @diamond = proc {puts "It is rare and expensive .."}
#<Proc:0x029fa4dc@(eval):1>
ruby> @diamond.call
It is rare and expensive ..
nil

@diamond is the procedure to remember, 
among other things.

ruby> @watch = proc {puts "We taped ^C (Ctrl_C). That will keep work, 
but not object's values .. "}
#<Proc:0x0276a4d8@(eval):1>
ruby> trap "SIGINT"
ERR: (eval):1:in `trap': tried to create Proc object without a block
ruby> trap "SIGINT",
ruby| @watch
"DEFAULT"

ruby> trap "SIGINT", @watch
#<Proc:0x0276a4d8@(eval):1>
ruby>
We taped ^C (Ctrl_C). That will keep work, but not object's values ..

C:\ruby\samples\RubySrc-1.8.6\sample>ruby eval.rb
ruby> @watch
nil
ruby>

Using up and down keys, we will that we still have stored 
the related work dine before ^C; for example, we have:

ruby> @diamond = proc {puts "It is rare and expensive .."}

Using the trap method and pressing (Ctrl_C), will keep our work, 
but not the attributes of objects. The trap method allows us to quit 
the interpreter ruby without loosing our work. The ^C becomes not 
sufficient to quit definitly the interpreter. To do this, 
use exit command."