<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'><id>tag:blogger.com,1999:blog-3718956085911858962.post4052962837996628376..comments</id><updated>2008-07-31T04:53:51.223+05:30</updated><title type='text'>Comments on Electric Sheep Blog: Ruby blocks gotchas</title><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://blog.sidu.in/feeds/4052962837996628376/comments/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3718956085911858962/4052962837996628376/comments/default'/><link rel='alternate' type='text/html' href='http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html'/><author><name>Sidu</name><uri>http://www.blogger.com/profile/11938300811286150164</uri><email>noreply@blogger.com</email></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>11</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3718956085911858962.post-5970695189001747397</id><published>2008-07-31T04:53:00.000+05:30</published><updated>2008-07-31T04:53:00.000+05:30</updated><title type='text'>I really don't think it can get any simpler than t...</title><content type='html'>I really don&amp;#39;t think it can get any simpler than this:&lt;BR/&gt;&lt;BR/&gt;x = lambda do&lt;BR/&gt;  puts &amp;#39;hello&amp;#39;&lt;BR/&gt;  yield&lt;BR/&gt;end&lt;BR/&gt;&lt;BR/&gt;x.call { puts &amp;#39;world&amp;#39; }&lt;BR/&gt;&lt;BR/&gt;And the above example still doesn&amp;#39;t work with ruby 1.9.0 (2008-07-31 revision 18282) [x86_64-linux].&lt;BR/&gt;&lt;BR/&gt;hello&lt;BR/&gt;test.rb:3:in `block in &amp;lt;main&amp;gt;&amp;#39;: no block given (yield) (LocalJumpError)&lt;BR/&gt; from test.rb:6:in `call&amp;#39;&lt;BR/&gt; from test.rb:6:in `&amp;lt;main&amp;gt;&amp;#39;&lt;BR/&gt;&lt;BR/&gt;So as far as I can see, no, Ruby 1.9 most definitely does not (yet) allow me to IMPLICITLY pass a block to another block, unless we are going to argue that lambdas are some kind of special case.&lt;BR/&gt;&lt;BR/&gt;A new thing Ruby 1.9 can do is allow me to EXPLICITLY pass a block. This works in Ruby 1.9 (but didn&amp;#39;t work in 1.8)&lt;BR/&gt;&lt;BR/&gt;x = lambda do |&amp;amp;block|&lt;BR/&gt;  puts &amp;#39;hello&amp;#39;&lt;BR/&gt;  block.call&lt;BR/&gt;end&lt;BR/&gt;&lt;BR/&gt;x.call { puts &amp;#39;world&amp;#39; }&lt;BR/&gt;&lt;BR/&gt;Functionally, this is good enough. But the whole business with |&amp;amp;block| is ugly and inconsistent with the use of yield inside methods. Wouldn&amp;#39;t allowing the use of yield inside a lambda be nicer? Or am I missing something here?</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3718956085911858962/4052962837996628376/comments/default/5970695189001747397'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3718956085911858962/4052962837996628376/comments/default/5970695189001747397'/><link rel='alternate' type='text/html' href='http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html?showComment=1217460180000#c5970695189001747397' title=''/><author><name>Benjamin Kay</name><email>noreply@blogger.com</email></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html' ref='tag:blogger.com,1999:blog-3718956085911858962.post-4052962837996628376' source='http://www.blogger.com/feeds/3718956085911858962/posts/default/4052962837996628376' type='text/html'/></entry><entry><id>tag:blogger.com,1999:blog-3718956085911858962.post-437826823072719330</id><published>2008-06-30T00:38:00.000+05:30</published><updated>2008-06-30T00:38:00.000+05:30</updated><title type='text'>Kudos on tackling a tricky subject with this post....</title><content type='html'>Kudos on tackling a tricky subject with this post. I'd like to offer a couple of "clarifications" which hopefully will help people.&lt;BR/&gt;&lt;BR/&gt;The reason the implicit version is faster is because you are repeatedly constructing the Proc every time in the explicit version. &lt;BR/&gt;&lt;BR/&gt;If you never access the Proc directly, why add the overhead of constructing an object? This way, you only pay for what you need.&lt;BR/&gt;&lt;BR/&gt;I don't really see how this violates the "everything is an object" rule, since as soon as you try to access it, the block becomes an object. :)&lt;BR/&gt;&lt;BR/&gt;Also: the problem with passing blocks to blocks is not due to define_method, just blocks. A similar difference exists with default arguments. Method arguments and block arguments will operate the same way (I think) in Ruby 1.9.&lt;BR/&gt;&lt;BR/&gt;IMHO, the biggest inconsistency with blocks, especially post 1.9, is one you haven't alluded to here, which is that return behaves differently between Proc.new and lambda.&lt;BR/&gt;&lt;BR/&gt;See:&lt;BR/&gt;http://tinyurl.com/29a28d&lt;BR/&gt;&lt;BR/&gt;When returning from blocks, passed explicitly or implicitly, return takes you all the way out of the calling method. That is, they behave like Proc.new (which makes sense), not lambda.&lt;BR/&gt;&lt;BR/&gt;This is arguably necessary to support the POLS when dealing with inline blocks (say, with #each). You don't expect return to just pop you out of the block, you expect to return from the method. Not sure if that changes in 1.9?&lt;BR/&gt;&lt;BR/&gt;Anyway, thanks again for the interesting article!</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3718956085911858962/4052962837996628376/comments/default/437826823072719330'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3718956085911858962/4052962837996628376/comments/default/437826823072719330'/><link rel='alternate' type='text/html' href='http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html?showComment=1214766480000#c437826823072719330' title=''/><author><name>Dan Yoder</name><uri>http://rubywaves.com</uri><email>noreply@blogger.com</email></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html' ref='tag:blogger.com,1999:blog-3718956085911858962.post-4052962837996628376' source='http://www.blogger.com/feeds/3718956085911858962/posts/default/4052962837996628376' type='text/html'/></entry><entry><id>tag:blogger.com,1999:blog-3718956085911858962.post-7645699998986099324</id><published>2008-06-29T15:38:00.000+05:30</published><updated>2008-06-29T15:38:00.000+05:30</updated><title type='text'>Why are you handrolling your own dubious benchmark...</title><content type='html'>Why are you handrolling your own dubious benchmarking class?</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3718956085911858962/4052962837996628376/comments/default/7645699998986099324'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3718956085911858962/4052962837996628376/comments/default/7645699998986099324'/><link rel='alternate' type='text/html' href='http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html?showComment=1214734080000#c7645699998986099324' title=''/><author><name>Anonymous</name><email>noreply@blogger.com</email></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html' ref='tag:blogger.com,1999:blog-3718956085911858962.post-4052962837996628376' source='http://www.blogger.com/feeds/3718956085911858962/posts/default/4052962837996628376' type='text/html'/></entry><entry><id>tag:blogger.com,1999:blog-3718956085911858962.post-8070348817213009224</id><published>2008-03-15T22:50:00.000+05:30</published><updated>2008-03-15T22:50:00.000+05:30</updated><title type='text'>Looks like this post is still alive and people rea...</title><content type='html'>Looks like this post is still alive and people read it.. A post that clarifies a lot more things about blocks, closures, procs in ruby is here http://innig.net/software/ruby/closures-in-ruby.rb</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3718956085911858962/4052962837996628376/comments/default/8070348817213009224'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3718956085911858962/4052962837996628376/comments/default/8070348817213009224'/><link rel='alternate' type='text/html' href='http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html?showComment=1205601600000#c8070348817213009224' title=''/><author><name>Sudhindra Rao</name><uri>http://www.blogger.com/profile/07437146809844632919</uri><email>noreply@blogger.com</email></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html' ref='tag:blogger.com,1999:blog-3718956085911858962.post-4052962837996628376' source='http://www.blogger.com/feeds/3718956085911858962/posts/default/4052962837996628376' type='text/html'/></entry><entry><id>tag:blogger.com,1999:blog-3718956085911858962.post-7189681035663889221</id><published>2008-01-02T21:15:00.000+05:30</published><updated>2008-01-02T21:15:00.000+05:30</updated><title type='text'>I see four problems with blocks in Ruby 1.8:1. Can...</title><content type='html'>I see four problems with blocks in Ruby 1.8:&lt;BR/&gt;&lt;BR/&gt;1. Can't pass a block to a block, thus causing problems with define_method and similar.&lt;BR/&gt;&lt;BR/&gt;2. The differences between Proc.new, proc, lambda, and implicit blocks. I have a hard time keeping the differences straight, and try to use only lambda and implicit blocks.&lt;BR/&gt;&lt;BR/&gt;3. The performance difference between explicit and implicit block-passing.&lt;BR/&gt;&lt;BR/&gt;4. The only way to pass more than one block to a method is to pass (all but one of) them as explicit procs/lambdas.&lt;BR/&gt;&lt;BR/&gt;Of these, the first one is definitely fixed in Ruby 1.9, and I think the second one is as well. The third is a quirk of MRI, and it would be worth running some benchmarks in other Ruby implementations (JRuby, Rubinius, IronRuby...). The fourth comes up rarely enough that I have a hard time suggesting a more convenient syntax, even though it annoys me on occasion.&lt;BR/&gt;&lt;BR/&gt;In addition, an implicit block can be captured into a Proc object:&lt;BR/&gt;&lt;BR/&gt;def foo&lt;BR/&gt;  Proc.new&lt;BR/&gt;end&lt;BR/&gt;&lt;BR/&gt;bar = foo { |arg| puts "You called me with #{arg}" }&lt;BR/&gt;bar.call(22)&lt;BR/&gt;bar.call(99)&lt;BR/&gt;&lt;BR/&gt;I might argue that the implicit block and yield construct in Ruby is a mistake to begin with, but it improves the readability of iteration methods. That one case may or may not justify the complication of two parallel block-passing constructs, however.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3718956085911858962/4052962837996628376/comments/default/7189681035663889221'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3718956085911858962/4052962837996628376/comments/default/7189681035663889221'/><link rel='alternate' type='text/html' href='http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html?showComment=1199288700000#c7189681035663889221' title=''/><author><name>Gregory</name><uri>http://www.blogger.com/profile/01377458805247260071</uri><email>noreply@blogger.com</email></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html' ref='tag:blogger.com,1999:blog-3718956085911858962.post-4052962837996628376' source='http://www.blogger.com/feeds/3718956085911858962/posts/default/4052962837996628376' type='text/html'/></entry><entry><id>tag:blogger.com,1999:blog-3718956085911858962.post-8497619841698858327</id><published>2007-11-30T00:46:00.000+05:30</published><updated>2007-11-30T00:46:00.000+05:30</updated><title type='text'>You're right, I've been investigating something si...</title><content type='html'>You're right, I've been investigating something similar and I stumble upon the same caveats that you did. If someone knows some little known black magic here would be great.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3718956085911858962/4052962837996628376/comments/default/8497619841698858327'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3718956085911858962/4052962837996628376/comments/default/8497619841698858327'/><link rel='alternate' type='text/html' href='http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html?showComment=1196363760000#c8497619841698858327' title=''/><author><name>Fabio Akita, PMP</name><uri>http://www.blogger.com/profile/05539202931163964720</uri><email>noreply@blogger.com</email></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html' ref='tag:blogger.com,1999:blog-3718956085911858962.post-4052962837996628376' source='http://www.blogger.com/feeds/3718956085911858962/posts/default/4052962837996628376' type='text/html'/></entry><entry><id>tag:blogger.com,1999:blog-3718956085911858962.post-5128551844828490808</id><published>2007-11-27T15:35:00.000+05:30</published><updated>2007-11-27T15:35:00.000+05:30</updated><title type='text'>re: Blocks violate the 'everything is an object' r...</title><content type='html'>re: Blocks violate the 'everything is an object' rule in Ruby for performance reasons. They only become objects when bound to a variable.&lt;BR/&gt;&lt;BR/&gt;I don't think this is correct.  Two reasons: 1 - I have read that ruby blocks are not first class objects due to not wanting to expose the implementation to allow for future change.  2 - I'm an old Smalltalk'er.  Blocks are first class objects in Smalltalk and a Smalltalk VM of 15 years ago in most cases outperforms a Ruby interpreter of today; including dynamic block dispatches. enjoy!!!</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3718956085911858962/4052962837996628376/comments/default/5128551844828490808'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3718956085911858962/4052962837996628376/comments/default/5128551844828490808'/><link rel='alternate' type='text/html' href='http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html?showComment=1196157900000#c5128551844828490808' title=''/><author><name>Jon Hancock</name><uri>http://www.blogger.com/profile/08064528668252467611</uri><email>noreply@blogger.com</email></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html' ref='tag:blogger.com,1999:blog-3718956085911858962.post-4052962837996628376' source='http://www.blogger.com/feeds/3718956085911858962/posts/default/4052962837996628376' type='text/html'/></entry><entry><id>tag:blogger.com,1999:blog-3718956085911858962.post-924945239980222681</id><published>2007-11-26T23:50:00.000+05:30</published><updated>2007-11-26T23:50:00.000+05:30</updated><title type='text'>@anonymous #1 - you're right, it is clearer your w...</title><content type='html'>@anonymous #1 - you're right, it &lt;I&gt;is&lt;/I&gt; clearer your way. Thanks!&lt;BR/&gt;&lt;BR/&gt;@anonymous #2 - thanks, for the tip, that's worthy of an update to the post.&lt;BR/&gt;&lt;BR/&gt;@anonymous #3 - I didn't read it anywhere. What I &lt;I&gt;did&lt;/I&gt; read was 'pure OO' and 'everything is an object'. Nothing mentioned exceptions to these rules.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3718956085911858962/4052962837996628376/comments/default/924945239980222681'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3718956085911858962/4052962837996628376/comments/default/924945239980222681'/><link rel='alternate' type='text/html' href='http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html?showComment=1196101200000#c924945239980222681' title=''/><author><name>Sidu</name><uri>http://www.blogger.com/profile/11938300811286150164</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='17880039954856882313'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html' ref='tag:blogger.com,1999:blog-3718956085911858962.post-4052962837996628376' source='http://www.blogger.com/feeds/3718956085911858962/posts/default/4052962837996628376' type='text/html'/></entry><entry><id>tag:blogger.com,1999:blog-3718956085911858962.post-2205626857179995688</id><published>2007-11-26T22:08:00.000+05:30</published><updated>2007-11-26T22:08:00.000+05:30</updated><title type='text'>I really dont think anyone ever wrote anything abo...</title><content type='html'>I really dont think anyone ever wrote anything about blocks being objects. Where did you read that?</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3718956085911858962/4052962837996628376/comments/default/2205626857179995688'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3718956085911858962/4052962837996628376/comments/default/2205626857179995688'/><link rel='alternate' type='text/html' href='http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html?showComment=1196095080000#c2205626857179995688' title=''/><author><name>Anonymous</name><email>noreply@blogger.com</email></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html' ref='tag:blogger.com,1999:blog-3718956085911858962.post-4052962837996628376' source='http://www.blogger.com/feeds/3718956085911858962/posts/default/4052962837996628376' type='text/html'/></entry><entry><id>tag:blogger.com,1999:blog-3718956085911858962.post-4395543111197408517</id><published>2007-11-26T14:41:00.000+05:30</published><updated>2007-11-26T14:41:00.000+05:30</updated><title type='text'>Ruby 1.9 block will accept a block as a parameter....</title><content type='html'>Ruby 1.9 block will accept a block as a parameter.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3718956085911858962/4052962837996628376/comments/default/4395543111197408517'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3718956085911858962/4052962837996628376/comments/default/4395543111197408517'/><link rel='alternate' type='text/html' href='http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html?showComment=1196068260000#c4395543111197408517' title=''/><author><name>Anonymous</name><email>noreply@blogger.com</email></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html' ref='tag:blogger.com,1999:blog-3718956085911858962.post-4052962837996628376' source='http://www.blogger.com/feeds/3718956085911858962/posts/default/4052962837996628376' type='text/html'/></entry><entry><id>tag:blogger.com,1999:blog-3718956085911858962.post-5895151754584194294</id><published>2007-11-25T21:59:00.000+05:30</published><updated>2007-11-25T21:59:00.000+05:30</updated><title type='text'>def foo(*args) blk = args.delete_at(-1) # We know ...</title><content type='html'>def foo(*args)&lt;BR/&gt; blk = args.delete_at(-1) # We know that the last argument &lt;BR/&gt;                          # is the bound block&lt;BR/&gt; blk.call(args.join(' '))  &lt;BR/&gt;end&lt;BR/&gt;&lt;BR/&gt;looks unnecessary clumsy. Why not:&lt;BR/&gt;&lt;BR/&gt;def foo(blk, *args)&lt;BR/&gt;   blk.call(args.join(' '))&lt;BR/&gt;end</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3718956085911858962/4052962837996628376/comments/default/5895151754584194294'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3718956085911858962/4052962837996628376/comments/default/5895151754584194294'/><link rel='alternate' type='text/html' href='http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html?showComment=1196008140000#c5895151754584194294' title=''/><author><name>Anonymous</name><email>noreply@blogger.com</email></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html' ref='tag:blogger.com,1999:blog-3718956085911858962.post-4052962837996628376' source='http://www.blogger.com/feeds/3718956085911858962/posts/default/4052962837996628376' type='text/html'/></entry></feed>