小编Jon*_*ard的帖子

字符串中的引用方法名称

人们可以这样做:

s="puts"
send(s,"Hello World!")
# => Hello World!
Run Code Online (Sandbox Code Playgroud)

如何检查是否s是一种方法?如果它是:

s="nomethod"
Run Code Online (Sandbox Code Playgroud)

send(s,arg) 将失败.

这很酷:

ismethod? s

PS我正在从PHP移植一些东西,它依赖于:

if (function_exists($s)) 
    ...
Run Code Online (Sandbox Code Playgroud)

如果只有Ruby有这么简单的东西.

ruby methods

2
推荐指数
1
解决办法
75
查看次数

标签 统计

methods ×1

ruby ×1