From: Joel VanderWerf Date: 2002-01-01T03:27:20+09:00 Subject: [ruby-talk:29879] Re: Proc.class vs yield David Alan Black wrote: > > Hi -- > > On Tue, 1 Jan 2002, Joel VanderWerf wrote: > > > David Alan Black wrote: > > > > > Hmmm... I wonder whether part of what you're not liking is the fact > > > that the {} operator is overloaded: it's a block constructor and also > > > a hash constructor. So there needs to be a way to deal with things > > > like: > > > > > > some_method( { "hi" }, { "one" => 1 }, { |x| puts x }) > > > > There is still some room to overload {} a bit more. The syntax > > > > some_method {|x| x+1} {|y| y-1} > > > > has no meaning currently--it's a parse error. Could this syntax be given > > a meaning so that > > > > def some_method(&b1, &b2) > > end > > > > allowed the method to use both blocks? > > What would yield do? Since you're explicitly converting each block to a proc, you don't need yield in this case. Maybe yield would look for a third, unobjectified, block? > > I'm not sure I'd propose this for RCR, since I haven't seen a use for it > > that couldn't be handled some other way. But still it would be kinda > > nice. It would allow you to write control structures a la Tcl: > > That would be a mighty big RC :-) > > > while {} { > > > > } > > > > I don't know Tcl. What does that structure give you? In Tcl, { ... } denotes a string without interpolation, like '...' in Ruby. So if you want to define a C-style for-loop in Ruby, you could, but it would look like: for {$x = 0} {$x < 100} {$x += 1} { puts $x } One problem: to use a local variable, you'd still have to reference it above the for-loop, or else the references in each of the blocks would be unrelated. Anyway, I still don't see a desperate need for this. When I really need to pass several blocks, I just use the auxilliary object trick with instance_eval. That has the advantage of associating a name with each block, so you don't have to remember the order. -- Joel VanderWerf California PATH, UC Berkeley mailto:vjoel@path.berkeley.edu Ph. (510) 231-9446 http://www.path.berkeley.edu FAX (510) 231-9512