在ruby中的函数定义中问号是什么意思?

时间:2022-12-14 22:26:52

I have got a function like this--

我有一个这样的函数

Function's name is seld.is_dl and it is accepting path parameter. My question is that what does this ? sign in the function definition indicate.

函数的名字叫seld。is_dl,它接受路径参数。我的问题是这是什么?函数定义中的符号表示。

def self.is_dl?(path)

  path = File.basename(path)

  if path =~ /setup.exe/i

    return false

  else

    return true

  end

end

I am java developer and I have seen "?" in case of If-ELSE block mainly, that is why I am not able to figure what does this mean?

我是java开发人员,我主要在If-ELSE块的情况下见过“?”,这就是为什么我不能理解这是什么意思?

1 个解决方案

#1


7  

? is a valid character in a method name.

吗?是方法名中的有效字符。

It is typically used to denote a method that returns true or false

它通常用于表示返回true或false的方法

For example:

例如:

Note: ! is also a valid character. It is typically used to denote a "destructive" method

注意:!也是一个有效字符。它通常用来表示一种“破坏性”的方法。


If you're feeling like going the extra mile, Ruby technically allows any string to be a method name. Odd ones need define_method() and send() calls, but formally there’s no restriction.

如果你想要额外的一英里,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

#1


7  

? is a valid character in a method name.

吗?是方法名中的有效字符。

It is typically used to denote a method that returns true or false

它通常用于表示返回true或false的方法

For example:

例如:

Note: ! is also a valid character. It is typically used to denote a "destructive" method

注意:!也是一个有效字符。它通常用来表示一种“破坏性”的方法。


If you're feeling like going the extra mile, Ruby technically allows any string to be a method name. Odd ones need define_method() and send() calls, but formally there’s no restriction.

如果你想要额外的一英里,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