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/
In this website, there are a lot of example for coding with Ruby.
2. What are the syntax difference in the way that Ruby and Javascript use the if statement?
In Javascript, the syntax of the if statement is as follows:
if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not true
}
Example:
if (time<10)
{
document.write("Good morning");
}
or
if (time==11)
{
document.write("Lunch-time!");
}
or
if(visitor == "teacher"){
document.write("My dog ate my homework...");
}else if(visitor == "principal"){
document.write("What stink bombs?");
} else {
document.write("How do you do?");
}
In Ruby, the if statement is as follow:
if( x < 7 && x > 12 ) { ... }
or
if x.between?(7,12) do ...
or
if income < 10000
rate = 0.02
elsif income < 30000
rate = 0.28
else
rate = 0.5
end
The else if used in the Javascript, in Ruby, it uses elsif. The Ruby language does not requires to user {} mark in beginning and the end of the if statement. There is no function x.between?(7, 12) in Javascript.
3. While Ruby and Python are quite similar, can you find some similarities between Ruby and Javascript?
Both Ruby and Javascript are requiring interpeter to run. Both are object-oriented.
Both language like C++ and Java.
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.
Code of the dognames.rb
def dognames
puts "Enter first dog name : "
name1 = gets
puts "Enter second dog name : "
name2 = gets
puts "Enter third dog name : "
name3 = gets
myarray=[name1,name2,name3]
puts "Sorted dogs names:"
print "First dog is ", myarray.sort[0]
print "Second dog is ", myarray.sort[1]
print "Third dog is ", myarray.sort[2]
end
dognames
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".
Code:
def fizzbuzz
1.upto(100) do |temp|
if temp % 5 == 0 and temp % 3 == 0
puts "FizzBuzz"
elsif temp % 5 == 0
puts "Buzz"
elsif temp % 3 == 0
puts "Fizz"
else
puts temp
end
end
end
fizzbuzz
Result:
3. 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()
By comparing both program,
a. The Python version is longer than the Ruby version.
b. The Ruby version does not require to define the main program to call the routine.
c. It is required to provide a mechanism to pause the program to hold teh result in Python while Ruby is not.
d. There is no function parameter required in Ruby program.
e. Line break or no line break output are different reserved word (puts, print) in Ruby.
Reference:
Ruby Tutorial with Code Samples (n.d.), http://www.fincher.org/tips/Languages/Ruby/
Selina D’S. (n.d.), T New Product Development with Ruby on Rails, Retrieve 10 April 2009 from www.aspiresys.com/WhitePapers/whitepaper_new_product_development-RoR_paper.pdf
w3school (2009), JavaScript If...Else Statements, Retrieve 10 April 2009 from http://www.w3schools.com/JS/js_if_else.asp
No comments:
Post a Comment