2009-02-28から1日間の記事一覧

追記 (2009-03-01)

okkez さんに Kernel.__method__ を教えてもらった。 RUBY_VERSION # => "1.9.1" def foo __method__ # => :foo end def bar foo __method__ # => :bar end bar __method__ # => nil Ruby 1.8.7 でも、ちゃんと動く。

メソッドの中から自身のメソッド名を知る方法

Ruby で今実行中のメソッド名を知る - 趣味的にっき Ruby 1.8 の時は、こんな感じだった。 RUBY_VERSION # => "1.8.7" class Object def current_method caller.first.scan(/`(.*)'/).to_s end end def foo current_method # => "foo" end def bar foo curre…