Friday, April 16, 2010

Workshop 4: Riding the Rails with Ruby

Topic objectives
• To learn about the Ruby language and its classes and methods;
• To use Ruby via an interpreter console window with Windows, Linux or MacOS
• To select and test a Ruby IDE
• To explain how Rails framework is built upon inheritance of Ruby classes and methods.


To do:

1. Spend some time moving your way through the 46 Ruby coding examples in the Ruby Tutorial with Code from http://www.fincher.org/tips/Languages/Ruby/
2. What are the syntax differences in the way that Ruby and Javascript use the if statement?

The main syntax different between Ruby and JavaScript using the "if" statement is that the "else if" condition in Ruby is stick together as "elseif" and there is not () brackets for condition statement and no {} brackets for the if else statement. Both if else conditional sample codes are attached as follows.

Ruby:
if var == 10
print “Variable is 10″
elsif var == “20″
print “Variable is 20″
else
print “Variable is something else”
end

JavaScript:
if (time < 10)
{
document.write("Good morning!");
}
else
{
document.write("Good day!");
}


3. While Ruby and Python are quite similar, can you find some similarities between Ruby and Javascript?

JavaScript and Ruby are very similar in many aspects, but they are also very different In some areas. Both languages are highly dynamic, allowing you to change objects and methods at runtime and both languages are very object-oriented. Both can use variables to hold data and reference to other objects.

In JavaScript we use function to provide scope. This means that for and while loops, for example, do not have their own scope. this is the same In Ruby, but while Ruby has classes and modules to provide shelter from the global scope.

There are two methods in JavaScript which are "call" and "apply". These methods allow you to call a function. With "call", you give it the arguments like you would when calling it directly but with "apply" you pass the arguments as an array. The "apply" version is similar to "splatting" an array in a similar situation in Ruby.

The concept of class method for both JavaScript and Ruby are the same which instantiates a new object from that class. The classes are also "open" and you can extend any class with new methods for both.


Recommended time: 1-4 hours, but it may take several trial attempts so be patient with initial success.



Challenge Problems:

1. Create, test and debug a Ruby program called dognames.rb or catnames.rb to accept 3 names from the keyboard and to display each name on the screen in alphabetical order WITHOUT using a data structure such as a list.

The code is created as below:

def dognames
puts "Enter the first dog name: "
$dogname1 = gets
puts "Enter the second dog name: "
$dogname2 = gets
puts "Enter the third dog name: "
$dogname3 = gets

if $dogname1 > $dogname2
$temp = $dogname1
$dogname1 = $dogname2
$dogname2 = $temp
end

if $dogname2 > $dogname3
$temp = $dogname2
$dogname2 = $dogname3
$dogname3 = $temp
end

if $dogname1 > $dogname2
$temp = $dogname1
$dogname1 = $dogname2
$dogname2 = $temp
end

puts "Dog names in alphabetical order:"
puts $dogname1, $dogname2, $dogname3

end
dognames

The above code is saved to file dognames.rb. It is tested and debugged by making use of the Ruby Interpreter (C:\Ruby\bin\irb.bat) and issue the command "irb dognames.rb" which successfully generates the desired result as follows:




2. Write a Ruby program called fizzbuzz.rb that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".

The code is created as below:

def fizzbuzz

1.upto(100) do |i|
if i % 5 == 0 and i % 3 == 0
puts "FizzBuzz"
elsif i % 5 == 0
puts "Buzz"
elsif i % 3 == 0
puts "Fizz"
else
puts i
end
end
end

fizzbuzz


Part of the output is generated as follow:





Compare the Ruby and Python versions of the dog years calculator:

#!/usr/bin/ruby
# The Dog year calculator program called dogyears.rb

def dogyears
# get the original age
puts “Enter your age (in human years): "
age = gets # gets is a method for input from keyboard
puts # is a method or operator for screen output

#do some range checking, then print result
if age < 0
puts "Negative age?!? I don't think so."
elsif age < 3 or age > 110
puts "Frankly, I don't believe you."
else
puts "That's", age*7, "in dog years."
end
dogyears

Python

#!/usr/bin/python
# The Dog year calculator program called dogyears.py

def dogyears():
# get the original age
age = input("Enter your age (in human years): ")
print # print a blank line

# do some range checking, then print result
if age < 0:
print "Negative age?!? I don't think so."
elif age < 3 or age > 110:
print "Frankly, I don't believe you."
else:
print "That's", age*7, "in dog years."

### pause for Return key (so window doesn't disappear)
raw_input('press Return>')

def main():
dogyears()
main()


After comparing the Ruby and Python code which perform the same function to calculate the dog age by multiplying the human age by 7, it is found that the logical flow and syntax is very familiar. With only some minor syntax difference for the If … Else condition. Also Python requires additional code to pause the output screen.



Reference

How-To Greek. (2010). Ruby IF, Else If Command Syntax. Retrieved 15 Apr, 2010, from http://www.howtogeek.com/howto/programming/ruby/ruby-if-else-if-command-syntax/

W3schools.com. (2010). JavaScript If…Else Statements. Retrieved 15 Apr, 2010, from http://www.w3schools.com/js/js_if_else.asp

Sneaky Abstractions. (2010).JavaScript eye for the Ruby Guy. Retrieved 15 Apr, 2010, from http://tore.darell.no/pages/javascript_eye_for_the_ruby_guy

No comments:

Post a Comment