Ruby Simplified Part 4: Ruby Loops

Let’s look at some Ruby loop constructs: for, while, and until.

# .Each loop
x = 10
x.times do |i|        
    puts "#{i}"    # Starts at 0, ends at 9
end

# For loop
for i in 0..10 do  # Needs a range
    puts "#{i}"    # Starts at 0, ends at 10
end

# While loop
x = 0
while x < 10
    puts "#{x}"    # Starts at 0, ends at 9
    x += 1
end

# Until loop
x = 0
until x >= 10 do
    puts "#{x}"    # Starts at 0, ends at 9
    x += 1
end

Ruby also supports break and next, these are equivalent to break and continue in C#.

# Break
for i in 0..10
    if i > 5
        break      # Terminate the most internal loop
    end
    puts "#{i}"    # Will print 0 1 2 3 4 5
end

# Next
for i in 0..10
    if i < 5
        next
    end
    puts "#{i}"    # Will print 5 6 7 8 9 10
end

Ruby Simplified Part 3: Ruby conditional statements

Let’s look at some conditional statements in Ruby, if-else, case, and unless.

=begin
A simple program to test user input,
notice that this comment is multiline
=end

# If-Else statement
puts "Please input number..."
x = gets        # Get user input
x = x.chomp     # Chop off the '\n' at the end
x = x.to_i      # convert the string into an integer

if (x < 0) 
    puts "user input " + x.to_s + " is negative"    # convert integer into string with .to_s
elsif (x == 0)
    puts "user input " + x.to_s + " is zero"
else
    puts "user input " + x.to_s + " is positive"
end

# The 'if' modifier
puts "Writes this line only if x is 1" if x == 1

# Case statement
puts "Please input another number..."
x = gets.chomp.to_i     # Notice how we shorten the operations

case
    when x < 0 
        puts "user input " + x.to_s + " is negative" 
    when x == 0
        puts "user input " + x.to_s + " is zero"
    when x > 0
        puts "user input " + x.to_s + " is positive"
end

# Unless statement
puts "Please input another number..."
x = gets.chomp.to_i

unless x > 0        # The equivalent of 'unless' is the if(!...) in C#
    unless x == 0
        puts "user input " + x.to_s + " is negative"
    else
        puts "user input " + x.to_s + " is zero"
    end
else
    puts "user input " + x.to_s + " is positive"
end

# Unless conditional
puts "Write this line unless x is 0, meaning, if x is 0 then don’t write this line" unless x == 0

# Anything inside 'BEGIN' is called before the program is run
BEGIN { puts "Simple program to test user input." }

# Anything inside 'END' is called after the program is run
END { puts "All done!" }

Ruby Simplified Part 2: Major language features

Some features of the Ruby programming language, comparing them with C#,

1. Ruby is a dynamically typed programming language.
    C# got dynamic extensions to the language in C# 4.0

2. Ruby is open source. Not only is it free of charge, but also free to use, copy, modify, and distribute.
    C# is not open source, for an open source implementation of the ECMA standards for C#, see the Mono Project.

3. Ruby was designed as an interpreted language. The first Ruby interpreter (MRI) was written in C and was a single pass interpreter. The current official interpreter (YARV), however, compiles Ruby into something called ‘YARV Instruction Sequence’, which is then compiled just-in-time (JIT) to assembly language. There are several other implementations of Ruby (ex., JRuby, IronRuby, and MacRuby) which also compile the Ruby code in a two step process to machine language.
    C# also has a two-step process, it’s first compiled into an intermediate language (IL) by the C# compiler and then JIT compiled to machine language by the CLR.

4. Ruby is a pure object oriented language. In Ruby everything is an object. You can append .class to anything to get the class name. Type puts 1.class (you’ll get ‘Fixnum’).
    In C#, not everything is a object (numbers, structs, etc.)

5. Ruby has automatic memory management.
    C# also has a full fledged garbage collector.

6. Ruby supports single inheritance only. Although we’ll see how to implement multiple inheritance using modules/mixins.
    C# also supports single inheritance only. Multiple inheritance in C# is implemented using interfaces.

7. Ruby has modern exception handling features (using begin-rescue-ensure).
    C# equivalent of this is the try-catch-finally blocks.