“Kent Dahl” kentda@stud.ntnu.no schrieb im Newsbeitrag
news:3E56057A.77E22E0C@stud.ntnu.no…
[snip]
class Entry < Array
end
class MyHash
def initialize
@entries =
end
def
# strange hashing magic
end
def insert(key, value)
e = Entry.new
e << key
e << value
@entries << e
end
def each
@entries.each{|e| yield e}
end
end
m = MyHash.new
m.insert( “a”, “Alfa” )
m.insert( “b”, “Beta” )
m.each{|e| p e.type } # I care about entry metadata
m.each{|k,v| p k.type, v.type } # I don’t, gimme the content!
[snip]
Your example was about what happens to a single yield argument (“yield e”)
but as I see it I was talking about the different behavior of yield with
multiple values. I see an asymmetry between these cases where my block has
too few parameters:
irb(main):067:0* def foo2() yield 1,2; end
nil
irb(main):068:0> foo2{|a| p a}
[1, 2]
nil
irb(main):069:0> foo2{|a| p a}
[1, 2]
nil
irb(main):070:0> foo2{|a,| p a}
1
nil
irb(main):071:0>
irb(main):072:0 def foo3() yield 1,2,3; end
nil
irb(main):073:0> foo3{|a,b| p a, b}
1
2
nil
irb(main):074:0>
It’s the question whether line 68 should behave like 69 or 70. I’d prefer
70 because that is IMHO more consistent with line 73. Read “too few
parameters => distribute arguments over parameters and forget the rest”.
I’d prefer to write *a if I want to get all parameters than to write a, to
make sure I get only the first.
I wouldn’t mandate a language change though, since I guess this would break
too much existing code. It is just that this asymmetry confused me. But I
think, I got it now. ![]()
But I guess it’s a lot a matter of taste.
Aye, and like fine wine, its an aquired one at that
Yup. ![]()
Disclaimer: I hope the above makes somewhat sense and isn’t too wrong in
parts. I haven’t slept too well so my head is everywhere.
Thanks again for your time.
Regards
robert