Skip to content

Instantly share code, notes, and snippets.

@joonyou
Created November 2, 2010 23:07
Show Gist options
  • Save joonyou/660488 to your computer and use it in GitHub Desktop.
Save joonyou/660488 to your computer and use it in GitHub Desktop.
calling method
require 'rspec'
class Victim
def called_name
caller_name = caller[0].scan(/\`.*\'/).first.gsub(/\`|\'/,"") rescue "outside of self"
if caller_name =="my_caller"
"looks like it's called from my_caller"
else
"who's calling me? #{caller_name} is"
end
end
def my_caller
called_name
end
end
describe "called from another method" do
it "should return my_caller" do
Victim.new.my_caller.should eql("looks like it's called from my_caller")
end
it "should return name of method" do
Victim.new.called_name.should eql("who's calling me? outside of self is")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment