# I don't even know how to ask the question (4 lines of code)

**URL:** <https://www.ruby-forum.com/t/i-dont-even-know-how-to-ask-the-question-4-lines-of-code/51414>\
**Category:** Rails\
**Created:** [December 31, 2005, 1:01am UTC](https://www.ruby-forum.com/t/i-dont-even-know-how-to-ask-the-question-4-lines-of-code/51414 "2005-12-31T01:01:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![brucebalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@brucebalmer](https://www.ruby-forum.com/u/brucebalmer)\
**Post date:** [December 31, 2005, 1:01am UTC](https://www.ruby-forum.com/t/i-dont-even-know-how-to-ask-the-question-4-lines-of-code/51414/1 "2005-12-31T01:01:39Z")

</div>

I create a hash using this line of code @x = Account.find(:all)

I then want to go through each element in turn and add a key value  
pair to the end of it. I thought this would work, but it does not.

@x.each do |a|

a.merge({“balance”=\>50}

I used merge because I thought ‘a’ would be a hash (how do you find  
out the type of an object?)

I then guess it was an array so I tried both \<\< and + no success.

What should I be doing?

---

<div class="post-metadata">

**Author:** ![brucebalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@brucebalmer](https://www.ruby-forum.com/u/brucebalmer)\
**Post date:** [December 31, 2005, 1:16am UTC](https://www.ruby-forum.com/t/i-dont-even-know-how-to-ask-the-question-4-lines-of-code/51414/2 "2005-12-31T01:16:46Z")

</div>

On 12/30/05, Bruce B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> out the type of an object?)
> 
> I then guess it was an array so I tried both \<\< and + no success.
> 
> What should I be doing?

Account.find(:all) returns an Array of ActiveRecord instances.  
You can determine this with inspect. e.g.:  
logger.debug @x.inspect  
…which will place the details in the log file.

@x.each do |a|

# a is an ActiveRecord object here.

# If ‘balance’ is already an attribute of the object, you can say:

a.balance = 50  
end

Your code would work if each entry in @x was indeed a Hash, but that’s  
not the case with the ActiveRecord finders.

---

<div class="post-metadata">

**Author:** ![brucebalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@brucebalmer](https://www.ruby-forum.com/u/brucebalmer)\
**Post date:** [December 31, 2005, 1:37am UTC](https://www.ruby-forum.com/t/i-dont-even-know-how-to-ask-the-question-4-lines-of-code/51414/3 "2005-12-31T01:37:52Z")

</div>

On Fri, Dec 30, 2005 at 04:58:06PM -0700, Bruce B. wrote:

> I create a hash using this line of code @x = Account.find(:all)

By my understanding, @x will be an array after this line.

> I then want to go through each element in turn and add a key value  
> pair to the end of it. I thought this would work, but it does not.
> 
> @x.each do |a|
> 
> a.merge({“balance”=\>50}

Assuming that what you’re trying to do is set the ‘balance’ attribute on  
all  
of your accounts to 50, you could try

@x.each { |a| a.balance = 50 }

> I used merge because I thought ‘a’ would be a hash

Each ‘a’ should be an object of type Account.

> (how do you find  
> out the type of an object?)

a.class

- Matt

–  
“[the average computer user] has been served so poorly that he expects  
his  
system to crash all the time, and we witness a massive worldwide  
distribution of bug-ridden software for which we should be deeply  
ashamed.”  
– Edsger Dijkstra

---

<div class="post-metadata">

**Author:** ![brucebalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@brucebalmer](https://www.ruby-forum.com/u/brucebalmer)\
**Post date:** [January 1, 2006, 9:31pm UTC](https://www.ruby-forum.com/t/i-dont-even-know-how-to-ask-the-question-4-lines-of-code/51414/4 "2006-01-01T21:31:57Z")

</div>

Thanks. I now guess that a is neither a hash nor an array but rather  
it is just an object of type object. Is that true?

My problem is that a does not currently have a balance attribute and  
so I wish to extend the object to include one. Is this possible? If  
not, how can I convert my object to a hash so I can add this  
attribute ‘balance’ that I require.

bruce

---

<div class="post-metadata">

**Author:** ![brucebalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@brucebalmer](https://www.ruby-forum.com/u/brucebalmer)\
**Post date:** [December 31, 2005, 2:05am UTC](https://www.ruby-forum.com/t/i-dont-even-know-how-to-ask-the-question-4-lines-of-code/51414/5 "2005-12-31T02:05:02Z")

</div>

The variable a will hold an Account object. I think you want to do  
something like

Account.find(:all).each do | a |  
a.balance = 50  
end

(maybe you want to save the accounts also, etc.)

In answer to your other question, you can use functions like is\_a? or  
.class.name to check types of objects:

Account.find(:all).each do | a |  
is\_acct = (a.is\_a? Account)

# is\_acct is now true

typename = a.class.name

# typename holds ‘Account’

end

---

<div class="post-metadata">

**Author:** ![brucebalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@brucebalmer](https://www.ruby-forum.com/u/brucebalmer)\
**Post date:** [January 1, 2006, 9:31pm UTC](https://www.ruby-forum.com/t/i-dont-even-know-how-to-ask-the-question-4-lines-of-code/51414/6 "2006-01-01T21:31:58Z")

</div>

Mathew:

I did understand what you said (except why I would want to avoid  
hashes). The real reason I wanted to avoid hashes (my reason) was  
that I felt that this ought to be possible through a proper object  
oriented approach. Your answer is wonderful. Exactly what my  
instincts were telling me I should search for but which my brain  
could not generate. Now you have said it, your explanation is  
totally and obvious.

Thanks so much. I really appreciate your help with this.

bruce

---

<div class="post-metadata">

**Author:** ![brucebalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@brucebalmer](https://www.ruby-forum.com/u/brucebalmer)\
**Post date:** [January 1, 2006, 9:31pm UTC](https://www.ruby-forum.com/t/i-dont-even-know-how-to-ask-the-question-4-lines-of-code/51414/7 "2006-01-01T21:31:58Z")

</div>

On Fri, Dec 30, 2005 at 05:51:35PM -0700, Bruce B. wrote:

> Thanks. I now guess that a is neither a hash nor an array but rather  
> it is just an object of type object. Is that true?

No, from the code in your last post, each ‘a’ will be an object of type  
Account, which should be a descendant of ActiveRecord::Base.

> My problem is that a does not currently have a balance attribute and  
> so I wish to extend the object to include one. Is this possible? If  
> not, how can I convert my object to a hash so I can add this  
> attribute ‘balance’ that I require.

Keep well away from the idea of hashes. You don’t want them. If you  
need  
to add a new attribute, you can just add it to the database table which  
backs the Account class (if you want to store that value in the  
database),  
or else add an :attr\_accessor balance to your class definition, which  
will  
create the necessary accessor methods to manipulate the instance  
variable  
@balance in your Account objects.

If the previous paragraph was complete gibberish to you, you might be  
well  
served to pick up a copy of the pickaxe and learn some more Ruby  
programming  
(or else my explanatory skills have gone badly downhill lately). I  
can’t  
imagine doing much at all with Rails without understanding the  
underlying  
Ruby code.

- Matt
