Forum: Ruby FInding out the name of the method from which I was called?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
953d32e245f7eb0e670bde0577f1eb43?d=identicon&s=25 Pito Salas (pitosalas)
on 2009-02-02 18:18
(Received via mailing list)
Is there a clever meta programming way for me to do this:

def calling_method_1

  reporting_method

end


def reporting_method_2

  puts "Hey I was just called from #{magic}"

end

and when running reporting_method_2 this prints

Hey I was just called from calling_method_1

Thanks for any pointers!

-- Pito
8853f712852b03ba6646b59b1723f44d?d=identicon&s=25 Flower Born (flowerborn)
on 2009-02-02 18:31
(Received via mailing list)
* R. Pito Salas <rps@salas.com> [2009-02-03 02:17:29 +0900]:

>
>   puts "Hey I was just called from #{magic}"
>
> end

def reporting_method
  caller.shift
end

Check Kernel#caller.

Jan
E67536b7234fcf4e0c01b100826cfc60?d=identicon&s=25 Lars Haugseth (Guest)
on 2009-02-04 00:15
(Received via mailing list)
* "R. Pito Salas" <rps@salas.com> wrote:
> def reporting_method_2
>
>   puts "Hey I was just called from #{magic}"
>
> end
>
> and when running reporting_method_2 this prints
>
> Hey I was just called from calling_method_1
>
> Thanks for any pointers!

Kernel#caller
http://www.ruby-doc.org/core/classes/Kernel.html#M005955

--
Lars Haugseth

"If anyone disagrees with anything I say, I am quite prepared not only
to
 retract it, but also to deny under oath that I ever said it." -Tom
Lehrer
This topic is locked and can not be replied to.