0

For example, how could I implement the following:

class A
  def initialize
    b = B.new
  end
end

class B
  def initialize
   puts #how can I find out who instantiated me
  end
end
1
  • See here. Commented Oct 1, 2013 at 23:51

1 Answer 1

3
class A
  def initialize
    B.new(self)
  end
end

class B
  def initialize initializer
    puts initializer
  end
end

A.new # => #<A:0x007fee78368dc8>
Sign up to request clarification or add additional context in comments.

2 Comments

Is there anyway for Class B to access it's "creator" object without self being explicitly passed?
Maybe you can play around with TracePoint. But I don't see any reason for doing that, and I would not do that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.