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/
Done.
2. What are the syntax differences in the way that Ruby and Javascript use the if statement?
Ruby if statement syntax is:
if condition
code to be executed if condition is true
end
Javascript if statement syntax is:
if (condition)
{ code to be executed if condition is true}
It is very obvious that Ruby is lose in syntax rule than Javascript. Javascript needs to have brackets for the condition and the code, Ruby doesn't need any brackets for condition and the code but only needs a "end" keyword to close the if statement loop.
3. While Ruby and Python are quite similar, can you find some similarities between Ruby and Javascript?
First of all, Ruby and Javascript are object-oriented programming languages therefore "everyone can modify the class structure". Secondly, function can be passed as a parameter in both Ruby (code blocks) and Javascript. Thirdly, it is easy to check if an object can do something in both JavaScript (object function check) and Ruby (respond_to) (Dan, 2007)
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 required dognames ruby coding is as follow:
This program will only accept lower case name because upper case name will always at the top of the alphabetical order. That means a mix of lower case and upper case names will produce a false result. Due to the names input from the gets command will contains a carriage return, it will affect the display result, I have use the gets.chomp command to input the names so the data will only contain the name. The output of this program will produce a very nice single line of names rather than a 3 different lines with a single name. The output is as below:
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 fizzbuzz.rb coding is listed below:
I am using a "for loop" to generate the number from 1 to 100 and divide each number by 3 and 5. The program will identify what the remainder is and decide to display the number, Fizz, Buzz or FizzBuzz. In order to give a more clear result, I have added a additional condition which will start a new line after display ten numbers. Therefore, the running result shown below will be 10 lines with 10 numbers on it.
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()
The main differences are :-
1. Python needs to have a program main body (def main ()) which ruby doesn't need.
2. Ruby uses "gets" to input the data , but python uses "input" to get the data.
3. The if loop structure is similar but the keyword has a little bit different for the case of else if. Ruby use "elsif" but python use "elif".
4. Ruby uses "puts" to display data , python uses "print" to display data.
No comments:
Post a Comment