# Insert key/value pair into hash, then read it out

**URL:** <https://www.ruby-forum.com/t/insert-key-value-pair-into-hash-then-read-it-out/152723>\
**Category:** Rails\
**Created:** [November 6, 2008, 9:28pm UTC](https://www.ruby-forum.com/t/insert-key-value-pair-into-hash-then-read-it-out/152723 "2008-11-06T21:28:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [November 6, 2008, 9:28pm UTC](https://www.ruby-forum.com/t/insert-key-value-pair-into-hash-then-read-it-out/152723/1 "2008-11-06T21:28:11Z")

</div>

Hi there,

I’ve been trying to figure out if/how the following is possible…

@books: array of books

@books.each do |book|  
do stuff…  
do stuff…  
do stuff…  
book[“number\_of\_pages”] = 123 #this is supposed to “inject” the  
key/value pair  
end

# I want to:

-\> “inject” the key ‘number\_of\_pages’ with the value ‘123’ into the hash  
“book” (“book” IS a hash, right?), for every book in @books

-\> Then, show in the view: All “Number of pages”: \<%= @books.collect  
{|b| [b.number\_of\_pages] } %\>

It doesn’t work like this… (-\> “undefined method `number\_of\_pages’ for  
#Book:0xb5c022bc”)

How can I solve it…?

Many thanks for your help!  
Tom

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [November 7, 2008, 11:52am UTC](https://www.ruby-forum.com/t/insert-key-value-pair-into-hash-then-read-it-out/152723/2 "2008-11-07T11:52:25Z")

</div>

… anybody … ?

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [November 7, 2008, 12:08pm UTC](https://www.ruby-forum.com/t/insert-key-value-pair-into-hash-then-read-it-out/152723/3 "2008-11-07T12:08:36Z")

</div>

On Fri, Nov 7, 2008 at 7:28 AM, Tom Ha  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> @books: array of books  
> @books.each do |book|  
> do stuff…  
> do stuff…  
> do stuff…  
> book[“number\_of\_pages”] = 123 #this is supposed to “inject” the  
> key/value pair  
> end

> → “inject” the key ‘number\_of\_pages’ with the value ‘123’ into the hash  
> “book” (“book” IS a hash, right?), for every book in @books

No, book is a class, I assume at least.

Probably in your models directory you have a book.rb file that has

class Book \< ActiveRecord::Base  
  
end

→ Then, show in the view: All “Number of pages”: \<%= @books.collect

> {|b| [b.number\_of\_pages] } %\>
> 
> It doesn’t work like this… (-\> “undefined method `number\_of\_pages’ for  
> #Book:0xb5c022bc”)

You probably want a column called number\_of\_pages in your books table

Then you can do:  
book.number\_of\_pages = 123  
book.number\_of\_pages #=\> 123

Hope that helps

Mikel

–

> **[Mikel Lindsaar](https://lindsaar.net/)**
>
> Internet Home for Mikel Lindsaar

Rails, RSpec and Life blog…

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [November 7, 2008, 12:48pm UTC](https://www.ruby-forum.com/t/insert-key-value-pair-into-hash-then-read-it-out/152723/4 "2008-11-07T12:48:57Z")

</div>

Thanks, Mikel. We’re half way there, I guess:

> No, book is a class, I assume at least.
> 
> Probably in your models directory you have a book.rb file that has
> 
> class Book \< ActiveRecord::Base  
>   
> end

Yes, that’s the case.

> You probably want a column called number\_of\_pages in your books table
> 
> Then you can do:  
> book.number\_of\_pages = 123  
> book.number\_of\_pages #=\> 123

Not really. Actually, I don’t want to store the “number\_of\_pages” in a  
database, I’d just lik to add it “on the fly” to each “book” to be able  
to display it in the view, for each “book”.

Is this possible in my case?

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [November 7, 2008, 1:08pm UTC](https://www.ruby-forum.com/t/insert-key-value-pair-into-hash-then-read-it-out/152723/5 "2008-11-07T13:08:26Z")

</div>

On Nov 7, 2008, at 5:48 AM, Tom Ha wrote:

> able  
> to display it in the view, for each “book”.
> 
> Is this possible in my case?

If you add these to the class:

def calc\_number\_of\_pages  
@page\_count = (your code to determine page count)  
end

def number\_of\_pages  
@page\_count  
end

Then you can do:

@books.each do |book|  
book.calc\_number\_of\_pages  
end

Then in your view you can have

\<%= book.number\_of\_pages %\>

and it will do what you’ve described.

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [November 7, 2008, 1:11pm UTC](https://www.ruby-forum.com/t/insert-key-value-pair-into-hash-then-read-it-out/152723/6 "2008-11-07T13:11:50Z")

</div>

Great, thanks a lot!
