procs/blocks - blocks with procs, blocks with blocks?

Discussion in 'Ruby' started by matt, Aug 5, 2004.

  1. matt

    matt Guest

    Well, maybe not blocks with blocks but blocks with yield? although
    right now, I only have a fix for procs with blocks and not blocks with
    blocks via blocks with yield when a proc block is not in stock...

    class Proc
    alias __proc_block_call call
    alias __proc_block_indexer []

    def call(*args, &block)
    __proc_block_call(*(block.nil? ? args : args << block))
    end

    def [](*args, &block)
    __proc_block_indexer(*(block.nil? ? args : args << block))
    end
    end

    -----
    usage

    prc = Proc.new {|arg, proc_block|
    p arg
    proc_block[arg]
    }

    prc.call("Foo") {|*what|
    puts "Got #{what.length} whats -- #{what.inspect}"
    }

    produces:

    "Foo"
    Got 1 whats -- ["Foo"]


    - also - it looks like the indexer function cannot take a block (parse
    error) - why?
    matt, Aug 5, 2004
    #1
    1. Advertising

  2. matt

    George Ogata Guest

    Matt Maycock <> writes:

    [snip code]

    If I understand what you're trying to do, the latest (CVS) ruby allows
    blocks to take blocks.

    irb(main):001:0> lammy = lambda{|&b| b.call}
    => #<Proc:0x402145f8@(irb):1>
    irb(main):002:0> lammy.call{10}
    => 10
    irb(main):003:0>

    > I still don't know why I can't use a block with the normal use of
    > []... is that going to ever change?
    >
    > I know I can do obj.send:)[], *args) or obj.[](*args) -- but I think
    > we all know using obj[*args] is best! :)


    I don't know if it will change or not. I'm somewhat impartial to it,
    though. I always thought the rationale for Proc#[] was not for saving
    keystrokes, nor for kludging a #() operator, but rather to allow a
    compact representation of a procedurally generated collection. E.g.,
    instead of explicitly using an array of the numbers [0, 2, 4, ...,
    something_big], you can just use lambda{|x| 2*x}. These sorts of uses
    generally have no business taking blocks.

    I could of course be wrong, though.
    George Ogata, Aug 6, 2004
    #2
    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. Paul R
    Replies:
    1
    Views:
    371
    Eric Veltman
    Dec 2, 2003
  2. =?Utf-8?B?QUtKVFI=?=

    Multiple stored procs for dataadapter

    =?Utf-8?B?QUtKVFI=?=, Dec 21, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    383
    Karl Seguin
    Dec 21, 2004
  3. Mehr, Assaph (Assaph)

    Object#send and blocks/procs

    Mehr, Assaph (Assaph), Sep 27, 2004, in forum: Ruby
    Replies:
    2
    Views:
    126
    George Ogata
    Sep 27, 2004
  4. Eli Bendersky
    Replies:
    2
    Views:
    116
    Eli Bendersky
    Apr 16, 2006
  5. Eli Bendersky
    Replies:
    0
    Views:
    121
    Eli Bendersky
    Apr 18, 2006
Loading...

Share This Page