"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

September 22, 2012

Learning Ruby - Part I

In continuation with previous post Ruby Getting Started we will look at examples for loop, multiplication tables, File I/O operations. I am referring to Beginning Ruby by Peter Cooper for learning Ruby Programming Syntax

Example program multiple.rb

x = 1;
#Example 1 - Multiplication using 10.times;
puts "From 1 to 10 Multiplication of 5";
10.times do
       a = x*5;
       puts a;
       x = x+1;
#Example 2 - Loop using upto;
puts "From 10 to 20 Multiplication of 5";
1.upto(10) do
       a = x*5;
       puts a;
       x = x+1;
puts "Arrays"
#Example 3 - Arrays list all elements;
arrayA = [1,2,3,4,5];
arrayA.each {|y| puts y};
puts "Array & Strings"
arrayB = [1,2,"One","Two","Three"];
arrayB.each {|y| puts y};
#Example 4 - List the program line by Line;
File.open("d:\multiply.rb").each { |line| puts line }

Happy Learning!!! 

No comments: