Ruby Language Features Interview Questions

Write a Math module to extend the Fixnum class. This module should provide two methods. First, a class method called pi that prints out the value of pi to two significant digits. Second, an instance method called squared that squares a number. E.g. when you are finished the following should be possible:

puts Fixnum.pi
# 3.14
puts 2.squared
# 4

Suppose you have a class that performs several expensive calculations. However, during the lifetime of an object, the result of the expensive calculation won't change. Therefore, you wish to ensure that each calculation is performed only once, and that result is cached. A simple technique for this would be as follows:

class Calculator
    def expensive_calc_one
        return @result1 unless @result1.nil?
       @result1 = # Do very expensive calculation.
    end

   def expensive_calc_two
        return @result2 unless @result2.nil?
       @result2 = # Do very expensive calculation.
    end
end

If this class contained many such expensive calculations, this memoization technique would become repetitive. Can you come up with a framework that reduces this repetition by allowing one to simply mark a method as memoized and no longer have to worry about manually handling the caching?

Bonus points: There is a flaw in the simple technique. Under one set of circumstances the caching will fail and repeated calls to expensive_calc_one will result in the expensive calculation being executed again and gain. When would this occur?

Suppose you have the following array

stuff = [:dog,:cat,:orange,:banana]
  1. How can you slice this array to create a new array [:cat,:orange]
  2. Add the element :apple on to the end of the array.
  3. Now take :apple back off again
  4. Add the element :fish to the start of the array.
  5. Now remove the element :fish.

What will original and copy evaluate to in each of the following examples? Explain why there is a difference.

original = "hello"
copy = original
copy << " there"
puts copy
puts original
original = "hello"
copy = original
copy += " there"
puts copy
puts original

In each example below indicate whether or not we should be asserting true or false. (E.g. what should the ? be?).

Explain the difference between ==, eql and equal?

str1 = "chitter"
str2 = "chitter"

assert_equal ?, str1 == str2
assert_equal ?, str1.eql?(str2)
assert_equal ?, str1.equal?(str2)
assert_equal ?, str1.object_id == str2.object_id
sym1 = :chatter
sym2 = :chatter

assert_equal ?, sym1 == sym2
assert_equal ?, sym1.eql?(sym2)
assert_equal ?, sym1.equal?(sym2)
assert_equal ?, sym1.object_id == sym2.object_id

Define the following object oriented concepts:

  • Class, object (and the difference between the two)
  • Instantiation
  • Method (as opposed to, say, a C function)
  • Static methods and classes
  • Destructor/finalizer
  • Inheritance
  • Encapsulation
  • Multiple inheritance (and give an example)
  • Abstract class
  • Interface/protocol (and different from abstract class)
  • Method overriding
  • Method overloading (and difference from overriding)
  • Polymorphism (without resorting to examples)
  • Method visibility (e.g. public/private/other)