Christoph R.
10/24/2003 7:48:00 PM
dblack wrote:
...
> I confess to confusion about the new arg/param semantics.
> For example:
Me too - but I start seeing some light:-) Here is what
I got playing with this stuff (I guess you need a recent
cvs version ...)
---
module Enumerable
def each_one
# turn an old style Hash#each
# into a new style Hash#each
each {|*e| yield *[e] }
end
def each_many
# reverse of the above
each {|e| yield *e }
end
def sum
result = 0
each { |e| result += yield e }
result
end
def sum_one
result = 0
each_one { |e| result += yield e }
result
end
def sum_many
result = 0
each_many { |e| result += yield *e }
result
end
end
class << n = Object.new
# new style Hash::each
include Enumerable
def each
yield [1,2]
yield [3,4]
end
end
class << o = Object.new
# old style Hash::each
include Enumerable
def each
yield 1,2
yield 3,4
end
end
n.sum {|a,b| b } # fine
o.sum_one {|a,b| b } # fine
o.sum {|a,b| b } # warning
o.sum_many {|a,b| b } # warning
# n.sum_one {|a,b| b } # Error ...
n.sum_many {|a,b| b } # warning
---
>
> irb(main):005:0> *a = 1,2
> => [1, 2]
> irb(main):006:0> *a = [1,2]
> => [1, 2]
> irb(main):007:0> *a = [[1,2]]
> => [[[1, 2]]]
>
> The one that doesn't seem to occur at all is [[1,2]] -- but
> that's what the new Hash#each will give... but aren't block
> semantics modeled on assignment semantics? (I'm also still
> puzzled about Proc.new {} and proc {} being different from
> each other, but I'll settle for understanding |*a| for now :-)
I guess I'll settle for understanding the wisdom behind the recent
enum.inject {|a,b| break "me" if .. }
enum.collect {|a| break "me" if .. }
change:-)
------------
/Christoph
Please send off list mail to
'my_mail@gmy.net'.gsub(/y/,'x')