[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

extension question: hiding block proc

Dave Lee

4/12/2005 1:19:00 PM

Hi all,

I have an each method where the value being yielded is an instance of
an Array subclass, created with rb_class_new_instance(argc, argv,
myClass). Clearly, the each method is being passed a block. My
problem is that my Array subclass basically calls super(size), which
sees the block given to my each method, and yields to it. This means
my each method is getting called an additional n times, where n is the
size of my array subclass instance. How can I hide, mask, or
temporarily remove the given block when I call the array constructor,
but still have it in place when my each method calls yield? I've
looked at the ruby source, but can't see anything obvious.

Thanks,
Dave


4 Answers

Berger, Daniel

4/12/2005 1:34:00 PM

0

Dave Lee wrote:
> Hi all,
>
> I have an each method where the value being yielded is an instance of
> an Array subclass, created with rb_class_new_instance(argc, argv,
> myClass). Clearly, the each method is being passed a block. My
> problem is that my Array subclass basically calls super(size), which
> sees the block given to my each method, and yields to it. This means
> my each method is getting called an additional n times, where n is the
> size of my array subclass instance. How can I hide, mask, or
> temporarily remove the given block when I call the array constructor,
> but still have it in place when my each method calls yield? I've
> looked at the ruby source, but can't see anything obvious.
>
> Thanks,
> Dave

I don't know if this will solve your problem or not, but try
rb_funcall(rb_cArray,rb_intern("new"),0,0) to create the Array instance
instead.

Regards,

Dan



Robert Klemme

4/12/2005 2:48:00 PM

0


"Dave Lee" <dave.lee.wilson@gmail.com> schrieb im Newsbeitrag
news:bedf97cd050412061816c882d2@mail.gmail.com...
> Hi all,
>
> I have an each method where the value being yielded is an instance of
> an Array subclass, created with rb_class_new_instance(argc, argv,
> myClass). Clearly, the each method is being passed a block. My
> problem is that my Array subclass basically calls super(size), which
> sees the block given to my each method, and yields to it. This means
> my each method is getting called an additional n times, where n is the
> size of my array subclass instance. How can I hide, mask, or
> temporarily remove the given block when I call the array constructor,
> but still have it in place when my each method calls yield? I've
> looked at the ruby source, but can't see anything obvious.

I find it difficult to translate what you wrote correctly. Do you do the
equivalent of this?

class ASub < Array
def initialize(size)
super(size)
end
end

class AnotherClass
def each
yield ASub.new(10)
end
end

I don't see how that could call your each method additional times. If at
all the block is invoked several times from Array#initialize. Maybe you
better post your original code...

Kind regards

robert

ts

4/12/2005 2:57:00 PM

0

>>>>> "R" == Robert Klemme <bob.news@gmx.net> writes:

R> I find it difficult to translate what you wrote correctly. Do you do the
R> equivalent of this?

no,

R> I don't see how that could call your each method additional times.

because rb_class_new_instance() don't call rb_funcall2() (what do your
ASub.new) and has access to the block given to the method.


Guy Decoux









Dave Lee

4/12/2005 3:41:00 PM

0

Daniel Berger <djberge@qwest.com> wrote:
> I don't know if this will solve your problem or not, but try
> rb_funcall(rb_cArray,rb_intern("new"),0,0) to create the Array instance
> instead.

thanks, this is pretty much what I needed to do. basically, I replaced

rb_class_new_instance(argc, argv, klass);

with

rb_funcall2(klass, rb_intern("new"), argc, argv);

Dave