0

Say I have a function:

def foo()
puts getFunctionIAMIn()
end

I want the output to be:"foo" And if I have this code:

def foo1()
puts getFunctionIAMIn()
end

I want the output to be:"foo1"

user1134991
  • 3,003
  • 2
  • 25
  • 35

2 Answers2

5

Just write as below using __method__:

def foo()
  puts __method__
end

Above is correct, but __callee__ sounds more technically correct.

__method__ returns defined name, and __callee__ returns called name.They are same usually, but different in a aliased method.

def foo
  [__method__, __callee__]
end

alias bar foo
p foo #=> [:foo, :foo]
p bar #=> [:foo, :bar]
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
2

You can use __method__ for that:

def test_method
  __method__
end
Severin
  • 8,508
  • 14
  • 68
  • 117