# Hash.new(0) vs. Hash.new { |h,k| h\[k\] = 0 }

**URL:** <https://www.ruby-forum.com/t/hash-new-0-vs-hash-new-h-k-h-k-0/177472>\
**Category:** Rails\
**Created:** [October 18, 2009, 3:28pm UTC](https://www.ruby-forum.com/t/hash-new-0-vs-hash-new-h-k-h-k-0/177472 "2009-10-18T15:28:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![devaulw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8e7dd6/32.png) [@devaulw](https://www.ruby-forum.com/u/devaulw)\
**Post date:** [October 18, 2009, 3:28pm UTC](https://www.ruby-forum.com/t/hash-new-0-vs-hash-new-h-k-h-k-0/177472/1 "2009-10-18T15:28:42Z")

</div>

While putting DRYing some tests, I am trying to initialize a hash as  
follows:

business = Business.new(0)

But this returns an error in testing. Specifically:

1. Error:  
test\_should\_have\_nils\_not\_zeroes\_except\_for\_sales\_tax\_rate(BusinessTest):  
TypeError: can’t dup Fixnum  
/test/unit/business\_test.rb:41:in `new' /test/unit/business_test.rb:41:in`test\_should\_have\_nils\_not\_zeroes\_except\_for\_sales\_tax\_rate’

However, this works fine:

business = Business.new { |h,k| h[k] = 0 }

The hash business is an ActiveRecord class.

Any thoughts on what the heck is going on? Are the two situations  
different because ActiveRecord needs to create the hash first and the  
block assignment lets that happen and then assigns values?

Bill

---

<div class="post-metadata">

**Author:** ![devaulw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8e7dd6/32.png) [@devaulw](https://www.ruby-forum.com/u/devaulw)\
**Post date:** [October 18, 2009, 4:46pm UTC](https://www.ruby-forum.com/t/hash-new-0-vs-hash-new-h-k-h-k-0/177472/2 "2009-10-18T16:46:43Z")

</div>

I spoke too soon. Neither one appears to work.

---

<div class="post-metadata">

**Author:** ![devaulw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8e7dd6/32.png) [@devaulw](https://www.ruby-forum.com/u/devaulw)\
**Post date:** [October 18, 2009, 4:49pm UTC](https://www.ruby-forum.com/t/hash-new-0-vs-hash-new-h-k-h-k-0/177472/3 "2009-10-18T16:49:41Z")

</div>

On Oct 18, 2:28 pm, Bill D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Any thoughts on what the heck is going on? Are the two situations  
> different because ActiveRecord needs to create the hash first and the  
> block assignment lets that happen and then assigns values?

ActiveRecord objects aren’t hashes - if you pass an argument to  
Business.new it’s expecting a hash of attribute values.

Fred

---

<div class="post-metadata">

**Author:** ![devaulw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8e7dd6/32.png) [@devaulw](https://www.ruby-forum.com/u/devaulw)\
**Post date:** [October 19, 2009, 2:53am UTC](https://www.ruby-forum.com/t/hash-new-0-vs-hash-new-h-k-h-k-0/177472/4 "2009-10-19T02:53:13Z")

</div>

> ActiveRecord objects aren’t hashes - if you pass an argument to  
> Business.new it’s expecting a hash of attribute values.
> 
> Fred

Thanks for the information. I’ve decided (at least for the time being)  
to keep my tests painfully expressive and slightly less DRY than other  
code. It can be hard to look as setting a list of attributes to 0 or an  
empty string but it works.

Bill
