Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 The Ruby Test
Index -> Programming, Ruby -> Ruby Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Wed Dec 07, 2005 9:21 pm   Post subject: 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.


  1. Explain the differences between:

    code:
    name
    $name
    @name
    @@name
    NAME


    5 points

  2. Explain the significance of ! and ? as suffixes on method names.

    5 points

  3. Consider the following snippet from irb:

    code:
    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 #<Foo:0x2dc0568>
            from (irb):7


    Call the bar method on a Foo object regardless of the "private" modifier.

    10 points

  4. Explain the differences between public, private, and protected.

    5 points

  5. Explain how the public, private, and protected modifiers work in Ruby. How can the following make both the bar and baz methods private?

    code:
    class Foo
       private
       def bar
          "bar"
       end
       
       def baz
          "baz"
       end
    end


    20 points

  6. Explain how Ruby makes it possible for a method defined outside of a class to be used inside a class.

    code:
    def bar
       "bar"
    end

    class Foo
       def baz
          bar
       end
    end


    10 points

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

  8. Explain what the ternary operator is, and how it can be useful.

    5 points

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

  10. Consider the following method:

    code:
    $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

  11. Aside from a bit shift, what would you use the << operator for in Ruby?

    5 points

  12. Write code that opens a file for appending and reading.

    5 points

  13. Without seeing the code that created a range "foo", write a line of code which determines whether or not the range was created with ".." or "...".

    5 points

  14. Consider the hash:

    code:
    baz = {"foo" => "bar"}


    Explain what changes would be necessary to make:

    code:
    baz["fOO"]


    Return "bar".

    The changes may not affect other hashes in the same program.

    10 points

  15. Consider the following:

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

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

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

  17. Take a name class:

    code:
    class Name
       def initialize(first, last)
          @first, @last = first, last
       end
    end


    Make it a valid Enumerable object.

    5 points

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

  19. Add a "reverse" method to the Enumerable module which outputs an Array. Implement your reverse method using only the "inject" method.

    Test it with:

    code:
    class Foo
       include Enumerable
       
       def each
          yield 42
          yield 27
       end
    end


    Calling:

    code:
    Foo.new.reverse


    Should return:

    code:
    [27, 42]


    10 points

Sponsor
Sponsor
Sponsor
sponsor
octopi




PostPosted: Sun Aug 13, 2006 4:37 pm   Post subject: (No subject)

...Since its been 8 months would you consider starting to post some anwsers the the questions?
Display posts from previous:   
   Index -> Programming, Ruby -> Ruby Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: