Sahil
Sahil

Reputation: 9488

What does question mark mean in the definition of function in ruby?

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.

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?

Upvotes: 2

Views: 623

Answers (1)

maček
maček

Reputation: 77806

? is a valid character in a method name.

It is typically used to denote a method that returns true or 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.

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

Upvotes: 7

Related Questions