CIT 597 Quiz 8: Ruby
Fall 2008, David Matuszek Name_________________________________________
  1. (5 points) Which of Java's arithmetic operators are not available in Ruby?   ++ and --

  2. (5 points) What arithmetic operator does Ruby have that isn't available in Java? Exponentiation, **

  3. (10 points) List two ways in which Ruby's case statement differs semantically (that is, ignoring syntax differences) from Java's switch statement.
    break is not required
    Tests are a lot more flexible (use === operator)

  4. (20 points) True or false:
    1.   true   Function calls sometimes need parentheses around the arguments.
    2.  false if statements sometimes need parentheses around the condition.
    3.   true   if statements sometimes use the word then.
    4.   true   All function definitions belong to some class.
    5.  false Functions may be overloaded.
    6.   true   Ruby's regular expressions are very similar to Perl's.
    7.   true   The range 1..5 includes the value 5.
    8.  false [1..5] denotes an array with more than one element.
    9.   true   If a = [1, 2, 3], then a[500] = 'hello' is a valid statement.
    10.   true   When if is used as a statement suffix, end is not required.

  5. (10 points) Use the each iterator to print out all the elements of the array a, one per line.
    a.each { |v| puts v }     or     a.each do |v| puts v end

  6. (5 points) What keywords does Ruby use in place of catch and finally? rescue and ensure

  7. (20 points) In Ruby, how would you write:
    1. An instance variable "value"?    @value
    2. A class variable "unique_id"?  @@unique_id
    3. A global variable "limit"?      $limit
    4. A local variable "temp"?      temp
    5. The symbol "summer"?      :summer

  8. (5 points) You can start an interactive Ruby session with the command   irb .

  9. (5 points) What is printed by the following three lines?
           x = 12
          def foo; x = 8; yield; end       12
          foo { puts x }
  10. (15 points) What does each of the following methods do?
    1. chop
      Returns a new string with the last character deleted
    2. chomp
      Returns a new string with the last character deleted, if that character is a newline
    3. chomp!
      Removes the last character from the given string, if that character is a newline