
-----------------------------------
wtd
Wed Dec 07, 2005 9:21 pm

The Ruby Test
-----------------------------------
Test #1

Please private message me with your answers.  I'll post a list of those who've successfully completed the test, which, for those who are curious, is a score of 125 points, with at least one 20 point question answered successfully. 


Explain the differences between:
   
name
$name
@name
@@name
NAME

5 points

Explain the significance of ! and ? as suffixes on method names.

5 points

Consider the following snippet from irb:

irb(main):001:0> class Foo
irb(main):002:1>    private
irb(main):003:1>    def bar
irb(main):004:2>      "baz"
irb(main):005:2>    end
irb(main):006:1> end
=> nil
irb(main):007:0> Foo.new.bar
NoMethodError: private method `bar' called for #
        from (irb):7

Call the bar method on a Foo object regardless of the "private" modifier.

10 points
        
Explain the differences between public, private, and protected.

5 points

Explain how the public, private, and protected modifiers work in Ruby.  How can the following make both the bar and baz methods private?

class Foo
   private
   def bar
      "bar"
   end
   
   def baz
      "baz"
   end
end

20 points

Explain how Ruby makes it possible for a method defined outside of a class to be used inside a class.

def bar
   "bar"
end

class Foo
   def baz
      bar
   end
end

10 points

a) Explain why an object, even if it "is a" Enumerable, cannot be guaranteed to have a working "collect" method.

5 points

b) Demonstrate how to check to make sure an object does have a working "collect" method.

5 points

Explain what the ternary operator is, and how it can be useful.

5 points

Write a program which reads through a simple plain text log file.  The log file is in CSV format and each record contains, in this order: the filename, the date as the number of seconds since epoch, and the name of the user who made the request.

The program should print this information to the console, but with the time in a human-readabe format.

Assume the log file can be of any length.  Make your program reasonably memory efficient.

10 points

Consider the following method:

$correct_answer = 42

def guess(number_of_guesses = 3)
   user_guess = gets.to_i
   
   if user_guess == $correct_answer
      puts "Correct!"
   else
      raise "Wrong..."
   end
end

Modify this method so that: 


The exception never escapes the method.
The user is only allowed to guess at most the number of times indicated by the method's parameter.
The current body of the method is left intact.


10 points

Aside from a bit shift, what would you use the  "bar"}

Explain what changes would be necessary to make:

baz["fOO"]

Return "bar".

The changes may not affect other hashes in the same program.

10 points

Consider the following:

irb(main):001:0> arr = [54, 67, 23, 42, 27, 98, 12, 84]
=> [54, 67, 23, 42, 27, 98, 12, 84]
irb(main):002:0> arr.values_at(1, 3, 5)
=> [67, 42, 98]

Let's assume arr is some arbitrary length, greater than 10,000 elements.  Write a reasonably concise bit of code which uses the values_at method to retrieve only the elements at even indexes.

10 points

Let's say we have a plain text data file that has information about people stored as "key=value" pairs separated by commas.

Each record should be parsed into a hash.

def parse_record(line)
   record = {}
   line.split(/\s*,\s*/).each do |pair|
      key, pair = pair.split(/\s*=\s*/, 2)
      record[key] = pair
   end
   record
end

Modify this to provide significantly better memory efficiency for large datasets.

10 points

Take a name class:

class Name
   def initialize(first, last)
      @first, @last = first, last
   end
end

Make it a valid Enumerable object.

5 points

Modify the Array class so that when a new value is pushed (with the push method) onto the end of the array, certain actions are carried out.

These actions should be specified at run-time for a given Array object by an "on_push" method you create.  The "actions" should be aware of the arguments being provided to "push".

20 points

Add a "reverse" method to the Enumerable module which outputs an Array.  Implement your reverse method using only the "inject" method.

Test it with:

class Foo
   include Enumerable
   
   def each
      yield 42
      yield 27
   end
end

Calling:

Foo.new.reverse

Should return:

[27, 42]

10 points



-----------------------------------
octopi
Sun Aug 13, 2006 4:37 pm


-----------------------------------
...Since its been 8 months would you consider starting to post some anwsers the the questions?
