What is the ruby way to do this?

ako… wrote:

sorry, what is ‘ri’?

ri is a commend line program that may or may not have been installed
with your distribution of ruby. It’s a viewer for the documentation
that was (hopefully) also installed with your disctribution of ruby.

If you type “ri Hash.new” it will display information on the class
method Hash.new. If you were to use the form “ri Hash#keys” then you
weould be requesting information about the instance method keys that may
be called on a hash object.

Try it, and if you don’t have it installed then someone can help you get
it set up.

Regards,
Matthew

@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 ?

I would do

a = Hash.new { || [] }

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

a.addValue “blah” => “a”
a.addValue “blah” => “b”
a.addValue “blah” => “c”

I kinda like the look of the arrows :wink:

j.


“Remember. Understand. Believe. Yield! → http://ruby-lang.org

Jeff W.

On 11/22/05, ako… [email protected] wrote:

hello,

i am still learning.

Hopefully there isn’t anyone who isn’t!

Hi –

On Wed, 23 Nov 2005, Jeff W. 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? :slight_smile: I’d
strongly encourage you to eschew that construct. It’s deservedly
unheard of :slight_smile:

David

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

j.

On 11/22/05, ako… [email protected] wrote:

@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 W.

On Wed, 23 Nov 2005, David A. Black wrote:

strongly encourage you to eschew that construct. It’s deservedly
unheard of :slight_smile:

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

-a

ok …

a = Hash.new ->{ [] }

new syntax :wink:

On 11/22/05, Ara.T.Howard [email protected] wrote:

===============================================================================
| 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

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


“Remember. Understand. Believe. Yield! → http://ruby-lang.org

Jeff W.

Ara.T.Howard wrote:

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

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
Ruby Code & Style - 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

Hi –

On Wed, 23 Nov 2005, Ara.T.Howard wrote:

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

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… :slight_smile:

David

Hi –

On Wed, 23 Nov 2005, James B. wrote:

before.
But now I’ve seen it, so you don’t have to :slight_smile:

David

Hi –

On Wed, 23 Nov 2005, Ryan L. wrote:

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

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 :slight_smile:

Et tu, Leavengood? :slight_smile:

David

On 11/22/05, David A. Black [email protected] wrote:

Et tu, Leavengood? :slight_smile:

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 Q.
submission.

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

Ryan

On 11/22/05, Ara.T.Howard [email protected] wrote:

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

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 :slight_smile:

Ryan

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

On 11/22/05, David A. Black [email protected] wrote:

On Wed, 23 Nov 2005, Jeff W. 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? :slight_smile: I’d
strongly encourage you to eschew that construct. It’s deservedly
unheard of :slight_smile:

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

a = Hash.new { [] }

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

Hi –

On Wed, 23 Nov 2005, Lyndon S. wrote:

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

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

David

On 11/22/05, Rob R. [email protected] wrote:

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

a = 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] <<= value, which is self[key] = 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 <<=
existed.)

Ryan

Lyndon S. wrote:

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

Such as http://www.rubygarden.org/ruby?RubyIdioms ?

James

David A. Black wrote:

confusing (my first reaction was: what or an empty array? :slight_smile: I’d
strongly encourage you to eschew that construct. It’s deservedly
unheard of :slight_smile:

It’s not completely useless:

irb(main):001:0> noargs = proc { || 3 }
=> #Proc:0xb7b3558c@:1(irb)
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