Skip to content

Instantly share code, notes, and snippets.

@Mon-Ouie
Created August 16, 2014 06:32
Show Gist options
  • Save Mon-Ouie/06a160c8c75cbec9678b to your computer and use it in GitHub Desktop.
Save Mon-Ouie/06a160c8c75cbec9678b to your computer and use it in GitHub Desktop.
pry(main):1> class Foo; attr_writer :bar; end
=> nil
pry(main):2> x = Foo.new
=> #<Foo:0x46040d8>
pry(main):3> x.bar ||= 3 # errors
NoMethodError: undefined method `bar' for #<Foo:0x000000046040d8>
from (pry):3:in `__pry__'
pry(main):4> (x.bar = x.bar if defined?(x.bar)) || x.bar = 3 # no error
=> 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment