Overriding "", [], and {}

B

Bill Atkins

Is there any way to cause "", [], and {} to have different behaviors.
I'd like to be able to have {} create OrderedHash's instead of Hash's,
but I don't know how to change this behavior. If I do Hash =
OrderedHash, then Hash.new will create an OrderedHash, but {} still
gives me an old-fashioned hash. If I do

def Hash.new *args
OrderedHash.new *args
end

I get the same results. Is it possible to override these built-in
constructors?

Bill
 
H

Hal Fulton

Bill said:
Is there any way to cause "", [], and {} to have different behaviors.
I'd like to be able to have {} create OrderedHash's instead of Hash's,
but I don't know how to change this behavior. If I do Hash =
OrderedHash, then Hash.new will create an OrderedHash, but {} still
gives me an old-fashioned hash. If I do

def Hash.new *args
OrderedHash.new *args
end

I get the same results. Is it possible to override these built-in
constructors?

I thought of something like this three years ago. It's not possible.
{ x => y } is a Hash no matter what you do.

[] can be redefined, but only as a method. [1,2,3] will still be a
plain Array.

That help any?


Hal
 
W

why the lucky stiff

Bill said:
Is there any way to cause "", [], and {} to have different behaviors.
I'd like to be able to have {} create OrderedHash's instead of Hash's,
but I don't know how to change this behavior. If I do Hash =
OrderedHash, then Hash.new will create an OrderedHash, but {} still
gives me an old-fashioned hash.
This isn't what you're looking for, but I think it's the best answer.
Both Array and Hash have a builtin [] class method, which can be used to
define new Arrays or Hashes.
a = Array['goats', 'coats', 'asymptotes'] => ["goats", "coats", "asymptotes"]
h = Hash['g' => 'goats', 'c' => 'coats']
=> {"c"=>"coats", "g"=>"goats"}

You can use the same class method to get away with really clean
definitions of new Array/Hash derivatives. For example, YAML has an
Omap (ordered hash) class:
o = YAML::Omap['c', 'coats', 'g', 'goats'] => [["c", "coats"], ["g", "goats"]]
o.class
=> YAML::Omap

_why
 
R

Robert Klemme

why the lucky stiff said:
Bill said:
Is there any way to cause "", [], and {} to have different behaviors.
I'd like to be able to have {} create OrderedHash's instead of Hash's,
but I don't know how to change this behavior. If I do Hash =
OrderedHash, then Hash.new will create an OrderedHash, but {} still
gives me an old-fashioned hash.
This isn't what you're looking for, but I think it's the best answer.
Both Array and Hash have a builtin [] class method, which can be used to
define new Arrays or Hashes.
a = Array['goats', 'coats', 'asymptotes'] => ["goats", "coats", "asymptotes"]
h = Hash['g' => 'goats', 'c' => 'coats']
=> {"c"=>"coats", "g"=>"goats"}

You can use the same class method to get away with really clean
definitions of new Array/Hash derivatives. For example, YAML has an
Omap (ordered hash) class:
o = YAML::Omap['c', 'coats', 'g', 'goats'] => [["c", "coats"], ["g", "goats"]]
o.class
=> YAML::Omap

_why

or how about

oh = OrderedHash.new.update( {
"foo" => "bar",
"bar2 => "foo",
} )

robert
 
R

Robert Klemme

Hal Fulton said:
Robert said:
Newsbeitrag news:[email protected]...
o = YAML::Omap['c', 'coats', 'g', 'goats']
=> [["c", "coats"], ["g", "goats"]]
o.class
=> YAML::Omap

_why


or how about

oh = OrderedHash.new.update( {
"foo" => "bar",
"bar2 => "foo",
} )

But then the order in the "ordered hash" is not necessarily the same
as that specified in the single hash passed in as a parameter.

My fault, I forgot that OrderedHash maintains insertion order and not some
other order. Sorry, just forget my proposal.

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top