0

例如,我该如何实现以下内容:

class A
  def initialize
    b = B.new
  end
end

class B
  def initialize
   puts #how can I find out who instantiated me
  end
end
4

1 回答 1

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

class B
  def initialize initializer
    puts initializer
  end
end

A.new # => #<A:0x007fee78368dc8>
于 2013-10-01T23:34:30.877 回答