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

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?

On 12/30/05, Bruce B. [email protected] 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.

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

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

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

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

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