[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Struggling with variable arguments to block

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')



1 Answer

Gavin Sinclair

10/25/2003 6:02:00 AM

0

On Saturday, October 25, 2003, 5:48:22 AM, Christoph wrote:

> 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 ...)


*throws hands up in air*

I grabbed the latest CVS, noted that Matz had fixed hash.c just
recently, tried to compile, and couldn't get past the second step.
"autoconf" worked, "./configure" didn't, with the most mundane error
message:

configure: error: sources are in ., but `cd .' does not work

Anyway, onto other things...

Gavin