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:
code: | 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:
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
- 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?
code: | 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.
code: | 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:
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
- Aside from a bit shift, what would you use the << operator for in Ruby?
5 points
- Write code that opens a file for appending and reading.
5 points
- 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
- Consider the hash:
code: | baz = {"foo" => "bar"} |
Explain what changes would be necessary to make:
Return "bar".
The changes may not affect other hashes in the same program.
10 points
- 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
- 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
- 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
- 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:
code: | class Foo
include Enumerable
def each
yield 42
yield 27
end
end |
Calling:
Should return:
10 points
|