virtus icon indicating copy to clipboard operation
virtus copied to clipboard

Hash with a default_proc being set

Open pnomolos opened this issue 10 years ago • 0 comments

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

pnomolos avatar Oct 30 '15 18:10 pnomolos