skip to main | skip to sidebar

Rubyssimo

Ruby, Rails, Postful, and Some Truly Hideous Ideas

Monday, July 23, 2007

Another Ruby Scoping Shocker

Try this:
i = 10
5.times do |i|
end
i

What would you expect the value of i to be? Coming from a language like Java with its nested variable scoping, I would expect 10. But it is not so in Ruby.

The result is 4, the last value of the variable passed to the block.
Posted by Ara Vartanian at 4:46 PM 2 comments
Labels: ruby
June 2007 Home
Subscribe to: Posts (Atom)

Tags

  • rails (6)
  • ruby (4)
  • web services (3)
  • testing (2)
  • html (1)
  • job protection (1)
  • os (1)
  • plugins (1)
  • quack ideas (1)
  • Postful
  • Postfully Yours

Blog Archive

  • ▼  2007 (15)
    • ▼  July (1)
      • Another Ruby Scoping Shocker
    • ►  June (1)
      • A Thought on Rails and Web Services
    • ►  May (5)
      • ActiveRecord Validations Gotcha
      • REST and Transactional Web Services
      • The Web: Waiting for the Other Shoe to Drop
      • The Document Abstraction
      • Unit Testing and Purity
    • ►  April (8)
      • Insano-Pattern: Tuple Madness
      • Coghead Revisited -- A New Kind of OS?
      • HTML 5 and Drag and Drop
      • In Ruby, Not All Objects Are Created Equally
      • Reporting on Rails
      • How to Make Your ActionController Go Up In A Bang
      • Ruby Scoping Shocker
      • Why I Hate Test Fixtures (And What I Am Prepared T...

About Me

My Photo
Ara Vartanian
I seldom remember the obvious.
View my complete profile