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.
Pito S. (Guest)
on 2009-02-02 19: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
Flower B. (Guest)
on 2009-02-02 19:31
(Received via mailing list)
* R. Pito S. <removed_email_address@domain.invalid> [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
Lars H. (Guest)
on 2009-02-04 01:15
(Received via mailing list)
* "R. Pito S." <removed_email_address@domain.invalid> 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 H.

"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.