Search code examples
rubyeigenclass

Anonymous classes in Ruby


I have two questions:

  1. Does method f_1 belong to the metaclass anonymous class?
  2. Does method f_2 belong to the anonymous class?

related to the following code:

car = "car"

class << car
  def self.f_1
    puts "f_1"
  end
  def f_2
    puts "f_2"
  end
end

Solution

  • Well, terminology is frangible, but FWIW I would say your class wasn't really an anonymous class. As for belonging, both of these methods only exist in the car object.

    I'll be honest and admit that I'm a little vague about the difference between a class method and an instance method when the class is defined against an individual object like this -- I would guess that if there is any difference, it will be an obscure one that will make your code much harder to read ;)

    Update: You might find this helpful, if you've not seen it before. (Personally, it makes my head hurt, but everyone's different...)