i would like to change the behavior of literal hashes
eg do some bookeeping anytime a
{:k => :v}
is seen
i thought for sure that redefining Hash (class method) would to this, but it
seems not to?
this sort of works :
irb(main):001:0> class Hash; def Hash.; raise; end; end
=> nil
irb(main):002:0> {:k => :v}
=> {:k=>:v}
irb(main):003:0> Hash[:k => :v]
RuntimeError:
from (irb):1:in `’
from (irb):3
my understanding was that a literal {:k => :v} was simply interpeted as a call
to Hash. but this obviously is not correct.
can anyone shed light on this?
-a
···
–
Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================
When you write this ruby create a node (NODE_HASH), this node is directly
processed by rb_eval() (rb_hash_new(), rb_hash_aset()), it don't call an
Hash method
Same with '[1, 2]' the node (NODE_ARRAY) is executed by rb_eval() and don't
call an Array method
Hash[ [ key => value ]* ] → aHash
Creates a new hash populated with the given objects. Equivalent to the
literal { key, value, … }. Keys and values occur in pairs, so there
must be an even number of arguments.
----- Original Message -----
From: “ahoward” ahoward@fsl.noaa.gov
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, April 10, 2003 11:45 AM
Subject: changing behavior of literal {:k => :v}
i would like to change the behavior of literal hashes
eg do some bookeeping anytime a
{:k => :v}
is seen
I once wanted to do this… the conclusion I
came to was that it is not possible.
–
Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================
Hmm. I think it means that the end result is the
same, not that all the internal gyrations are
the same.
Still I’ve wondered whether this could have a
“hook” added. But it might not be worth the
effort.
Hal
···
----- Original Message -----
From: “ahoward” ahoward@fsl.noaa.gov
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, April 10, 2003 2:46 PM
Subject: Re: changing behavior of literal {:k => :v}
On Fri, 11 Apr 2003, Hal E. Fulton wrote:
I once wanted to do this… the conclusion I
came to was that it is not possible.
bummer. the pickaxe sort of implies it is :
…
class methods
Hash[ [ key => value ]* ] → aHash
Creates a new hash populated with the given objects.
Equivalent to the
literal { key, value, … }. Keys and values occur in pairs,
so there
must be an even number of arguments.
Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================