参数为hash
def test(args)
args
end
test(a:1, b:2)
#=> {:a=>1, :b=>2}
普通参数和hash混合。最好把hash参数放在最后
def test(a, b, args)
p a
p b
p args
end
test(1, 2, a:1, b:2)
# 1
# 2
# {:a=>1, :b=>2}
# => {:a=>1, :b=>2}
将参数收集在数组中
def test(*args)
args
end
test(1, 2, 3)
#=> [1, 2, 3]
键为符号的参数
def test(a, b, args)
p a
p b
p args[:c]
end
test(1, 2, c: 2)
# 1
# 2
# 2
# => 2
键为字符串的参数
def test(a, b, args)
p a
p b
p args["c"]
end
test(1, 2, "c" => 2)
# 1
# 2
# 2
# => 2
以下两种写法等同
login(:name => 'bill', :password => "123")
login(name: 'bill', password: "123")
以下两种写法不同
login(:name => 'bill', :password => "123")
login("name": 'bill', "password": "123")