Forum: Ruby on Rails Hash

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7694e527192e45b73b95f2ca41034463?d=identicon&s=25 Cyrus Dev (cyrusdev)
on 2009-02-19 11:54
Hello all

any one has idea how to add new column to existing hash array ?

I have one hash instance like
@restaurant

using each with index i m looping it and want to add new key and value
to each row

how can i do that ?

please help me

thanks
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-02-19 12:17
(Received via mailing list)
On 19 Feb 2009, at 10:54, Cyrus Dev wrote:

>
> Hello all
>
> any one has idea how to add new column to existing hash array ?
>
by hash array do you mean an array of hashes ?
> I have one hash instance like
> @restaurant
>
> using each with index i m looping it and want to add new key and value
> to each row
>
unless i'm missing part of the question, you just do it.

array_of_hashes.each_with_index do |some_hash, i|
   some_hash[:foo] = :bar
end

If you want to change the array you are iterating over, that isn't
supported.

Fred
7694e527192e45b73b95f2ca41034463?d=identicon&s=25 Cyrus Dev (cyrusdev)
on 2009-02-19 12:20
Hi,
thanks

but I have instance like this

@restaurant= Restaurant.find(:all)

now for each i have to add one more column

@restaurant.each_with_index do |c,index|

     @restaurant[index]['diffinmiles'] =22

end

but this is not work

so that i have to not change in view file and i can direct access like
db values ?

any idea ?



Frederick Cheung wrote:
> On 19 Feb 2009, at 10:54, Cyrus Dev wrote:
>
>>
>> Hello all
>>
>> any one has idea how to add new column to existing hash array ?
>>
> by hash array do you mean an array of hashes ?
>> I have one hash instance like
>> @restaurant
>>
>> using each with index i m looping it and want to add new key and value
>> to each row
>>
> unless i'm missing part of the question, you just do it.
>
> array_of_hashes.each_with_index do |some_hash, i|
>    some_hash[:foo] = :bar
> end
>
> If you want to change the array you are iterating over, that isn't
> supported.
>
> Fred
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2009-02-19 14:46
(Received via mailing list)
On Feb 19, 2009, at 6:20 AM, Cyrus Dev wrote:
> Hi,
> thanks
>
> but I have instance like this
>
> @restaurant= Restaurant.find(:all,
:select => 'restaurants.*, 22 AS diffinmiles'
> but this is not work
>
> so that i have to not change in view file and i can direct access like
> db values ?
>
> any idea ?

Except that is not really what you meant, I'm sure.  If you need some
other method to be available on the Restaurant model, just add it in!

class Restaurant
   def diffinmiles(my_location)
     # do some calculations with other attributes and the passed
parameters
   end
end

>>>
>> If you want to change the array you are iterating over, that isn't
>> supported.
>>
>> Fred

You're not actually changing the array here as Fred suggests, you're
just modifying each member which is perfectly fine (even though, as I
said, not what I think you really want to do here).

-Rob

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
7694e527192e45b73b95f2ca41034463?d=identicon&s=25 Cyrus Dev (cyrusdev)
on 2009-02-20 07:06
Thanks Rob

But actually what I want to do here

I have hash @restaurant

now for each row I want to add diffinmiles and then filter some of rows
from main hash with this new column add

and then sort this new array based on diffin miles

thanks

Rob Biedenharn wrote:
> On Feb 19, 2009, at 6:20 AM, Cyrus Dev wrote:
>> Hi,
>> thanks
>>
>> but I have instance like this
>>
>> @restaurant= Restaurant.find(:all,
> :select => 'restaurants.*, 22 AS diffinmiles'
>> but this is not work
>>
>> so that i have to not change in view file and i can direct access like
>> db values ?
>>
>> any idea ?
>
> Except that is not really what you meant, I'm sure.  If you need some
> other method to be available on the Restaurant model, just add it in!
>
> class Restaurant
>    def diffinmiles(my_location)
>      # do some calculations with other attributes and the passed
> parameters
>    end
> end
>
>>>>
>>> If you want to change the array you are iterating over, that isn't
>>> supported.
>>>
>>> Fred
>
> You're not actually changing the array here as Fred suggests, you're
> just modifying each member which is perfectly fine (even though, as I
> said, not what I think you really want to do here).
>
> -Rob
>
> Rob Biedenharn    http://agileconsultingllc.com
> Rob@AgileConsultingLLC.com
This topic is locked and can not be replied to.