我有这样的功能 -
函数名称为seld.is_dl,它接受路径参数.我的问题是这个?函数定义中的符号表示什么.
def self.is_dl?(path)
path = File.basename(path)
if path =~ /setup.exe/i
return false
else
return true
end
end
Run Code Online (Sandbox Code Playgroud)
我是java开发人员,我看过"?" 在主要是If-ELSE块的情况下,这就是为什么我无法确定这是什么意思?
? 是方法名称中的有效字符.
它通常用于表示返回true或的方法false
例如:
注意:!也是有效字符.它通常用于表示"破坏性"方法
如果你想加倍努力,Ruby在技术上允许任何字符串作为方法名称.奇怪的需要define_method()和send()打电话,但正式没有限制.
module Hello
class << self
define_method "this is my method :)" do |foo|
puts "you gave my method #{foo}"
end
define_method "this isn't your method :(, sorry" do
puts "sorry, not your method, bro"
end
end
end
Hello.send("this is my method :)", "candy")
#=> you gave my method candy
Hello.send("this isn't your method :(, sorry")
#=> sorry, not your method, bro
Run Code Online (Sandbox Code Playgroud)