How does this ruby ​​function return anything?

I am working with legacy ruby ​​code and I don't understand how it works. Parameter:

    def determine_caller(host)
      if host["name"] =~ /special/
        method(:call_special)
      else
        method(:call_normal)
      end
    end

      

later the method is called:

caller = determine_caller(host_name)

      

Ultimately, the program uses the caller as a variable because it runs a method in another class. call_special

and call_normal

are methods later in the program.

I believe this is the definition of a method to send to another method, so it can later call the method as needed (either call_special

or call_normal

), but I don't understand how it determin_caller

returns anything. I thought the keyword is being method

called by the method itself, but that cannot be right, at least as I understand the program I am looking at.

+3


source to share


2 answers


The simple answer, without going into the ruby ​​object model or whatever, is that the last statement in the method always returns. So if the caller name is special and not return what is ever returned when the "method" is called with these parameters



+2


source


The method Object#method

returns Method

. He doesn't say anything. (If that were the case, Method

it would be an incredibly awful name for what he did.)



BTW: Method

not a keyword, determine_caller

not a function. These are both methods.

+1


source







All Articles