Skip to content

Instantly share code, notes, and snippets.

@tildedave
Created December 2, 2010 14:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tildedave/725337 to your computer and use it in GitHub Desktop.
Save tildedave/725337 to your computer and use it in GitHub Desktop.
from Andrew Kennedy
# scoping in ruby 1.8.7
x = 0
[1,2,3].each{|x| puts x}
puts x
@tildedave
Copy link
Author

I definitely feel that 'weird' scoping behavior is important to know about, even if it has been fixed in later versions. Python 3 and Ruby 1.9.2 may be the most recent versions of their respective languages, but these are not the default versions on the most recent Ubuntu (for example), so there is still an awful lot of Python 2.6 and Ruby 1.8.7 code being written.

As there is no ruby specification (although https://github.com/rubyspec/rubyspec looks like an interesting project!), these implementation bugs are also bugs in the platonic Ruby language itself.

I have no political axe to grind against Ruby -- I like Ruby a lot! However, it is always interesting to me how other languages implement closures. This particular oddity is why you do a renaming on bound variables or some use mechanism like de bruijn indices in most closure implementations.

@banister
Copy link

@tildedave, I agree the scoping behaviour of 1.8.7 is important to know about; the only point i took issue with was it being labelled "Ruby Scoping Behaviour"; esp. since this behaviour has been corrected in the stable and official 1.9 branch for 2 years now. :)

Ruby is in the process of getting a specification, and there is a draft one already released, see here: http://ruby-std.netlab.jp/draft_spec/download.html

@banister
Copy link

@ironcamel,

Are you aware that Ruby 1.9 is almost a total reimplementation of Ruby? You appear to think it was a minor update.

Internally, Ruby 1.9 has very little in common with Ruby 1.8. In terms of features; Ruby 1.9 adds some major new features and cleans up a few inconsistencies and bugs - one of those bugs being the block variable scoping pointed out in this gist. Nonetheless Ruby 1.9 aims to be more or less 95% backwards compatible with Ruby 1.8 - which it has achieved.

Major releases are allowed to have breaking changes. Ruby 1.9 is a major release, it has breaking changes. In terms of 'expectation' - there were expected to be breaking changes. Once again: It is a Major Release.

Get over it. Everyone else has.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment