0

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?

2
  • Why not initialise the class with a block? Commented Jul 23, 2013 at 16:37
  • @Mohamad: Sorry, not following you. Commented Jul 23, 2013 at 16:39

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.