<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Understanding Ruby blocks, Procs and methods</title>
	<link>http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/</link>
	<description>Eli Bendersky's personal website</description>
	<pubDate>Wed, 20 Aug 2008 16:04:26 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2</generator>

	<item>
		<title>By: Brian</title>
		<link>http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/#comment-122376</link>
		<author>Brian</author>
		<pubDate>Thu, 12 Jun 2008 15:32:26 +0000</pubDate>
		<guid>http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/#comment-122376</guid>
		<description>Great article. Really cleared up a lot of my understanding about this topic!</description>
		<content:encoded><![CDATA[<p>Great article. Really cleared up a lot of my understanding about this topic!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andriy</title>
		<link>http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/#comment-112406</link>
		<author>Andriy</author>
		<pubDate>Tue, 26 Feb 2008 01:39:14 +0000</pubDate>
		<guid>http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/#comment-112406</guid>
		<description>Great explanation. Finally i understand "how" yield works.</description>
		<content:encoded><![CDATA[<p>Great explanation. Finally i understand &#8220;how&#8221; yield works.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Deepak Gole</title>
		<link>http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/#comment-98813</link>
		<author>Deepak Gole</author>
		<pubDate>Tue, 15 Jan 2008 07:28:30 +0000</pubDate>
		<guid>http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/#comment-98813</guid>
		<description>Hello If I want pass arguments to method (&#38;:center), then how to send it
class Symbol
    
    # A generalized conversion of a method name
    # to a proc that runs this method.
    #
    def to_proc
        lambda {&#124;x, *args&#124; x.send(self, *args)}
    end
    
end

# Viola !
words = %w(Jane, aara, multiko)
upcase_words = words.map(&#38;:center)</description>
		<content:encoded><![CDATA[<p>Hello If I want pass arguments to method (&amp;:center), then how to send it<br />
class Symbol</p>
<p>    # A generalized conversion of a method name<br />
    # to a proc that runs this method.<br />
    #<br />
    def to_proc<br />
        lambda {|x, *args| x.send(self, *args)}<br />
    end</p>
<p>end</p>
<p># Viola !<br />
words = %w(Jane, aara, multiko)<br />
upcase_words = words.map(&amp;:center)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shining</title>
		<link>http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/#comment-94881</link>
		<author>shining</author>
		<pubDate>Fri, 28 Dec 2007 14:36:12 +0000</pubDate>
		<guid>http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/#comment-94881</guid>
		<description>Nice and concise!</description>
		<content:encoded><![CDATA[<p>Nice and concise!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cks2k2</title>
		<link>http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/#comment-91385</link>
		<author>cks2k2</author>
		<pubDate>Mon, 10 Dec 2007 06:10:37 +0000</pubDate>
		<guid>http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/#comment-91385</guid>
		<description># this doesn't (ArgumentError), because &#38;f 
# isn't really an argument - it's only there 
# to convert a block
contrived(25, lambda {&#124;x&#124; puts x})

I tried this on 1.8.6-25 but getting an ArgumentError(2 for 1).
Or am I the only getting this error?
I've tried contrived( 25, Proc.new {&#124;x&#124; puts x}) but it's still not working.</description>
		<content:encoded><![CDATA[<p># this doesn&#8217;t (ArgumentError), because &amp;f<br />
# isn&#8217;t really an argument - it&#8217;s only there<br />
# to convert a block<br />
contrived(25, lambda {|x| puts x})</p>
<p>I tried this on 1.8.6-25 but getting an ArgumentError(2 for 1).<br />
Or am I the only getting this error?<br />
I&#8217;ve tried contrived( 25, Proc.new {|x| puts x}) but it&#8217;s still not working.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: elliottcable</title>
		<link>http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/#comment-89799</link>
		<author>elliottcable</author>
		<pubDate>Tue, 27 Nov 2007 21:44:15 +0000</pubDate>
		<guid>http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/#comment-89799</guid>
		<description>&lt;blockquote&gt;In addition to being way cooler a whopping two characters shorter&lt;/blockquote&gt;

Or, if you're really awesomely cool, you could do this instead:
&lt;code&gt;module Kernel
   alias λ proc # (U+03BB)
end

padd = λ {&#124;x, y&#124; puts x + y}
padd.call&lt;/code&gt;</description>
		<content:encoded><![CDATA[<blockquote><p>In addition to being way cooler a whopping two characters shorter</p></blockquote>
<p>Or, if you&#8217;re really awesomely cool, you could do this instead:<br />
<code>module Kernel<br />
   alias λ proc # (U+03BB)<br />
end</p>
<p>padd = λ {|x, y| puts x + y}<br />
padd.call</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Coffee_fan</title>
		<link>http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/#comment-82541</link>
		<author>Coffee_fan</author>
		<pubDate>Sat, 03 Nov 2007 02:17:40 +0000</pubDate>
		<guid>http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/#comment-82541</guid>
		<description>Eli, this is a very nice article. On a side-note, I also found quite enlightening  that the control directives 'next', 'break' do work in the context of codeblocks in an intuitive way. As such it is possible to replace most loop iterations by an iterator with a codeblock with little or no modification to the control logic.</description>
		<content:encoded><![CDATA[<p>Eli, this is a very nice article. On a side-note, I also found quite enlightening  that the control directives &#8216;next&#8217;, &#8216;break&#8217; do work in the context of codeblocks in an intuitive way. As such it is possible to replace most loop iterations by an iterator with a codeblock with little or no modification to the control logic.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Leet Geezer</title>
		<link>http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/#comment-51506</link>
		<author>Leet Geezer</author>
		<pubDate>Sun, 13 May 2007 08:15:17 +0000</pubDate>
		<guid>http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/#comment-51506</guid>
		<description>Thanks for great article. Clarified alot.</description>
		<content:encoded><![CDATA[<p>Thanks for great article. Clarified alot.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tarmo</title>
		<link>http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/#comment-45702</link>
		<author>Tarmo</author>
		<pubDate>Mon, 02 Apr 2007 23:27:47 +0000</pubDate>
		<guid>http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/#comment-45702</guid>
		<description>kybernetikos, the reason neither of your examples could work is that ruby has to be able to differentiate between methods and local variables as methods can be called without parentheses. And the way ruby does it is this: if a local variable for some name "x" is not defined then "x" is the same as "x()", meaning a method call; but when a local variable "x" is defined then "x" refers to the variable not the method and "x()" still refers to the method. In your example you are creating a local variable "func" in which case "func(2)" is the only way to refer to a method named "func" as opposed to a variable named "func".

What you are trying to do is quite similiar to what is described here: http://www.thekode.net/ruby/techniques/DefiningMethodsWithClosures.html but with the downside of not being able to do this in a global context, define_method can only be called for a class or a module.</description>
		<content:encoded><![CDATA[<p>kybernetikos, the reason neither of your examples could work is that ruby has to be able to differentiate between methods and local variables as methods can be called without parentheses. And the way ruby does it is this: if a local variable for some name &#8220;x&#8221; is not defined then &#8220;x&#8221; is the same as &#8220;x()&#8221;, meaning a method call; but when a local variable &#8220;x&#8221; is defined then &#8220;x&#8221; refers to the variable not the method and &#8220;x()&#8221; still refers to the method. In your example you are creating a local variable &#8220;func&#8221; in which case &#8220;func(2)&#8221; is the only way to refer to a method named &#8220;func&#8221; as opposed to a variable named &#8220;func&#8221;.</p>
<p>What you are trying to do is quite similiar to what is described here: <a href="http://www.thekode.net/ruby/techniques/DefiningMethodsWithClosures.html" rel="nofollow">http://www.thekode.net/ruby/techniques/DefiningMethodsWithClosures.html</a> but with the downside of not being able to do this in a global context, define_method can only be called for a class or a module.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kybernetikos</title>
		<link>http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/#comment-39252</link>
		<author>kybernetikos</author>
		<pubDate>Tue, 20 Mar 2007 11:49:20 +0000</pubDate>
		<guid>http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/#comment-39252</guid>
		<description>I like ruby so far, but procs and lambda seem like a hack to allow functional programming.  What happened to the principle of least surprise?  Why can't I do (which I tried immediately)

func = do &#124;x&#124;
    x+5
end
func(2)

So I read your page and try

func = lambda {&#124;x&#124; x+5 }
func(2)

And that still doesn't work.  Why on earth would I want to do func.call(2)?  Putting square brackets after something calls the [] method, why doesn't putting () brackets after something call the call method?   I would say that although you are able to program in a functional style in ruby, it makes it hard for you.  It's a piece of nonorthogonality that is ugly.  There should be no need for wrapping blocks in procs.</description>
		<content:encoded><![CDATA[<p>I like ruby so far, but procs and lambda seem like a hack to allow functional programming.  What happened to the principle of least surprise?  Why can&#8217;t I do (which I tried immediately)</p>
<p>func = do |x|<br />
    x+5<br />
end<br />
func(2)</p>
<p>So I read your page and try</p>
<p>func = lambda {|x| x+5 }<br />
func(2)</p>
<p>And that still doesn&#8217;t work.  Why on earth would I want to do func.call(2)?  Putting square brackets after something calls the [] method, why doesn&#8217;t putting () brackets after something call the call method?   I would say that although you are able to program in a functional style in ruby, it makes it hard for you.  It&#8217;s a piece of nonorthogonality that is ugly.  There should be no need for wrapping blocks in procs.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
