[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Defining a default proc for Hash

Srijayanth Sridhar

4/30/2009 7:14:00 AM

[Note: parts of this message were removed to make it a legal post.]

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

5 Answers

7stud --

4/30/2009 7:28:00 AM

0

Srijayanth Sridhar 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>




--
Posted via http://www.ruby-....

Srijayanth Sridhar

4/30/2009 7:35:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

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



On Thu, Apr 30, 2009 at 12:58 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:

> Srijayanth Sridhar 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>
>
>
>
>
> --
> Posted via http://www.ruby-....
>
>

Heesob Park

4/30/2009 8:01:00 AM

0

2009/4/30 Srijayanth Sridhar <srijayanth@gmail.com>:
> 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 Heesob

Srijayanth Sridhar

4/30/2009 9:50:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

Thanks,

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

Jayanth

On Thu, Apr 30, 2009 at 1:31 PM, Heesob Park <phasis@gmail.com> wrote:

> 2009/4/30 Srijayanth Sridhar <srijayanth@gmail.com>:
> > 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 Heesob
>
>

Robert Klemme

4/30/2009 11:16:00 AM

0

2009/4/30 Srijayanth Sridhar <srijayanth@gmail.com>:
> 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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestprac...