From: jweirich@... Date: 2001-06-16T03:11:54+09:00 Subject: [ruby-talk:16502] Playing with Ruby Syntax (was: Initial thoughts about Ruby From a Smalltalk Programmer) Michael> Hi Everyone, I have to say I'm utterly fascinated by Ruby Michael> and I'm really getting in to the spirit of it. Hi Michael. Glad you've been enjoying Ruby. Michael> Procs, Blocks - a confusion. [...] One of the most Michael> comment extensions to Smalltalk that you might find is Michael> this: Michael> detect: detectBlock ifFound: foundBlock ifNone: noneBlock Yes, I agree that I have occasionally wished for the ability to easily pass multiple blocks to a method. However, I have found that this limitation is not too onerous. Here's some ideas ... Add the following to Kernel and NilClass ... module Kernel def if_found yield self self end def if_not_found self end end class NilClass def if_found nil end def if_not_found yield self nil end end Now you can write ... [1,2,3].detect { |e| e > 2 }.if_found { |e| puts "Found #{e}" }.if_not_found { puts "Not Found" } There's some minor differences between the above and the Smalltalk version. (1) there is some confusion between detecting "nil" and not finding a match, and (2) I suspect the Smalltalk version returns the value of the found/not_found block, the above returns the found item. Although hacking Kernel and NilClass is kinda fun, I probably wouldn't do it in real life. I think the following code (using straight Ruby out of the box) is still the most readable ... if item = [1,2,3].detect {|e| e>2} puts "Found #{item}" else puts "Not Found" end -- -- Jim Weirich