Ruby Methods and Optional parameters

后端 未结 11 1611

I\'m playing with Ruby on Rails and I\'m trying to create a method with optional parameters. Apparently there are many ways to do it. I trying naming the optional parameters

11条回答
  •  星月不相逢
    2020-12-13 09:47

    For the default values in your hash you should use this

    def some_method(required_1, required_2, options={})
      defaults = {
        :option_1 => "option 1 default",
        :option_2 => "option 2 default",
        :option_3 => "option 3 default",
        :option_4 => "option 4 default"
      }
      options = defaults.merge(options)
    
      # Do something awesome!
    end
    

提交回复
热议问题