Named parameters in Ruby 2

后端 未结 8 1752
闹比i
闹比i 2020-12-13 13:28

I don\'t understand completely how named parameters in Ruby 2.0 work.

def test(var1, var2, var3)
  puts \"#{var1} #{var2} #{var3}\"
end

test(var3:\"var3-ne         


        
8条回答
  •  南方客
    南方客 (楼主)
    2020-12-13 14:12

    Firstly, the last example you posted is misleading. I totally disagree that the behavior is similar to the one before. The last example passes the argument hash in as the first optional parameter which is a different thing!

    If you do not want to have a default value, you can just use nil.

    If you want to read a good writeup, see "Ruby 2 Keyword Arguments".

提交回复
热议问题