Project

General

Profile

Backport #2376 ยป __method__spec.rb

shugo (Shugo Maeda), 12/06/2009 01:19 AM

 
require File.dirname(__FILE__) + '/../../spec_helper'
require File.dirname(__FILE__) + '/fixtures/classes'

def f
__method__
end
alias g f

describe "Kernel.__method__" do

it "returns the current method, even when aliased" do
f.should == :f
end

it "returns the original name when aliased method" do
g.should == :f
end

it "returns the caller from blocks too" do
def h
(1..2).map { __method__ }
end
h.should == [:h, :h]
end

it "returns the caller from define_method too" do
klass = Class.new {define_method(:f) {__method__}}
klass.new.f.should == :f
end

it "returns the caller from block inside define_method too" do
klass = Class.new {define_method(:f) { 1.times{break __method__}}}
klass.new.f.should == :f
end

it "returns the caller from a define_method called from the same class" do
class C
define_method(:f) { 1.times{break __method__}}
def g; f end
end
C.new.g.should == :f
end

it "returns method name even from eval" do
def h
eval "__method__"
end
h.should == :h
end

it "returns nil when not called from a method" do
__method__.should == nil
end

end
    (1-1/1)