Pushing hash

Dave Thomas [mailto:dave@pragprog.com] humbly replied:

Is there a reason why “<<” isn’t allowed for hashes?

Perhaps because hashes aren’t ordered, and the common
semantics of “<<”
implies order.

Yes, you’re right sir Dave, but that is an old notion.
I much prefer

hash << “x”=>2

rather than

hash[“x”] = 2

The latter uses “” notation, wc is used for arrays, wc implies order,
right?

Cheers

Dave

kind regards -botp

Peña, Botp wrote:

Dave Thomas [mailto:dave@pragprog.com] humbly replied:

Is there a reason why “<<” isn’t allowed for hashes?

Perhaps because hashes aren’t ordered, and the common
semantics of “<<”
implies order.

Yes, you’re right sir Dave, but that is an old notion.
I much prefer

hash << “x”=>2

rather than

hash[“x”] = 2

The latter uses “” notation, wc is used for arrays, wc implies order,
right?

Well, perhaps we should either enable it for Hash or disable it
for Set.

But then, pushing hash is illegal in most countries.

Hal

Hi,

At Sat, 24 Apr 2004 12:08:26 +0900,
Hal Fulton wrote in [ruby-talk:98192]:

But then, pushing hash is illegal in most countries.

IMHO, sometime it feels convenient.

$ irb --prompt xmp
class Hash
alias << :update
end
==>nil
table = Hash.new {{}}
==>{}
table[“foo”] <<= {“x”=>1}
==>{“x”=>1}
table
==>{“foo”=>{“x”=>1}}
table[“bar”]
==>{}
table
==>{“foo”=>{“x”=>1}}

Since <<= is assignment “foo” is added, but “bar” is not.

···


Nobu Nakada

Peña, Botp wrote:

Dave Thomas [mailto:dave@pragprog.com] humbly replied:

Is there a reason why “<<” isn’t allowed for hashes?

Perhaps because hashes aren’t ordered, and the common
semantics of “<<”
implies order.

Yes, you’re right sir Dave, but that is an old notion.
I much prefer

hash << “x”=>2

rather than

hash[“x”] = 2

The latter uses “” notation, wc is used for arrays, wc implies order,
right?

Well, perhaps we should either enable it for Hash or disable it
for Set.

i like ‘<<’ because you just bounce on the dang key to type it - this, IMHO,
is the single best thing about c–. i’m glad ruby has it and we should use it
more.

But then, pushing hash is illegal in most countries.

i’m glad someone said that - that subject jumped of the screen at me more than
the one about lesbians earlier this week! i never knew lesbians were into
programming… of course, if you google for them they seem to be into most
everything? where do they find the time?

-a

···

On Sat, 24 Apr 2004, Hal Fulton wrote:

===============================================================================

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
URL :: Solar-Terrestrial Physics Data | NCEI
TRY :: for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done
===============================================================================

$ irb --prompt xmp

       ^^^^^^^^^^^^

That’s a great feature. It would be nice if the output began with a
comment char ‘#’ so that you could paste into irb (or a source file) and
run.

class Hash
alias << :update
end
==>nil
table = Hash.new {{}}
==>{}
table[“foo”] <<= {“x”=>1}
==>{“x”=>1}
table
==>{“foo”=>{“x”=>1}}
table[“bar”]

Did you mean something like this?

 table["bar"] << {"y"=>2}
···

nobu.nokada@softhome.net wrote:

  ==>{}

table
==>{“foo”=>{“x”=>1}}

Since <<= is assignment “foo” is added, but “bar” is not.

Hi,

At Sat, 24 Apr 2004 14:28:45 +0900,
Joel VanderWerf wrote in [ruby-talk:98213]:

$ irb --prompt xmp

       ^^^^^^^^^^^^

That’s a great feature. It would be nice if the output began with a
comment char ‘#’ so that you could paste into irb (or a source file) and
run.

$ grep PROMPT ~/.irbrc
IRB.conf[:PROMPT][:XMP][:RETURN] = “# =>%s\n”

$ irb --prompt xmp
class Hash
alias << :update
end

=>nil

table = Hash.new {{}}

=>{}

table[“foo”] <<= {“x”=>1}

=>{“x”=>1}

table[“foo”] <<= {“y”=>2}

=>{“x”=>1, “y”=>2}

table

=>{“foo”=>{“x”=>1, “y”=>2}}

class Hash
alias << :update
end
==>nil
table = Hash.new {{}}
==>{}
table[“foo”] <<= {“x”=>1}
==>{“x”=>1}
table
==>{“foo”=>{“x”=>1}}
table[“bar”]

Did you mean something like this?

 table["bar"] << {"y"=>2}

Just a shorthand for (table[“foo”] ||= {})[“x”] = 1.

···


Nobu Nakada

Ahh… nice. Thanks!

···

nobu.nokada@softhome.net wrote:

Hi,

At Sat, 24 Apr 2004 14:28:45 +0900,
Joel VanderWerf wrote in [ruby-talk:98213]:

$ irb --prompt xmp

      ^^^^^^^^^^^^

That’s a great feature. It would be nice if the output began with a
comment char ‘#’ so that you could paste into irb (or a source file) and
run.

$ grep PROMPT ~/.irbrc
IRB.conf[:PROMPT][:XMP][:RETURN] = “# =>%s\n”