Ruby

Appeared in:
1995
Influenced by:
Influenced:
Paradigm:
Typing discipline:
File extensions:
.rb
Versions and implementations (Collapse all | Expand all):
Programming language

Ruby is a dynamic, reflective, general purpose object-oriented programming language. Every data type is an object (classes are objects of class Class). Every function is a method.

Ruby originated in Japan during the mid-1990s and was initially designed and developed by Yukihiro Matsumoto.

There is currently no specification of the Ruby language, so the standard Ruby implementation written in C is considered to be the de facto reference.

In recent years Ruby gain a lot of attention and popularity due to success of “Ruby on Rails” web-framework.

Elements of syntax:

Inline comments #
Case-sensitivity yes
Variable identifier regexp [_a-z][_a-zA-Z0-9]*
Function identifier regexp [_a-zA-Z][_a-zA-Z0-9]*[!?]?
Variable assignment =
Variable declaration with assignment variable = value
Grouping expressions ( ... )
Block begin ... end
Physical (shallow) equality a.equal?(b)
Physical (shallow) inequality !a.equal?(b)
Deep equality ==
Deep inequality !=
Comparison < > <= >= <=>
Function definition def f(p1, p2, ...) ... end
Function call f(a, b, ...) {for a method} or f[a, b, ... ] or f.call(a, b, ... ) {for a function object}
Function call with no parameters f {for a method} or f[] or f.call {for a function object}
Sequence ; or end of line
If - then if condition then ... end
If - then - else if condition then ... else ... end
Loop forever loop { ... }
While condition do while condition do ... end
Do until condition begin ... end until condition
For each value in a numeric range, 1 increment 1.upto(10) {|i| ... }
For each value in a numeric range, 1 decrement 10.downto(1) {|i| ... }

Ruby logo (from official website)
Ruby logo (from official website)

Examples:

Factorial:

Example for versions Ruby 1.8.5

This example uses recursive factorial definition.

#! /usr/bin/env ruby
def factorial(n)
    if n == 0
        1
    else
        n * factorial(n - 1)
    end
end

0.upto(16) do |n|
    print(n, "! = ", factorial(n), "\n")
end

Factorial:

Example for versions Ruby 1.8.5

This example uses an iterative factorial definition.

def fact(n)
  (1..n).inject(1) {|a,b| a*b}
end

(0..16).each {|x| puts "#{x}! = #{fact(x)}"}

Fibonacci numbers:

Example for versions Ruby 1.8.5

This example uses recursive definition of Fibonacci numbers.

#! /usr/bin/env ruby
def fibonacci(n)
    if n < 3
        1
    else
        fibonacci(n - 1) + fibonacci(n - 2)
    end
end

(1..16).each {|n| puts fibonacci(n)}
puts "..."

Hello, World!:

Example for versions Ruby 1.8.5
puts "Hello, World!"

Factorial:

Example for versions Ruby 1.9

This example uses an iterative factorial definition

def fact(n)
  (1..n).reduce(:*)
end

(0..16).each {|x| puts "#{x}! = #{fact(x)}"}

Quadratic equation:

Example for versions Ruby 1.9
puts 'A = '
A = gets.chomp.to_f
if (A == 0)
   puts 'Not a quadratic equation.'
   return
end
puts 'B = '
B = gets.chomp.to_f
puts 'C = '
C = gets.chomp.to_f
 
D = B*B - 4*A*C
 
if (D == 0)
   puts 'x = '+(-B/2/A).to_s
else
   if (D > 0)
      puts 'x1 = '+((-B-Math.sqrt(D))/2/A).to_s
      puts 'x2 = '+((-B+Math.sqrt(D))/2/A).to_s
   else
      puts 'x1 = ('+(-B/2/A).to_s+','+(Math.sqrt(-D)/2/A).to_s+')'
      puts 'x2 = ('+(-B/2/A).to_s+','+(-Math.sqrt(-D)/2/A).to_s+')'
   end
end

CamelCase:

Example for versions Ruby 1.9
  1. Read the string from standard input (gets.chomp)
  2. Split the string into parts, separated with matches of regular expression, which describes a sequence of one or more non-letter characters
  3. Apply to each part function capitalise
  4. Concatenate the resulting strings (join) and print the result (puts)
puts gets.chomp.split( /[^a-zA-Z]+/ ).map {|w| w.capitalize}.join

CamelCase:

Example for versions Ruby 1.9

This works in a same way as this example, but scan extracts the matches of regular expression instead of discarding them and extracting parts of string between them, as split does.

puts gets.chomp.scan( /[a-zA-Z]+/ ).map {|w| w.capitalize}.join