Optional arguments with default value in Ruby

后端 未结 3 1820
既然无缘
既然无缘 2020-12-10 19:13

I would like to create a function that has optional arguments with default values

def my_function(a = nil, b=nil, c=500)

end

and call the

3条回答
  •  感情败类
    2020-12-10 20:05

    You cannot do that (or something similar) in Ruby < 2.0. The best you could do is:

    def my_function(h = {})
      h[:c] ||= 500
      # Use h[:a], h[:b], h[:c]
      ...
    end
    
    my_function(b: 100)
    

提交回复
热议问题