extension question: hiding block proc

Discussion in 'Ruby' started by Dave Lee, Apr 12, 2005.

  1. Dave Lee

    Dave Lee Guest

    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
     
    Dave Lee, Apr 12, 2005
    #1
    1. Advertising

  2. 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
     
    Daniel Berger, Apr 12, 2005
    #2
    1. Advertising

  3. "Dave Lee" <> schrieb im Newsbeitrag
    news:...
    > 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
     
    Robert Klemme, Apr 12, 2005
    #3
  4. Dave Lee

    ts Guest

    >>>>> "R" == Robert Klemme <> 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
     
    ts, Apr 12, 2005
    #4
  5. Dave Lee

    Dave Lee Guest

    Daniel Berger <> 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
     
    Dave Lee, Apr 12, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lozzi
    Replies:
    3
    Views:
    1,951
    David Lozzi
    Jun 1, 2005
  2. NevilleDNZ
    Replies:
    9
    Views:
    454
    NevilleDNZ
    Aug 16, 2006
  3. Dominik Bathon
    Replies:
    2
    Views:
    92
    Dominik Bathon
    Feb 20, 2006
  4. Jacob Repp
    Replies:
    0
    Views:
    77
    Jacob Repp
    Feb 24, 2006
  5. Wolfgang Nádasi-Donner
    Replies:
    0
    Views:
    127
    Wolfgang Nádasi-Donner
    May 31, 2007
Loading...

Share This Page