Forum: Ruby on Rails I don't even know how to ask the question (4 lines of code)

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.
2dd904ec5981c31e7bb7a5743a53caf8?d=identicon&s=25 Bruce Balmer (brucebalmer)
on 2005-12-31 01:01
(Received via mailing list)
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?
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2005-12-31 01:16
(Received via mailing list)
On 12/30/05, Bruce Balmer <brucebalmer@mac.com> 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.
55428cbf149e35dd4b65f1d019d04139?d=identicon&s=25 Matthew Palmer (Guest)
on 2005-12-31 01:37
(Received via mailing list)
On Fri, Dec 30, 2005 at 04:58:06PM -0700, Bruce Balmer 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
3ec705c5dd3480c6268b72c5617e8dae?d=identicon&s=25 Michael Smedberg (Guest)
on 2005-12-31 02:05
(Received via mailing list)
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
2dd904ec5981c31e7bb7a5743a53caf8?d=identicon&s=25 Bruce Balmer (brucebalmer)
on 2006-01-01 21:31
(Received via mailing list)
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
55428cbf149e35dd4b65f1d019d04139?d=identicon&s=25 Matthew Palmer (Guest)
on 2006-01-01 21:31
(Received via mailing list)
On Fri, Dec 30, 2005 at 05:51:35PM -0700, Bruce Balmer 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
2dd904ec5981c31e7bb7a5743a53caf8?d=identicon&s=25 Bruce Balmer (brucebalmer)
on 2006-01-01 21:31
(Received via mailing list)
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
This topic is locked and can not be replied to.