__method__spec.rb
| 1 |
require File.dirname(__FILE__) + '/../../spec_helper' |
|---|---|
| 2 |
require File.dirname(__FILE__) + '/fixtures/classes' |
| 3 |
|
| 4 |
def f |
| 5 |
__method__ |
| 6 |
end
|
| 7 |
alias g f |
| 8 |
|
| 9 |
describe "Kernel.__method__" do |
| 10 |
|
| 11 |
it "returns the current method, even when aliased" do |
| 12 |
f.should == :f
|
| 13 |
end
|
| 14 |
|
| 15 |
it "returns the original name when aliased method" do |
| 16 |
g.should == :f
|
| 17 |
end
|
| 18 |
|
| 19 |
it "returns the caller from blocks too" do |
| 20 |
def h |
| 21 |
(1..2).map { __method__ } |
| 22 |
end
|
| 23 |
h.should == [:h, :h] |
| 24 |
end
|
| 25 |
|
| 26 |
it "returns the caller from define_method too" do |
| 27 |
klass = Class.new {define_method(:f) {__method__}} |
| 28 |
klass.new.f.should == :f
|
| 29 |
end
|
| 30 |
|
| 31 |
it "returns the caller from block inside define_method too" do |
| 32 |
klass = Class.new {define_method(:f) { 1.times{break __method__}}} |
| 33 |
klass.new.f.should == :f
|
| 34 |
end
|
| 35 |
|
| 36 |
it "returns the caller from a define_method called from the same class" do |
| 37 |
class C |
| 38 |
define_method(:f) { 1.times{break __method__}} |
| 39 |
def g; f end |
| 40 |
end
|
| 41 |
C.new.g.should == :f |
| 42 |
end
|
| 43 |
|
| 44 |
it "returns method name even from eval" do |
| 45 |
def h |
| 46 |
eval "__method__"
|
| 47 |
end
|
| 48 |
h.should == :h
|
| 49 |
end
|
| 50 |
|
| 51 |
it "returns nil when not called from a method" do |
| 52 |
__method__.should == nil
|
| 53 |
end
|
| 54 |
|
| 55 |
end
|