views:

28

answers:

2

My problem basically looks like this:

module Foo
  class Bar
    def self.who
      self.class.to_s
    end
  end
end

class World < Foo::Bar

end

When I call World.who I don't get "World" as a result, I get "Class". Some quick Googling didn't yield anything useful, so hence I'm here hoping someone will know how to get the correct class name :)

+2  A: 

You get that because World is a Class. In ruby, AClass.class != AClass. So, you could use this:

module Foo
  class Bar
    def self.who
      to_s
    end
  end
end

class World < Foo::Bar

end
Adrian
+2  A: 

If you're calling foo.bar then inside the bar method the value of self will be foo. So when you call World.who the value of self inside who is World. Since World is a class, World.class will return Class, so that's what you get.

To get back "World" just call self.to_s or self.name (or just to_s or name).

sepp2k