<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Ruby Scoping Bug?</title>
	<atom:link href="http://blog.hungrymachine.com/2009/01/28/ruby-scoping-bug/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.hungrymachine.com/2009/01/28/ruby-scoping-bug/</link>
	<description>The guys behind LivingSocial</description>
	<lastBuildDate>Fri, 12 Mar 2010 02:58:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Exeplealarash</title>
		<link>http://blog.hungrymachine.com/2009/01/28/ruby-scoping-bug/comment-page-1/#comment-370</link>
		<dc:creator>Exeplealarash</dc:creator>
		<pubDate>Sat, 31 Oct 2009 07:06:41 +0000</pubDate>
		<guid isPermaLink="false">http://blog.hungrymachine.com/?p=2852#comment-370</guid>
		<description>Other variant is possible also</description>
		<content:encoded><![CDATA[<p>Other variant is possible also</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Radarek</title>
		<link>http://blog.hungrymachine.com/2009/01/28/ruby-scoping-bug/comment-page-1/#comment-228</link>
		<dc:creator>Radarek</dc:creator>
		<pubDate>Fri, 30 Jan 2009 17:22:35 +0000</pubDate>
		<guid isPermaLink="false">http://blog.hungrymachine.com/?p=2852#comment-228</guid>
		<description>&quot;When lexer encounters local variable assignment (a=, a&#124;&#124;=) within the scope (e.g. method body; if-then-else blocks are not new scopes by design), it marks “a” as a local variable and you have no way to call a method “a” then.&quot;

It&#039;s not true. You can always use &quot;()&quot; to make explicit method call.</description>
		<content:encoded><![CDATA[<p>&#8220;When lexer encounters local variable assignment (a=, a||=) within the scope (e.g. method body; if-then-else blocks are not new scopes by design), it marks “a” as a local variable and you have no way to call a method “a” then.&#8221;</p>
<p>It&#8217;s not true. You can always use &#8220;()&#8221; to make explicit method call.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Radarek</title>
		<link>http://blog.hungrymachine.com/2009/01/28/ruby-scoping-bug/comment-page-1/#comment-227</link>
		<dc:creator>Radarek</dc:creator>
		<pubDate>Fri, 30 Jan 2009 17:09:50 +0000</pubDate>
		<guid isPermaLink="false">http://blog.hungrymachine.com/?p=2852#comment-227</guid>
		<description>Guys, everything is expected. If interpreter would decide that given name is local variable or method in runtime it could works even strangely. Consider this example:

http://pastie.org/375282

class Foo
 def title
    &quot;Oh hai.&quot;
  end

  def buggy
    puts title

    if rand(2) == 0
      title = &#039;Oh noes!&#039;
    end

    puts title
  end
end

Foo.new.buggy

Depending on returned value by rand(2) it would use sometimes variable title and sometimes call a method. It would be very evil. Now you can be sure: it always use local variable (even if it will be set to nil).

Another advantage of that behavior is that you IDE/editor can say during editing that given name is variable or method. Netbeans do that by make bold methods so looking at code you can see it clearly. It&#039;s very important because one of frequent allegations that people saying is that it&#039;s difficult to say looking on source code if something is method or variable, for example:

def foo
  # many lines of code
  x = a + b # a and b could be variable of method
end

But we can for 100% say if something is method or variable. As far as I know Perl can&#039;t do that (sometimes) and decide it on runtime. We should be thankful for that.</description>
		<content:encoded><![CDATA[<p>Guys, everything is expected. If interpreter would decide that given name is local variable or method in runtime it could works even strangely. Consider this example:</p>
<p><a href="http://pastie.org/375282" rel="nofollow">http://pastie.org/375282</a></p>
<p>class Foo<br />
 def title<br />
    &#8220;Oh hai.&#8221;<br />
  end</p>
<p>  def buggy<br />
    puts title</p>
<p>    if rand(2) == 0<br />
      title = &#8216;Oh noes!&#8217;<br />
    end</p>
<p>    puts title<br />
  end<br />
end</p>
<p>Foo.new.buggy</p>
<p>Depending on returned value by rand(2) it would use sometimes variable title and sometimes call a method. It would be very evil. Now you can be sure: it always use local variable (even if it will be set to nil).</p>
<p>Another advantage of that behavior is that you IDE/editor can say during editing that given name is variable or method. Netbeans do that by make bold methods so looking at code you can see it clearly. It&#8217;s very important because one of frequent allegations that people saying is that it&#8217;s difficult to say looking on source code if something is method or variable, for example:</p>
<p>def foo<br />
  # many lines of code<br />
  x = a + b # a and b could be variable of method<br />
end</p>
<p>But we can for 100% say if something is method or variable. As far as I know Perl can&#8217;t do that (sometimes) and decide it on runtime. We should be thankful for that.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cies breijs</title>
		<link>http://blog.hungrymachine.com/2009/01/28/ruby-scoping-bug/comment-page-1/#comment-226</link>
		<dc:creator>cies breijs</dc:creator>
		<pubDate>Fri, 30 Jan 2009 10:02:06 +0000</pubDate>
		<guid isPermaLink="false">http://blog.hungrymachine.com/?p=2852#comment-226</guid>
		<description>anybody tested it with 1.9?

i think this is one of the issues Matz pointed out he wants to get &#039;rite&#039; in 1.9/2.0.

_cies.</description>
		<content:encoded><![CDATA[<p>anybody tested it with 1.9?</p>
<p>i think this is one of the issues Matz pointed out he wants to get &#8216;rite&#8217; in 1.9/2.0.</p>
<p>_cies.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ste</title>
		<link>http://blog.hungrymachine.com/2009/01/28/ruby-scoping-bug/comment-page-1/#comment-224</link>
		<dc:creator>ste</dc:creator>
		<pubDate>Thu, 29 Jan 2009 12:07:59 +0000</pubDate>
		<guid isPermaLink="false">http://blog.hungrymachine.com/?p=2852#comment-224</guid>
		<description>
class Foo
  def title
    &quot;Oh hai.&quot;
  end

  def buggy
    puts title

    if false
      title = &#039;Oh noes!&#039;
    end

    puts title()
  end
end

Foo.new.buggy

Oh hai.
Oh hai.


Like Oleg said, &quot;if&quot; does not introduce a new scope: at first &quot;title&quot; is resolved to a method invocation, then intepreter sees a variable assignment inside the _current_ scope, so from there on &quot;title&quot; is bound to a local variable. If for some reason you want to have both a variable and a method with the same name in your code, the least you can do is not fooling the interpreter :-)
I think this &quot;bug&quot; is a very small price to pay to have uniform access.</description>
		<content:encoded><![CDATA[<p>class Foo<br />
  def title<br />
    &#8220;Oh hai.&#8221;<br />
  end</p>
<p>  def buggy<br />
    puts title</p>
<p>    if false<br />
      title = &#8216;Oh noes!&#8217;<br />
    end</p>
<p>    puts title()<br />
  end<br />
end</p>
<p>Foo.new.buggy</p>
<p>Oh hai.<br />
Oh hai.</p>
<p>Like Oleg said, &#8220;if&#8221; does not introduce a new scope: at first &#8220;title&#8221; is resolved to a method invocation, then intepreter sees a variable assignment inside the _current_ scope, so from there on &#8220;title&#8221; is bound to a local variable. If for some reason you want to have both a variable and a method with the same name in your code, the least you can do is not fooling the interpreter <img src='http://blog.hungrymachine.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
I think this &#8220;bug&#8221; is a very small price to pay to have uniform access.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stephen Sykes</title>
		<link>http://blog.hungrymachine.com/2009/01/28/ruby-scoping-bug/comment-page-1/#comment-223</link>
		<dc:creator>Stephen Sykes</dc:creator>
		<pubDate>Wed, 28 Jan 2009 23:34:39 +0000</pubDate>
		<guid isPermaLink="false">http://blog.hungrymachine.com/?p=2852#comment-223</guid>
		<description>Oleg is right, but it may seem surprising.

See here for some more somewhat surprising results: http://pennysmalls.com/2007/10/01/why-ruby-is-not-my-favourite-programming-language/</description>
		<content:encoded><![CDATA[<p>Oleg is right, but it may seem surprising.</p>
<p>See here for some more somewhat surprising results: <a href="http://pennysmalls.com/2007/10/01/why-ruby-is-not-my-favourite-programming-language/" rel="nofollow">http://pennysmalls.com/2007/10/01/why-ruby-is-not-my-favourite-programming-language/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mirko Froehlich</title>
		<link>http://blog.hungrymachine.com/2009/01/28/ruby-scoping-bug/comment-page-1/#comment-222</link>
		<dc:creator>Mirko Froehlich</dc:creator>
		<pubDate>Wed, 28 Jan 2009 23:17:01 +0000</pubDate>
		<guid isPermaLink="false">http://blog.hungrymachine.com/?p=2852#comment-222</guid>
		<description>Wow, interesting bug! I don&#039;t have an explanation for it either, and was able to reproduce it in both Ruby 1.8.6 and 1.8.7. It&#039;s good to know this issue exists, as I&#039;m sure it could cause some debugging headaches... However, I generally try to avoid naming my local variables the same as my methods, because of the potential for confusion.</description>
		<content:encoded><![CDATA[<p>Wow, interesting bug! I don&#8217;t have an explanation for it either, and was able to reproduce it in both Ruby 1.8.6 and 1.8.7. It&#8217;s good to know this issue exists, as I&#8217;m sure it could cause some debugging headaches&#8230; However, I generally try to avoid naming my local variables the same as my methods, because of the potential for confusion.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Oleg Andreev</title>
		<link>http://blog.hungrymachine.com/2009/01/28/ruby-scoping-bug/comment-page-1/#comment-221</link>
		<dc:creator>Oleg Andreev</dc:creator>
		<pubDate>Wed, 28 Jan 2009 22:48:54 +0000</pubDate>
		<guid isPermaLink="false">http://blog.hungrymachine.com/?p=2852#comment-221</guid>
		<description>This is a feature, not a bug. Ruby knows when &quot;a&quot; is a method or a local variable only by lexer heuristics. When lexer encounters local variable assignment (a=, a&#124;&#124;=) within the scope (e.g. method body; if-then-else blocks are not new scopes by design), it marks &quot;a&quot; as a local variable and you have no way to call a method &quot;a&quot; then. 

Sometimes this funny behavior is useful:
http://oleganza.tumblr.com/post/57906287/ruby-local-variable-semantics

In my practice i have never had a trouble with these semantics: i have small methods, thus little number of local variables, and well-chosen names for everything.</description>
		<content:encoded><![CDATA[<p>This is a feature, not a bug. Ruby knows when &#8220;a&#8221; is a method or a local variable only by lexer heuristics. When lexer encounters local variable assignment (a=, a||=) within the scope (e.g. method body; if-then-else blocks are not new scopes by design), it marks &#8220;a&#8221; as a local variable and you have no way to call a method &#8220;a&#8221; then. </p>
<p>Sometimes this funny behavior is useful:<br />
<a href="http://oleganza.tumblr.com/post/57906287/ruby-local-variable-semantics" rel="nofollow">http://oleganza.tumblr.com/post/57906287/ruby-local-variable-semantics</a></p>
<p>In my practice i have never had a trouble with these semantics: i have small methods, thus little number of local variables, and well-chosen names for everything.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
