Saturday, March 10, 2007

An answer to PragDave Quick Ruby Quiz

Dave Thomas in Quick Ruby Quiz asked how to call method say_hello in
class Fred
  class << self
    def self.say_hello
      puts "Hi!"
    end
  end
end
I have figured out one of possible way is
klass = class << Fred; self; end
klass.singleton_methods #=> ["constants","nesting",
                        # "say_hello"]
klass.say_hello #Hi!

No comments: