Ruby Quicktips Logo

Ruby Quicktips

Random Ruby and Rails tips.
This blog is dedicated to deliver short, interesting and practical tidbits of the Ruby language and Ruby on Rails framework. Read more...

Your submissions are more than welcome!
Jan 24 ’12

Once set, variables are always defined

When you set a variable in a section of code that is never executed, the variable will still be defined.

if false
  a = 'whatever'
end

puts a
# => "nil"
# Does NOT raise 'NameError: undefined local variable or method `a' for main:Object'

This post was submitted by Olivier El Mekki.

8 notes 0 comments

  1. Olivier El Mekki submitted this to rubyquicktips

Comments

You can use HTML tags for formatting. Wrap code in <code> tags and multiple lines of code in <pre><code> tags.

blog comments powered by Disqus