Saturday, April 3, 2010

Workshop 4 Riding the Rails with Ruby

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/

This is a very useful website, especially for programming numpties like me as it started with very basic Ruby coding examples and moves through to more complex examples. The world has certainly changed from the days when I used to dable in some basic programming owith my trusty and much loved Commodore 64. Whilst the programming code in Rudy does take some getting used to I do find it somewhat intuitive.
2.
What are the syntax differences in the way that Ruby and Javascript use the
if statement?

To code an IF loop in Javascript the following syntax is used:

"if (object < method =" action}">

Whereas Ruby would use the following syntax:

"if object < method =" action">

The differences between these two are numerous. Javascript uses brackets to encapsulate the loop; if(condition) whereas ruby does not. Ruby uses "end" to close if-else loop. Ruby also uses "end" to close the block of code. The conditional action is located inside a set of { } in javascript where they are not needed in Ruby

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

Whilst Ruby and Javascript are often compared and considered at opposite end of the programming spectrum, they do have a lot in common, beginning with the fact that they both follow some of the same object-oriented principles. Everything is an object in Ruby and, with a few small exceptions and this true for Java as well. Both languages pass objects by reference and both languages are garbage collected. Both languages are highly dynamic and are dynamically typed.


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.

Below is my code for this program. Given my limited background in programing, this particular task was very difficult and time consuming for me. I ended up searching the internat and finding a similar program to what was required and ended up editting the code the meet my requirements.


Dognames.rb


class Dognames
attr_reader :name1, :name2, :name3
def initialize()
end
def get_dognames
# get the names
puts "Enter the first dog name: "
@name1 = gets

puts "Enter the second dog name: "
@name2 = gets

puts "Enter the third dog name: "
@name3 = gets

puts "\n"
end

def order_name(x,y)
if x < dognames1 =" Dognames.new()">

When the program is run in Ruby the output is as displayed in the below image.



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".


FizzBuzz.rb

for i in 1..100 do
if(i%3 == 0 && i%5 == 0)
puts("FizzBuzz")
elsif(i%5 == 0)
puts("Buzz")
elsif(i%3 == 0)
puts("Fizz")
elsif
puts(i)
end
end

Part of the output of this program is shown in the below image.



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 <>

puts "Negative age?!? I don't think so."

elsif 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 <>

print "Negative age?!? I don't think so."

elif 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()

Comparison of Dog Years Calculator

The difference between the two programs are as follows:

  • Ruby requires less lines of code at the end of the script to run the program.
  • The method names in ruby don’t require brackets.
  • When inputting data Python looks considerably easier, only one line compared to Ruby’s two.
  • For output Python uses print, Ruby uses puts
  • Python requires ‘:‘ at the end of ‘if‘, ‘elif‘ and ‘else‘. Ruby doesn’t.
  • Ruby requires ‘end‘ after the if statement block


1 comment:

  1. Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.


    Ruby Development

    ReplyDelete