what is the ruby way to do this?

A

ako...

@attributes = {}
# Assumes the given object quacks on 'key'
def @attributes.<<( keyed_obj )
(self[keyed_obj.key]||=[])<<keyed_obj
end

@attributes << a1
@attributes << a2
@attributes << a3

this is something i have never seen. would you explain the 'def' line
above? is this the same as class << @attributes; def << ...; end ?
 
J

Jeff Wood

------=_Part_706_870493.1132707994813
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

It's defining the << operator on the @attributes object

j.

@attributes =3D {}

# Assumes the given object quacks on 'key'
def @attributes.<<( keyed_obj )
(self[keyed_obj.key]||=3D[])<<keyed_obj
end

@attributes << a1
@attributes << a2
@attributes << a3

this is something i have never seen. would you explain the 'def' line
above? is this the same as class << @attributes; def << ...; end ?


--
"Remember. Understand. Believe. Yield! -> http://ruby-lang.org"

Jeff Wood

------=_Part_706_870493.1132707994813--
 
J

Jeff Wood

------=_Part_890_1223728.1132708919305
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

I would do

a =3D Hash.new { || [] }

def a.addValue( valueHash )
valueHash.each_pair { |key,value| self[ key ] <<=3D value }
end

a.addValue "blah" =3D> "a"
a.addValue "blah" =3D> "b"
a.addValue "blah" =3D> "c"

I kinda like the look of the arrows ;)

j.

--
"Remember. Understand. Believe. Yield! -> http://ruby-lang.org"

Jeff Wood

------=_Part_890_1223728.1132708919305--
 
D

David A. Black

Hi --

I would do

a = Hash.new { || [] }

I believe that's the first empty || I've ever seen. It's awfully
confusing (my first reaction was: *what* or an empty array? :) I'd
strongly encourage you to eschew that construct. It's deservedly
unheard of :)


David
 
A

Ara.T.Howard

Hi --

I would do

a = Hash.new { || [] }

I believe that's the first empty || I've ever seen. It's awfully
confusing (my first reaction was: *what* or an empty array? :) I'd
strongly encourage you to eschew that construct. It's deservedly
unheard of :)

i must confess i love it! probably won't use it much. but i love it.

-a
--
===============================================================================
| ara [dot] t [dot] howard [at] gmail [dot] com
| all happiness comes from the desire for others to be happy. all misery
| comes from the desire for oneself to be happy.
| -- bodhicaryavatara
===============================================================================
 
J

Jeff Wood

------=_Part_998_18097895.1132710674199
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

ok ...

a =3D Hash.new ->{ [] }

# new syntax ;)

...

Hi --

I would do

a =3D Hash.new { || [] }

I believe that's the first empty || I've ever seen. It's awfully
confusing (my first reaction was: *what* or an empty array? :) I'd
strongly encourage you to eschew that construct. It's deservedly
unheard of :)

i must confess i love it! probably won't use it much. but i love it.

-a
--

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D
| ara [dot] t [dot] howard [at] gmail [dot] com
| all happiness comes from the desire for others to be happy. all misery
| comes from the desire for oneself to be happy.
| -- bodhicaryavatara

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D


--
"Remember. Understand. Believe. Yield! -> http://ruby-lang.org"

Jeff Wood

------=_Part_998_18097895.1132710674199--
 
J

James Britt

Ara.T.Howard said:
Hi --

I would do

a = Hash.new { || [] }


I believe that's the first empty || I've ever seen. It's awfully
confusing (my first reaction was: *what* or an empty array? :) I'd
strongly encourage you to eschew that construct. It's deservedly
unheard of :)


i must confess i love it! probably won't use it much. but i love it.

I love it too, and will use it simply because David said he'd never seen
it before.



James

--

http://www.ruby-doc.org - Ruby Help & Documentation
http://www.artima.com/rubycs/ - Ruby Code & Style: Writers wanted
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys
http://www.30secondrule.com - Building Better Tools
 
D

David A. Black

Hi --

Hi --

I would do

a = Hash.new { || [] }

I believe that's the first empty || I've ever seen. It's awfully
confusing (my first reaction was: *what* or an empty array? :) I'd
strongly encourage you to eschew that construct. It's deservedly
unheard of :)

i must confess i love it! probably won't use it much. but i love it.

Keep it firmly in mind if there's every another obfuscated Ruby
contest.... :)


David
 
D

David A. Black

Hi --

Ara.T.Howard said:
Hi --

On Wed, 23 Nov 2005, Jeff Wood wrote:

I would do

a = Hash.new { || [] }


I believe that's the first empty || I've ever seen. It's awfully
confusing (my first reaction was: *what* or an empty array? :) I'd
strongly encourage you to eschew that construct. It's deservedly
unheard of :)


i must confess i love it! probably won't use it much. but i love it.

I love it too, and will use it simply because David said he'd never seen it
before.

But now I've seen it, so you don't have to :)


David
 
R

Ryan Leavengood

Hi --

I would do

a =3D Hash.new { || [] }

I believe that's the first empty || I've ever seen. It's awfully
confusing (my first reaction was: *what* or an empty array? :) I'd
strongly encourage you to eschew that construct. It's deservedly
unheard of :)

i must confess i love it! probably won't use it much. but i love it.

Yeah that is nuts...but I really like it :)

Ryan
 
D

David A. Black

Hi --

Hi --

On Wed, 23 Nov 2005, Jeff Wood wrote:

I would do

a = Hash.new { || [] }

I believe that's the first empty || I've ever seen. It's awfully
confusing (my first reaction was: *what* or an empty array? :) I'd
strongly encourage you to eschew that construct. It's deservedly
unheard of :)

i must confess i love it! probably won't use it much. but i love it.

Yeah that is nuts...but I really like it :)

Et tu, Leavengood? :)


David
 
R

Ryan Leavengood

Et tu, Leavengood? :)

ROFL. Yes indeed. I have yet to really reveal it, but I have quite a
penchant for obfuscated Ruby code, and plan to compete in the next
IORCC. I also enjoy Ruby Golf, as can be seen in my recent Ruby Quiz
submission.

I just like the irony that it takes a really good Ruby programmer to
write such bad, evil code :)

Ryan
 
R

Rob Rypka

I would do

a =3D Hash.new { || [] }

I believe that's the first empty || I've ever seen. It's awfully
confusing (my first reaction was: *what* or an empty array? :) I'd
strongly encourage you to eschew that construct. It's deservedly
unheard of :)

Perhaps I'm missing something. Is there something wrong with the following=
?

a =3D Hash.new { [] }

irb tells me that it gets a new array each time.
 
L

Lyndon Samson

------=_Part_2066_27187000.1132715258702
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

It would be nice if there was a wiki page somewhere of these Uberdioms...



I would do

a =3D Hash.new { || [] }

I believe that's the first empty || I've ever seen. It's awfully
confusing (my first reaction was: *what* or an empty array? :) I'd
strongly encourage you to eschew that construct. It's deservedly
unheard of :)

Perhaps I'm missing something. Is there something wrong with the
following?

a =3D Hash.new { [] }

irb tells me that it gets a new array each time.
 
R

Ryan Leavengood

Perhaps I'm missing something. Is there something wrong with the followi= ng?

a =3D Hash.new { [] }

irb tells me that it gets a new array each time.

No that is how it works and that is basically what Jeff's code does.
He just has empty "goal-posts" in the block as well (it isn't really
an or operation.) He gets around the new array every time thing by
using self[key] <<=3D value, which is self[key] =3D self[key] << value.
The block will only be called when there is no values for the given
key, and once the above code is called, there will be a value for the
given key (the array.) It is pretty slick (I didn't even know <<=3D
existed.)

Ryan
 
L

Lyndon Samson

------=_Part_3186_8184457.1132721079920
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Hi --


Uberdioms...

I'm not sure what you mean. A wiki page of... ?


Sorry :)

Uberdioms =3D Uber Idioms =3D Made up word representing powerfull and somet=
imes
lacking in immediate clarity language constructs that programmers should
be aware of but not necessarily utilising in all circumstances.





David
 
J

Joel VanderWerf

David said:
Hi --

I would do

a = Hash.new { || [] }


I believe that's the first empty || I've ever seen. It's awfully
confusing (my first reaction was: *what* or an empty array? :) I'd
strongly encourage you to eschew that construct. It's deservedly
unheard of :)

It's not completely useless:

irb(main):001:0> noargs = proc { || 3 }
=> #<Proc:0xb7b3558c@(irb):1>
irb(main):002:0> noargs[]
=> 3
irb(main):003:0> noargs[2]
ArgumentError: wrong number of arguments (1 for 0)
from (irb):3
from (irb):3
 

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
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top