私はSome_other_method
中を呼んでいSome_method
ます。Some_method
名前をSome_other_method
文字列形式で渡したい。どうすればいいですか?パラメータとして疑問符の代わりに何を入れればよいですか?
def Some_method
...
Some_other_method (?)
end
私はSome_other_method
中を呼んでいSome_method
ます。Some_method
名前をSome_other_method
文字列形式で渡したい。どうすればいいですか?パラメータとして疑問符の代わりに何を入れればよいですか?
def Some_method
...
Some_other_method (?)
end
現在のメソッドの名前をSymbolとして返します。メソッドの外部で呼び出された場合、nilを返します。
例えば:
def say_your_name
puts __method__.to_s
end
to_s
文字列の代わりに記号に満足している場合は、省略できます。
そのためにはobject#methodを使用する必要があります。ドキュメントから:
指定されたメソッドを obj のレシーバーとして検索し、Method オブジェクトを返します (または NameError を発生させます)。Method オブジェクトは obj のオブジェクト インスタンスのクロージャとして機能するため、インスタンス変数と self の値は引き続き使用できます。
> user = User.first
=> #<User id: 1, email: "aslam@mapunity.in", created_at: "2011-05-24 07:17:51", updated_at: "2011-06-02 05:28:37", username: "admin">
> meth = user.method(:email)
=> #<Method: User(#<Module:0x9ceff3c>)#_email>
> meth.name
=> :email
> meth.name.to_s
=> "email"