Skip to content

Ruby

Yet another scripting language ?. Mainly, i need this now for vagrant which uses ruby style configuration files.

Also the devhints.io sources use ruby.

hello

hello world
#!/usr/bin/ruby
puts "Hello, world!";

classes

Classes are defined like below, and each of the variables has it's specific notation :

  • local variable, not visible outside the class start with _
  • instance variables start with @
  • class variables (static)start with @@
  • global variable start with $
  • a symbol starts with : , symbols are immutable

methods

methods are define with : def... end, so for a working example :

def
1
2
3
4
5
6
7
8
9
#!/usr/bin/ruby
class Sample
    def hello
        puts "Hello Ruby!"
    end
end
# Now using above class to create objects
object = Sample.new
object.hello

So creation is done with new, but called as a method of the class.

To show an example of class variables, this example maintains a count of Customer objects created:

class
#!/usr/bin/ruby

class Customer
    @@no_of_customers=0
    def initialize(id, name, addr)
        @cust_id=id
        @cust_name=name
        @cust_addr=addr
    end
    def display_details()
        puts "Customer id #@cust_id"
        puts "Customer name #@cust_name"
        puts "Customer address #@cust_addr"
    end
    def total_no_of_customers()
        @@no_of_customers += 1
        puts "Total number of customers: #@@no_of_customers"
    end
end

# Create Objects
cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2=Customer.new("2", "Poul", "New Empire road, Khandala")

# Call Methods
cust1.total_no_of_customers()
cust2.total_no_of_customers()

The output:

output
Total number of customers: 1
Total number of customers: 2

Alternatively when you run display_details :

details
cust2.display_details()

you get :

output
1
2
3
Customer id 1
Customer name John
Customer address Wisdom Apartments, Ludhiya

This displays the way to get the value of a variable of constant, just put an # in front of it.

constants

These simply begin with an uppercase letter, so let's just take the trusted convention of all caps :

all caps
#!/usr/bin/ruby

class Example
    VAR1 = 100
    VAR2 = 200
    def show
        puts "Value of first Constant is #{VAR1}"
        puts "Value of second Constant is #{VAR2}"
    end
end

# Create Objects
object=Example.new()
object.show

Note that we need the {} around the constant, otherwise it will print #VAR1

arrays

arrays
1
2
3
4
5
6
#!/usr/bin/ruby

ary = [  "fred", 10, 3.14, "This is a string", "last element", ]
ary.each do |i|
    puts i
end

Straightforward in the declaration, except the trailing comma. which is just discarded.

ranges

Here an example will suffice i think:

ranges
1
2
3
4
5
#!/usr/bin/ruby

(10..15).each do |n| 
    print n, ' ' 
end

hashes

hashes
1
2
3
4
5
6
#!/usr/bin/ruby

hsh = colors = { "red" => 0xf00, "green" => 0x0f0, "blue" => 0x00f }
    hsh.each do |key, value|
    print key, " is ", value, "n"
end

So, the || can contain multiple values.

closures

These are called code blocks in ruby, but they are closures so why not call them that. They have the general form :

do ... end

Forms :

  • do ... end
  • do || ... end
  • do |arg_list| ... end
  • { ... }
  • { || ... }
  • { |arg_list| ... }

The closure is not executed as it is encountered, but ruby remembers the context as it is encountered, with local variables, current objects etc. And then enters the method. Once inside the method, you can call the block using the yield keyword with a value. Only then will the block be executed with the former context in place.