Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > extension question: hiding block proc

Reply
Thread Tools

extension question: hiding block proc

 
 
Dave Lee
Guest
Posts: n/a
 
      04-12-2005
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


 
Reply With Quote
 
 
 
 
Daniel Berger
Guest
Posts: n/a
 
      04-12-2005
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



 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      04-12-2005

"Dave Lee" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> 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

 
Reply With Quote
 
ts
Guest
Posts: n/a
 
      04-12-2005
>>>>> "R" == Robert Klemme <(E-Mail Removed)> 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









 
Reply With Quote
 
Dave Lee
Guest
Posts: n/a
 
      04-12-2005
Daniel Berger <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"break" from a block and a proc object used in block position Wolfgang Nádasi-Donner Ruby 0 05-31-2007 06:05 PM
Question: is it possible for an extension to convert a block to a proc and execute it later Jacob Repp Ruby 0 02-24-2006 07:33 AM
Passing a proc as block to a method from a C extension Dominik Bathon Ruby 2 02-20-2006 12:48 AM
Convert VB.NET to TSQL PROC & Reference a Proc from another Proc David Lozzi ASP .Net 3 06-01-2005 06:35 PM
What is the diff btwn 'sho proc' and 'sho proc cpu' William J King Cisco 1 12-18-2003 11:50 PM



Advertisments