Joon
11/2/2010 - 11:07 PM

calling method

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