From: Gennady Date: 2003-04-18T02:31:19+09:00 Subject: Re: Newbie proc question ----- Original Message ----- From: "Phil Tomson" Newsgroups: comp.lang.ruby To: "ruby-talk ML" Sent: Wednesday, April 16, 2003 11:59 PM Subject: Re: Newbie proc question ... > > The other thing to add here is that Ruby's block mechanism is quite > elegant and unique - it essentially allows you to create domain specific I would not call it unique -- as far as I understand, it came from Smaltalk, where this technique is use quite extensively. Another block usage worth mentioning is that the passed code may be executed in a controlled enfironment. Say, before executing the code some resources may be allocated (like, for example, locks) and released after the code is finished. It can be arranged so that deallocation takes place even if the code raises exception: def method_accepting_block obtain_lock begin yield if block_given? ensure release_lock end end (or def method_accepting_block(&block) obtain_lock begin block.call unless block.nil? ensure release_lock end end ) A great example of this approach is method File.open(). If a block is given, File.open opens the file, executes the block making the file handle available to it, and closes the file when the block terminates. > languages that look syntactically very natural. (RHDL is an example of > this) > > Phil > > Gennady.