1. Variables:

Kinds of variables

Ruby has three kinds of variables: 
variables and constants have no type. 

$ 	global variable
@ 	instance variable (object)
[a-z] or _ 	local variable
[A-Z] 	constant

Pseudo-variables: 
self: variable that refers to the currently executing object 
(equivalent of this in C++ or Java). its value is main.
nil: constant whose value is assigned to uninitialized variables. 

We have:
ruby> number = 23; print number, "\n"; defined? number 23 "local-variable(in-block)" ruby> @number = 23; print @number, "\n"; defined? @number 23 "instance-variable" ruby> $number =23; print $number, "\n"; defined? $number 23 "global-variable" ruby> @@number = 23; print @@number, "\n"; defined? @@number 23 "class variable"

2. Examples:

Example 1:

ruby> self main ruby> nil nil

Example 2:

 
ruby> i=8
8
ruby> i
ERR: undefined local variable or method `i' for #
ruby>
But:
ruby> @i=18
18
ruby> @i=18
18
ruby>

Example 3: Using case

The case statement to test a sequence 
of conditions is similar to switch in C and Java or PHP.

10..15 means the range between 10 and 15, inclusive. 
We will test here whether the value of the object @i 
falls within that range:
ruby> @i=18 18 ruby> case @i ruby| when 1, 10..15 ruby| puts "1..15" ruby| when 16..20 ruby| puts "16..20" ruby| end 16..20 nil ruby>

Example 4: Looping with "if", "while" and "for"

4.1.IF
ruby> @i = 10 10 ruby> puts "Less than a week." if @i<7 nil ruby> puts "Week has seven days." if @i==7 nil ruby> puts "A couple of weeks." if @i>7 A couple of weeks. nil ruby>
4.1.WHILE
ruby> @sum =0 0 ruby> while @sum <4 ruby| puts "@sum = " + @sum.to_s ruby| @sum +=1 ruby| end @sum = 0 @sum = 1 @sum = 2 @sum = 3 nil ruby>
4.3.FOR
ruby> for number in (1..4) ruby| puts number ruby| end 1 2 3 4 1..4 ruby>

Example 5: The :: operator shows the module to consider

The :: operator tells the ruby interpreter which module
ruby> sqrt(36) ERR: undefined method `sqrt' for # ruby> Math.sqrt(36) 6.0 ruby> Math::sqrt(36) 6.0 ruby> include Math Object ruby> sqrt(36) 6.0 ruby> PI 3.14159265358979 ruby>