Ruby Methods and Optional parameters

后端 未结 11 1613

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 10:03

    If you want to default the values in your options hash, you want to merge the defaults in your function. If you put the defaults in the default parameter itself, it'll be over-written:

    def my_info(name, options = {})
      options.reverse_merge!(age: 27, weight: 160, city: "New York")
    
      ...
    end
    

提交回复
热议问题