Sunday, April 4, 2010

Workshop 4 - Riding the Rails with Ruby

Now down to some actual ruby coding. This workshop provided an insight into the actual code of ruby with some great examples. It contrasted the various styles of java vs ruby in syntax, with some coding examples to figure out in ruby.

To Do
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 = condition)
{
'code to be executed
}

Whereas Ruby would use the following syntax:

if object = condition
'code to be executed
end
The differences are as follows:
  • javscript must contain the condition rule within brackets ()
  • javscript must have contain open and close {} squiggly line brackets for code to be executed
  • javascript uses else if
  • ruby doesn't need to have brackets around the condition
  • ruby doesn't need to contain {} squiggly line brackets for encapsulation of the code to be executed
  • ruby uses an elsif instead of else if
  • ruby to close of the if statement uses and 'end'

While Ruby and Python are quite similar, can you find some similarities between Ruby and Javascript?
  • Both are based on object-orientated principles
  • Both languages are dynamically typed
  • Both pass objects by reference
  • Both languages are garbage collected, as in they have memory management


Challenge Problems
Now here comes the coding parts of the workshops...

1. Create and test a Ruby program called dognames.rb

Here is the code of my program dognames.rb, as you can see i take in a name variable and assign each item into the array, once the array has been defined i sort the array and output the result.

#!/usr/bin/ruby

puts "What is your dog name?"
$dog1 = gets

puts "What is your dog name?"
$dog2 = gets
puts "What is your dog name?"
$dog3 = gets
array = [$dog1,$dog2,$dog3]
puts "your dog name"
puts array.sort


Test 1: ('Pearl', 'Coco', 'Butch')
Result 1:










Test 2: ('Pearl', 'Andy', 'Butch')
Result 2:











Test 3: ('Pearl', 'Andy', 'Fifi')
Result 3:











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

Here is the code below to be able to produce the expected result using Ruby, as we can see there is a loop involved going upto the number 100, then conditional if statements to replace numbers with the words if met.

#!/usr/bin/ruby

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


Output is as follows from the command prompt when executed.
























3. Compare the Ruby and Python versions of the dog years calculator
  • Ruby doesn't need brackets () for declaring a method.
  • Python used an input() method for capturing data entered via the keyboard.
  • Ruby used a gets method for capturing data via the keyboard.
  • Ruby uses if statement without : at the end of statement but Python uses it.
  • Ruby uses a puts method or operator for screen output but Python uses print.
  • Python must have a main function in order for the program to run.

No comments:

Post a Comment