# Hash

**URL:** <https://www.ruby-forum.com/t/hash/160757>\
**Category:** Rails\
**Created:** [February 19, 2009, 11:54am UTC](https://www.ruby-forum.com/t/hash/160757 "2009-02-19T11:54:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cyrus](https://www.ruby-forum.com/u/cyrus)\
**Post date:** [February 19, 2009, 11:54am UTC](https://www.ruby-forum.com/t/hash/160757/1 "2009-02-19T11:54:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cyrus](https://www.ruby-forum.com/u/cyrus)\
**Post date:** [February 19, 2009, 12:17pm UTC](https://www.ruby-forum.com/t/hash/160757/2 "2009-02-19T12:17:04Z")

</div>

On 19 Feb 2009, at 10:54, Cyrus D. 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

---

<div class="post-metadata">

**Author:** ![cyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cyrus](https://www.ruby-forum.com/u/cyrus)\
**Post date:** [February 19, 2009, 2:46pm UTC](https://www.ruby-forum.com/t/hash/160757/3 "2009-02-19T14:46:11Z")

</div>

On Feb 19, 2009, at 6:20 AM, Cyrus D. 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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![cyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cyrus](https://www.ruby-forum.com/u/cyrus)\
**Post date:** [February 19, 2009, 12:20pm UTC](https://www.ruby-forum.com/t/hash/160757/4 "2009-02-19T12:20:17Z")

</div>

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 C. wrote:

> On 19 Feb 2009, at 10:54, Cyrus D. 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

---

<div class="post-metadata">

**Author:** ![cyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cyrus](https://www.ruby-forum.com/u/cyrus)\
**Post date:** [February 20, 2009, 7:06am UTC](https://www.ruby-forum.com/t/hash/160757/5 "2009-02-20T07:06:51Z")

</div>

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 B. wrote:

> On Feb 19, 2009, at 6:20 AM, Cyrus D. 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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid
