No named parameters in Ruby?

前端 未结 4 1217
日久生厌
日久生厌 2020-12-13 09:20

This is so simple that I can\'t believe it caught me.

def meth(id, options = \"options\", scope = \"scope\")
  puts options
end

meth(1, scope = \"meh\")

-&         


        
相关标签:
4条回答
  • 2020-12-13 09:48

    What's actually happening:

    # Assign a value of "meh" to scope, which is OUTSIDE meth and equivalent to
    #   scope = "meth"
    #   meth(1, scope)
    meth(1, scope = "meh")
    
    # Ruby takes the return value of assignment to scope, which is "meh"
    # If you were to run `puts scope` at this point you would get "meh"
    meth(1, "meh")
    
    # id = 1, options = "meh", scope = "scope"
    puts options
    
    # => "meh"
    

    There is no support* for named parameters (see below for 2.0 update). What you're seeing is just the result of assigning "meh" to scope being passed as the options value in meth. The value of that assignment, of course, is "meh".

    There are several ways of doing it:

    def meth(id, opts = {})
      # Method 1
      options = opts[:options] || "options"
      scope   = opts[:scope]   || "scope"
    
      # Method 2
      opts = { :options => "options", :scope => "scope" }.merge(opts)
    
      # Method 3, for setting instance variables
      opts.each do |key, value|
        instance_variable_set "@#{key}", value
        # or, if you have setter methods
        send "#{key}=", value
      end
      @options ||= "options"
      @scope   ||= "scope"
    end
    
    # Then you can call it with either of these:
    meth 1, :scope => "meh"
    meth 1, scope: "meh"
    

    And so on. They're all workarounds, though, for the lack of named parameters.


    Edit (February 15, 2013):

    * Well, at least until the upcoming Ruby 2.0, which supports keyword arguments! As of this writing it's on release candidate 2, the last before the official release. Although you'll need to know the methods above to work with 1.8.7, 1.9.3, etc., those able to work with newer versions now have the following option:

    def meth(id, options: "options", scope: "scope")
      puts options
    end
    
    meth 1, scope: "meh"
    # => "options"
    
    0 讨论(0)
  • 2020-12-13 09:49

    Although named parameters are not supported by the Ruby language, you can simulate them by passing your function arguments through a hash. For example:

    def meth(id, parameters = {})
      options = parameters["options"] || "options"
      scope = parameters["scope"] || "scope"
    
      puts options
    end
    

    Which can be used as follows:

    meth(1, scope: "meh")
    

    Your existing code simply assigns a varaible, then passes that variable to your function. For more information see: http://deepfall.blogspot.com/2008/08/named-parameters-in-ruby.html.

    0 讨论(0)
  • 2020-12-13 09:50

    Ruby doesn't have named parameters.

    The example method definition has parameters with default values.

    The call site example assigns a value to a caller's-scope local variable named scope and then passes its value (meh) to the options parameter.

    0 讨论(0)
  • 2020-12-13 09:51

    I think 2 things are happening here:

    1. You are defining a parameter on the method named 'scope' with a default value of "scope"
    2. When you call the method, you are assigning the value "meh" to a new local variable named 'scope', which is unrelated to the parameter name on the method you are calling.
    0 讨论(0)
提交回复
热议问题