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

M

matt

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?
 
G

George Ogata

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top