Any way to determine which object called a method?

后端 未结 5 1815
悲哀的现实
悲哀的现实 2020-12-30 01:05

I\'m hoping that Ruby\'s message-passing infrastructure means there might be some clever trick for this.

How do I determine the calling object -- which object called

5条回答
  •  自闭症患者
    2020-12-30 01:15

    You can easily look at the line of code that called the function of interest through

    caller.first
    

    which will tell you the filename and line number which called the relevant function. You could then back-calculate which object it was.

    However, it sounds like you're more after some object that called a certain function, perhaps within an instance method. I'm not aware of a method for figuring this out - but I wouldn't use it anyway, since it seems to violate encapsulation badly.

提交回复
热议问题