gpt4 book ai didi

ruby - 如何以字符串格式获取方法的名称

转载 作者:数据小太阳 更新时间:2023-10-29 08:29:02 25 4
gpt4 key购买 nike

我正在 Some_method 中调用 Some_other_method。我想以字符串格式将名称 Some_method 传递给 Some_other_method。我该怎么做?我应该把什么而不是问号作为参数?

def Some_method
...
Some_other_method (?)
end

最佳答案

使用Kernel#__method__ :

Returns the name of the current method as a Symbol. If called outside of a method, it returns nil.

例如:

def say_your_name
puts __method__.to_s
end

如果您对符号而不是字符串感到满意,则可以省略 to_s

关于ruby - 如何以字符串格式获取方法的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6353984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com