Obtener el nombre del método que se está ejecutando actualmente
$0
¿es la variable para el programa Ruby de nivel superior, pero hay una para el método actual?
5 answers
Incluso mejor que mi primera respuesta puedes usar _ _ método__:
class Foo
def test_method
__method__
end
end
Esto devuelve un símbolo – por ejemplo, :test_method
. Para devolver el nombre del método como una cadena, llame a __method__.to_s
en su lugar.
Nota: Esto requiere Ruby 1.8.7.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-19 03:47:50
De http://snippets.dzone.com/posts/show/2785:
module Kernel
private
def this_method_name
caller[0] =~ /`([^']*)'/ and $1
end
end
class Foo
def test_method
this_method_name
end
end
puts Foo.new.test_method # => test_method
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-10-14 00:21:38
Dependiendo de lo que realmente desee, puede usar __method__
o __callee__
, que devuelven el nombre del método actualmente en ejecución como símbolo.
En ruby 1.9, ambos se comportan de manera idéntica (en lo que respecta a los documentos y mis pruebas).
En ruby 2.1 & 2.2 __callee__
se comporta de manera diferente si llama a un alias del método definido (consulte la descripción doc ligeramente diferente)
Script de prueba:
require 'pp'
puts RUBY_VERSION
class Foo
def orig
{callee: __callee__, method: __method__}
end
alias_method :myalias, :orig
end
pp( {call_orig: Foo.new.orig, call_alias: Foo.new.myalias} )
1.9.3 Salida:
1.9.3
{:call_orig=>{:callee=>:orig, :method=>:orig},
:call_alias=>{:callee=>:orig, :method=>:orig}}
2.1.2 Output (__callee__
devuelve el nombre aliased, pero __method__
devuelve el nombre en el punto en el que se definió el método):
2.1.2
{:call_orig=>{:callee=>:orig, :method=>:orig},
:call_alias=>{:callee=>:myalias, :method=>:orig}}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-25 18:05:49
Para Ruby 1.9+ recomiendo usar __callee__
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-12-04 02:46:56
Tengo el mismo problema para recuperar el nombre del método en el archivo view. Tengo la solución por
params[:action] # it will return method's name
Si desea obtener el nombre del controlador entonces
params[:controller] # it will return you controller's name
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-12 12:59:11