virtus
virtus copied to clipboard
Hash with a default_proc being set
I'm trying to get a default_proc set for attributes, and I feel like either I'm missing something obvious or something wacky is going on. I've tried all of the following with no success:
attribute :features, Hash, default: Hash.new{ |h, k| h[k] = 0 }
attribute :features, Hash, default: ->(*){ Hash.new{ |h, k| h[k] = 0 } }
attribute :features, Hash, default: :create_default_proc
def create_default_proc
Hash.new{ |h, k| h[k] = 0 }
end
attribute :features, Hash.new{ |h, k| h[k] = 0 }
In every case, I end up with something like:
obj = Foobar.new
obj.features # => {}
# The following should be 0
obj.features["blah"] # => nil
# The following should not be nil
obj.features.default_proc # => nil