An array of hashes

Hello all!
Can this be done better/nicer?
In @person i have an array of telephones. telephone is a hash with an id and a number, and i would like to join them.

@person.telephones.collect {|x| [] << x.number}.join(', ')

I don't know why, but I don't like this line of code... how would you write it?

Francesco wrote:

Hello all!
Can this be done better/nicer?
In @person i have an array of telephones. telephone is a hash with an id
and a number, and i would like to join them.

@person.telephones.collect {|x| << x.number}.join(', ')

I don't know why, but I don't like this line of code... how would you
write it?

  @person.telephones.collect {|x| x.number}.join(', ')

T.

Two things are odd:

1. there is no hash dereferencing in your code

2. you're creating one element arrays all the time in a weired way.

Why don't you just do this?

@person.telephones.collect {|x| x.number}.join(', ')

Kind regards

    robert

···

Francesco <fran.cy@libero.it.invalid> wrote:

Hello all!
Can this be done better/nicer?
In @person i have an array of telephones. telephone is a hash with an
id and a number, and i would like to join them.

@person.telephones.collect {|x| << x.number}.join(', ')

I don't know why, but I don't like this line of code... how would you
write it?

If telephone (x) is indeed a hash, the above would not work. It seems
like telephone is a struct. Right?

Then why don;t you do

@person.telephones.map {|x| x.number}.join(', ')

cheers,

Brian

···

On 07/11/05, Francesco <fran.cy@libero.it.invalid> wrote:

Hello all!
Can this be done better/nicer?
In @person i have an array of telephones. telephone is a hash with an id
and a number, and i would like to join them.

@person.telephones.collect {|x| << x.number}.join(', ')

I don't know why, but I don't like this line of code... how would you
write it?

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

@person.telephones.map {|x| x.number}.join(', ')

···

On Mon, 07 Nov 2005 19:22:13 +0100, Francesco <fran.cy@libero.it.invalid> wrote:

Hello all!
Can this be done better/nicer?
In @person i have an array of telephones. telephone is a hash with an id and a number, and i would like to join them.

@person.telephones.collect {|x| << x.number}.join(', ')

I don't know why, but I don't like this line of code... how would you write it?

Francesco wrote:

@person.telephones.collect {|x| << x.number}.join(', ')

@person.telephones.collect {|x| x.number}.join(', ')

Thank you all! You all gave the same answer, so that must be the correct way :slight_smile:
I don't know why I created that array... Maybe I was thinking too long about that problem.

Thanks again.

Francesco wrote:

Francesco wrote:
> @person.telephones.collect {|x| << x.number}.join(', ')
@person.telephones.collect {|x| x.number}.join(', ')

Thank you all! You all gave the same answer,
so that must be the correct way :slight_smile:

If you show some sample data, you may see alternatives.

Your example with 'x.number' led to the conclusion that
'number' is an accessor into structure other than a Hash.

# If 'telephones' really is a Hash {id => num}, e.g.

    phones = {:home => '00-0001',
              :office => '00-0002',
              :mobile => '00-0003'}

# then each of these:

    puts phones.collect {|x| x[1]}.join(', ')
    puts phones.collect {|k,v| v}.join(', ')
    puts phones.values.join(', ') # <-- better

# gives the same result: #-> 00-0003, 00-0002, 00-0001

If you're happy with what you have already, please ignore.

daz