Skip to content

Instantly share code, notes, and snippets.

@joonyou
Created November 2, 2010 23:07

Revisions

  1. joonyou created this gist Nov 2, 2010.
    25 changes: 25 additions & 0 deletions calling method
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    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