Search code examples
rubyruby-1.9.3

Ruby: Using a not defined variable


How can I make something like this?

1.9.3p286 :006 > defined? activated_flag
 => nil 
1.9.3p286 :007 > puts (activated_flat ? "activated!" : "no activated")

I would like to see here no activated, but instead I have:

NameError: undefined local variable or method `activated_flat' for main:Object
  from (irb):7
  from /Users/fguillen/.rvm/rubies/ruby-1.9.3-p286/bin/irb:16:in `<main>'

Solution

  • why not using defined?:

    puts (defined?(activated_flat) ? "activated!" : "no activated")
    #=> no activated