Reputation: 1227
I'm trying to create a singleton class that requires some sophisticated initialization. I've boiled my problem down to this test case:
class Dumb
attr_accessor :mything
@my_thing = 1 # this works
self.init_some_stuff # this gives undefined method
class << self
def init_some_stuff
@my_thing = 2
end
def spill_it
puts "My Thing: #{@my_thing}"
end
end
end
I can initialize simple variables, but want to call class methods to do it, and I get "undefined method". Since I intend it to be used as a singleton, a constructor would not get called. What am I missing?
Upvotes: 0
Views: 1415
Reputation: 168101
A method is executed whenever it is met.
self.init_some_stuff
is placed before the definition of it. That is the problem. Place it after the definition.
Upvotes: 1