Sunday, June 28, 2009

Answers to Ruby Review #3

What is the Ruby object that's a number but not an integer called?
Numbers that aren't integers are called floats.

What's the syntax of the Ruby case statement?
case x
   when 5 then puts 'x == 5'
   when 15 then puts 'x == 15'
   when 10 then puts 'x == 10'

   else puts "I don't know what x is equal to."
end

What happens if more than one of the statements within a case statement evaluates to true?
The first one is executed.

What objects can be stored in a Ruby array?
Any object can be stored in a Ruby array.

What is an iterator?
An iterator is a method that acts essentially like a loop.

What is a block?
A block is code that follows a method and intereacts with the code in the method.

What is the difference between a block that uses 'do' and 'end' and a block that uses '{' and '}'?
While there are differences, these differences are not important when you are first learning Ruby.

How do you pass a value into a method?
You create a method that accepts a value by adding a variable name (or names) within the parentheses following the method name on the def line. If a pre-defined method accepts a parameter (or parameters) they can be added following the method call (the parenthesis are usually optional).

How do you pass a value from a method into a block?
You pass control of execution fro the method to the block vi the yield method. You can pass an object from the method to the block by including that object after the yield method call. In this case as well, the parentheses are optional.

No comments:

Post a Comment