RubyFlow The Ruby and Rails community linklog

×

The Ruby and Rails community linklog

Made a library? Written a blog post? Found a useful tutorial? Share it with the Ruby community here or just enjoy what everyone else has found!

Using block-reference instead of yield + block_given?

Comments

Hi Mehdi,

I don’t know if you are running rubycademy (there’s no way for visitors to get that info) but this particular card is misleading to me. Here are my comments on the card code (can’t comment directly on rubycademy):

The code with the block-reference is marked “better” because it’s a one-liner (I guess), but this example is contrived because it does nothing except delegate to #each. (So a even more “better” way would be to use alias or alias_method)

In the real world there may be code either before #each, certainly inside the block (before passing to the &block).

BTW, it is generally better to use yield + block_given? because there’s no &block parameter to make available in the current closure.

Best Regards

Post a comment

You can use basic HTML markup (e.g. <a>) or Markdown.

As you are not logged in, you will be
directed via GitHub to signup or sign in