# Defining a default proc for Hash

**URL:** <https://www.ruby-forum.com/t/defining-a-default-proc-for-hash/166860>\
**Category:** Ruby\
**Created:** [April 30, 2009, 9:15am UTC](https://www.ruby-forum.com/t/defining-a-default-proc-for-hash/166860 "2009-04-30T09:15:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Srijayanth\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@Srijayanth\_S](https://www.ruby-forum.com/u/Srijayanth_S)\
**Post date:** [April 30, 2009, 9:15am UTC](https://www.ruby-forum.com/t/defining-a-default-proc-for-hash/166860/1 "2009-04-30T09:15:13Z")

</div>

Hello,

Is there a way to redefine or newly define a default proc for Hash way  
after  
it has been constructed and populated with elements?

There seems to be a #default= but no #default\_proc=

Thank you,

Jayanth

---

<div class="post-metadata">

**Author:** ![Srijayanth\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@Srijayanth\_S](https://www.ruby-forum.com/u/Srijayanth_S)\
**Post date:** [April 30, 2009, 9:28am UTC](https://www.ruby-forum.com/t/defining-a-default-proc-for-hash/166860/2 "2009-04-30T09:28:04Z")

</div>

Srijayanth S. wrote:

> Hello,
> 
> Is there a way to redefine or newly define a default proc for Hash way  
> after  
> it has been constructed and populated with elements?
> 
> There seems to be a #default= but no #default\_proc=

Did you read this?

## ---------------------------------------------------------- Hash#default= hsh.default = obj =\> hsh

```
  ....

  It is not possible to set the a default to a
 +Proc+ that will be executed on each key lookup.

   ...

    # This doesn't do what you might hope...
    h.default = proc do |hash, key|
      hash[key] = key + key
    end
    h[2] #=> #<Proc:0x401b3948@-:6>
    h["cat"] #=> #<Proc:0x401b3948@-:6>
```

---

<div class="post-metadata">

**Author:** ![Srijayanth\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@Srijayanth\_S](https://www.ruby-forum.com/u/Srijayanth_S)\
**Post date:** [April 30, 2009, 9:39am UTC](https://www.ruby-forum.com/t/defining-a-default-proc-for-hash/166860/3 "2009-04-30T09:39:47Z")

</div>

Yes, I did. All that tells me is that it isn’t possible to set a default  
proc using default or any other calls. Which is why I asked, if there is  
any  
_other_ way to do it. If not, it simply can’t be done. Of course, it  
would  
also be nice if someone could explain the rationale for not wanting to  
alter  
the default proc once it has been defined.

Thank you,

Jayanth

---

<div class="post-metadata">

**Author:** ![Srijayanth\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@Srijayanth\_S](https://www.ruby-forum.com/u/Srijayanth_S)\
**Post date:** [April 30, 2009, 11:51am UTC](https://www.ruby-forum.com/t/defining-a-default-proc-for-hash/166860/4 "2009-04-30T11:51:46Z")

</div>

Thanks,

That’s twice in two days that merge has come up wrt Hash.

Jayanth

---

<div class="post-metadata">

**Author:** ![Srijayanth\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@Srijayanth\_S](https://www.ruby-forum.com/u/Srijayanth_S)\
**Post date:** [April 30, 2009, 1:21pm UTC](https://www.ruby-forum.com/t/defining-a-default-proc-for-hash/166860/5 "2009-04-30T13:21:01Z")

</div>

2009/4/30 Srijayanth S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Yes, I did. All that tells me is that it isn’t possible to set a default  
> proc using default or any other calls. Which is why I asked, if there is any  
> _other_ way to do it. If not, it simply can’t be done. Of course, it would  
> also be nice if someone could explain the rationale for not wanting to alter  
> the default proc once it has been defined.

It’s there - but not in 1.8.\*:

13:14:34 JavaProducts\_NGCP\_dev\_R1.0\_PoA$ allruby -e ‘puts  
Hash.instance\_methods.grep(/proc/)’  
CYGWIN\_NT-5.1 padrklemme1 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin  
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]  
default\_proc  
ruby 1.9.1p0 (2009-01-30 revision 21907) [i386-cygwin]  
default\_proc  
default\_proc=  
13:14:51 JavaProducts\_NGCP\_dev\_R1.0\_PoA$

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Srijayanth\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@Srijayanth\_S](https://www.ruby-forum.com/u/Srijayanth_S)\
**Post date:** [April 30, 2009, 10:02am UTC](https://www.ruby-forum.com/t/defining-a-default-proc-for-hash/166860/6 "2009-04-30T10:02:05Z")

</div>

2009/4/30 Srijayanth S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Yes, I did. All that tells me is that it isn’t possible to set a default  
> proc using default or any other calls. Which is why I asked, if there is any  
> _other_ way to do it. If not, it simply can’t be done. Of course, it would  
> also be nice if someone could explain the rationale for not wanting to alter  
> the default proc once it has been defined.

You can do it like this:

class Hash  
def default\_proc=(blk)  
self.replace(Hash.new(&blk).merge(self))  
end  
end

h = {1=\>2,3=\>4}  
p h.default\_proc  
h.default\_proc = proc {|h,k| h[k]=3 }  
p h.default\_proc

Regards,

Park H.
