From: Simon Strandgaard Date: 2005-07-19T21:58:53+09:00 Subject: Re: [RCR] nil for unassigned keys On 7/19/05, Yukihiro Matsumoto wrote: > on Tue, 19 Jul 2005 20:40:47 +0900, Simon Strandgaard writes: > > |Sometimes I find myself writing :key=>true, > |it would be nice if one didn't had to do assignment, > |so its value instead defaulted to nil. > | > |def test(hash={}) > | p hash > |end > | > |test('x', 'y'=>2, 'z') # {"x"=>nil, "y"=>2, "z"=>nil} > > In that case, how can we distinguish a hash key without value and > ordinal mandatory argument? Maybe I don't understand the mandatory argument thing? But can't you just count them somehow? def test(arg1, arg2, hash={}) puts "#{arg1.inspect} #{arg2.inspect} #{hash.inspect}" end # mandatory argument? test(1, 2) # 1 2 {} # hash key without value test(1, 2, 42) # 1 2 {42=>nil} test(1, 2, 3, 4) # 1 2 {3=>nil, 4=>nil} test(1, 2, 3, 4=>5) # 1 2 {3=>nil, 4=>5} -- Simon Strandgaard