# Hash with default

**URL:** <https://www.ruby-forum.com/t/hash-with-default/227813>\
**Category:** Ruby\
**Created:** [March 29, 2013, 10:32am UTC](https://www.ruby-forum.com/t/hash-with-default/227813 "2013-03-29T10:32:57Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![duffbeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@duffbeer](https://www.ruby-forum.com/u/duffbeer)\
**Post date:** [March 29, 2013, 10:32am UTC](https://www.ruby-forum.com/t/hash-with-default/227813/1 "2013-03-29T10:32:57Z")

</div>

I would like to make a hash like h2 with the default described by h in  
one  
line when I create the hash instead of 3 lines and 2 hashes like I did  
here.  
Any ideas?

h = Hash.new {|x,y| x[y] = y}  
h2 = {“two”=\>2,“three”=\>3}  
h.merge!(h2)  
p h[“two”] #\> 2  
p h[“tree”] #\> “tree”

Harry

---

<div class="post-metadata">

**Author:** ![duffbeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@duffbeer](https://www.ruby-forum.com/u/duffbeer)\
**Post date:** [March 29, 2013, 10:46am UTC](https://www.ruby-forum.com/t/hash-with-default/227813/2 "2013-03-29T10:46:01Z")

</div>

If you want to do it in one line you could create a method which does  
this, with whatever arguments you want to include.

---

<div class="post-metadata">

**Author:** ![duffbeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@duffbeer](https://www.ruby-forum.com/u/duffbeer)\
**Post date:** [March 29, 2013, 11:01am UTC](https://www.ruby-forum.com/t/hash-with-default/227813/3 "2013-03-29T11:01:00Z")

</div>

On Fri, Mar 29, 2013 at 6:46 PM, Joel P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> If you want to do it in one line you could create a method which does  
> this, with whatever arguments you want to include.

Thanks. I was just wondering if there was a way to create the hash and  
set  
defaults at the same time.

h = Hash.new({“two”=\>2,“three”=\>3}){|x,y| y}

This does not work, but I want something like it.  
I can make it myself if necessary, I just didn’t like the way it looked.  
It seemed wasteful to use 2 hashes.

Harry

---

<div class="post-metadata">

**Author:** ![duffbeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@duffbeer](https://www.ruby-forum.com/u/duffbeer)\
**Post date:** [March 29, 2013, 11:12am UTC](https://www.ruby-forum.com/t/hash-with-default/227813/4 "2013-03-29T11:12:20Z")

</div>

Well, if you really wanted to do it all in one go then this might work:

h = Hash.new {|x,y|  
if some\_condition  
x[y] = ‘moose’  
else  
x[y] = y  
end  
}

---

<div class="post-metadata">

**Author:** ![duffbeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@duffbeer](https://www.ruby-forum.com/u/duffbeer)\
**Post date:** [March 29, 2013, 3:15pm UTC](https://www.ruby-forum.com/t/hash-with-default/227813/5 "2013-03-29T15:15:43Z")

</div>

On Fri, Mar 29, 2013 at 7:39 PM, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> h = Hash.new {|h, k| k==‘two’ && h[k]=2 or k==‘three’ && h[k]=3 or  
> h[k]=nil}
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

What!?

---

<div class="post-metadata">

**Author:** ![duffbeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@duffbeer](https://www.ruby-forum.com/u/duffbeer)\
**Post date:** [March 29, 2013, 11:39am UTC](https://www.ruby-forum.com/t/hash-with-default/227813/6 "2013-03-29T11:39:09Z")

</div>

h = Hash.new {|h, k| k==‘two’ && h[k]=2 or k==‘three’ && h[k]=3 or  
h[k]=nil}

The one line requirement should be dropped. Clarity trumps brevity  
every time.

---

<div class="post-metadata">

**Author:** ![duffbeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@duffbeer](https://www.ruby-forum.com/u/duffbeer)\
**Post date:** [March 29, 2013, 4:45pm UTC](https://www.ruby-forum.com/t/hash-with-default/227813/7 "2013-03-29T16:45:42Z")

</div>

On 29/03/2013, at 8:32 PM, Harry K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Harry

You _can_ set a default… but you can’t set the default to be a piece  
of code that can change default behaviour. By the way, the first line of  
your program doesn’t work. You know that, right?

To get what you want, you can do this:

h = {}; def h.; self.has\_key?(key) ? super(key) : key; end

or alternatively:

h = {}  
(class \<\< h; self; end).send(:define\_method, :){|key|  
self.has\_key?(key) ? super(key) : key}

Julian

---

<div class="post-metadata">

**Author:** ![duffbeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@duffbeer](https://www.ruby-forum.com/u/duffbeer)\
**Post date:** [March 29, 2013, 4:54pm UTC](https://www.ruby-forum.com/t/hash-with-default/227813/8 "2013-03-29T16:54:29Z")

</div>

On 30/03/2013, at 2:45 AM, Julian L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > 
> 
> h = {}; def h.; self.has\_key?(key) ? super(key) : key; end
> 
> or alternatively:
> 
> h = {}  
> (class \<\< h; self; end).send(:define\_method, :){|key| self.has\_key?(key) ?  
> super(key) : key}
> 
> Julian

Sorry, forgot to set your data in there, too:

h = {“two”=\>2,“three”=\>3}; def h.; self.has\_key?(key) ?  
super(key) : key; end

There you go. Not really on one line, but oh well, you wanted different  
behaviour than the default hash behaviour for defaults.

Julian

---

<div class="post-metadata">

**Author:** ![duffbeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@duffbeer](https://www.ruby-forum.com/u/duffbeer)\
**Post date:** [March 29, 2013, 5:03pm UTC](https://www.ruby-forum.com/t/hash-with-default/227813/9 "2013-03-29T17:03:50Z")

</div>

On 29 March 2013 15:53, Julian L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> h = {“two”=\>2,“three”=\>3}; def h.; self.has\_key?(key) ? super(key) :  
> key; end

Why not use the block to Hash.new instead of overriding # ?

> > h = Hash.new { |h,k| k }  
> > =\> {}  
> > h[“one”]  
> > =\> “one”  
> > h  
> > =\> {}

Note this is different than using h[k] = k in the block.

As for the original question, I think you can rely on default\_proc and  
get around the fact it doesn’t return the receiver pretty easily.

> > h = { “two” =\> 2, “three” =\> 3 }.tap { |o| o.default\_proc = lambda { |h, k| k }  
> > }  
> > =\> {“two”=\>2, “three”=\>3}  
> > h  
> > =\> {“two”=\>2, “three”=\>3}  
> > h[“two”]  
> > =\> 2  
> > h[“five”]  
> > =\> “five”  
> > h  
> > =\> {“two”=\>2, “three”=\>3}

Having said that, I don’t see any benefit in trying to get it down to  
a single line and sacrificing readability. You can define h to be what  
you want, then on a separate line just call #default\_proc=.

---

<div class="post-metadata">

**Author:** ![duffbeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@duffbeer](https://www.ruby-forum.com/u/duffbeer)\
**Post date:** [March 29, 2013, 5:44pm UTC](https://www.ruby-forum.com/t/hash-with-default/227813/10 "2013-03-29T17:44:46Z")

</div>

On 30/03/2013, at 3:02 AM, Adam P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =\> {}
> 
> > > h[“two”]  
> > > =\> 2  
> > > h[“five”]  
> > > =\> “five”  
> > > h  
> > > =\> {“two”=\>2, “three”=\>3}
> 
> Having said that, I don’t see any benefit in trying to get it down to  
> a single line and sacrificing readability. You can define h to be what  
> you want, then on a separate line just call #default\_proc=.

Sorry. I apologise. My information was outdated, and I stand corrected!

---

<div class="post-metadata">

**Author:** ![duffbeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@duffbeer](https://www.ruby-forum.com/u/duffbeer)\
**Post date:** [March 29, 2013, 11:09pm UTC](https://www.ruby-forum.com/t/hash-with-default/227813/11 "2013-03-29T23:09:03Z")

</div>

On Fri, Mar 29, 2013 at 10:32 AM, Harry K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I would like to make a hash like h2 with the default described by h in one  
> line when I create the hash instead of 3 lines and 2 hashes like I did here.  
> Any ideas?
> 
> h = Hash.new {|x,y| x[y] = y}

I don’t think this is a good implementation of a default value: you add  
items to the hash without any need because the return value of h  
where x  
is not stored in the Hash does not change but you incur the overhead of  
storing the value in the Hash (which may include memory reallocation  
etc.).

> h2 = {“two”=\>2,“three”=\>3}  
> h.merge!(h2)  
> p h[“two”] #\> 2  
> p h[“tree”] #\> “tree”

What’s wrong with

h = Hash.new {|x,y| x[y] = y}.merge!(“two”=\>2, “three”=\>3)

? You can even omit the brackets:

h = Hash.new {|x,y| x[y] = y}.merge! “two”=\>2, “three”=\>3

If you have just one usage location where you need the default  
replacement  
this would be an alternative:

h = {“two”=\>2,“three”=\>3}  
…  
h.fetch(key) {|y| h[y] = y}

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![duffbeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@duffbeer](https://www.ruby-forum.com/u/duffbeer)\
**Post date:** [March 30, 2013, 3:01am UTC](https://www.ruby-forum.com/t/hash-with-default/227813/12 "2013-03-30T03:01:35Z")

</div>

Thanks, everybody.

I like this because it does what I want and it is easy for me to read.

h = Hash.new {|x,y| y}.merge!(“two”=\>2, “three”=\>3)  
p h[“two”]  
p h[“three”]  
p h[“tree”]  
p h

Harry
