Some code that I had that used attr_accessor_with_default
in a rails model is now giving me a deprecation warning, telling me to "Use Ruby instead!"
So, thinking that maybe there was a new bit in ruby 1.9.2
that made attr_accessor
handle defaults, I googled it, but I don't see that. I did see a bunch of methods to override attr_accessor
to handle defaults though.
Is that what they mean when they tell me to "Use Ruby?" Or am I supposed to write full getters/setters now? Or is there some new way I can't find?
This apidock page suggests to just do it in the initialize method.
class Something
attr_accessor :pancakes
def initialize
@pancakes = true
super
end
end
Don't forget to call super
especially when using ActiveRecord or similar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With