Hash and Array

Hello all

I have problem regarding hash and array

I am retriving records using find method and then doing some operations
on it
and finally i got results like following

[{“1”=>“75225”, “2”=>“Sat”},{“1”=>“75225”, “2”=>“Sat”}]

which contains array of hashes

when we write simple find method of any tables it returns

[#, #, #, #] in this way

so can anybody tell me what is difference between this 2 types of array

becuase I dont want to modify my view file

Also can i convert first array of hashes to acitve record type of array
of hashes

how can we do that ?

Thanks

On Feb 21, 11:00 am, Cyrus D. [email protected]
wrote:

which contains array of hashes

when we write simple find method of any tables it returns

[#, #, #, #] in this way

Sounds like your view just contains <%= some_array %>

Which means that you’ll just get the result of calling to_s on each of
your array elements, which is unlikely to be useful and also invalid
html since what it outputs is something like #<SomeObject:
0x12345678> . It is up to you to produce some meaningful textual
output for your array elements (eg <%= h some_array.inspect %> but
even that is pretty crude)

Fred

thanks fred

but in o/p

when we use find method and print that object it shows like that

[#, #, #, #]

but when i modify same array with some operations

i got array like this

[{“1”=>“75225”, “2”=>“Sat”},{“1”=>“75225”, “2”=>“Sat”}]

now can I convert this as like above like active record type of array ?

Frederick C. wrote:

On Feb 21, 11:00�am, Cyrus D. [email protected]
wrote:

which contains array of hashes

when we write simple find method of any tables it returns

[#, #, #, #] in this way

Sounds like your view just contains <%= some_array %>

Which means that you’ll just get the result of calling to_s on each of
your array elements, which is unlikely to be useful and also invalid
html since what it outputs is something like #<SomeObject:
0x12345678> . It is up to you to produce some meaningful textual
output for your array elements (eg <%= h some_array.inspect %> but
even that is pretty crude)

Fred

No fred

its not something like that this 2 types of array are total different it
seems like that

becuase if you write

@users = User.find(:all)

then in view file you can directly use that array with each loop

@users.each do |c|
c.title
end

but in my case

in view file i write same code like for @users but I am getting errr
like

undefined title

when I use this array

[#,#] [active record array ] it works in view file

but after modifying this array with
[{zip=>‘d’,{po => ‘d’}}]

then we can’t use that directly in view it should some thing i have to
write in file file liek

@users.each do |c|
c.title it will not work
end

instead of that i have to write @users[0].title

got it ? any solution ?

Frederick C. wrote:

On Feb 21, 11:11�am, Cyrus D. [email protected]
wrote:

thanks fred

but in o/p

when we use find method and print that object �it shows like that

[#, #, #, #]

That’s probably because of exactly what I said. the default to_s/
inspect produces output like #<ClassName: …> but that’s not legal
html so it displays as just #. If you want something useful to be
displayed that’s up to you, eg <%= records.collect {|r| r.name}.join
(', ') %> (assuming what you wanted to display was the name attribute
of the records. If you want to display all the attributes you could
probably just call inspect on the attributes property of each of your
records.

Fred

On Feb 21, 12:06 pm, Cyrus D. [email protected]
wrote:

but after modifying this array with
[{zip=>‘d’,{po => ‘d’}}]

then we can’t use that directly in view it should some thing i have to
write in file file liek

Well you’ve changed your array of active record objects into a hash or
an array of hashes - you just need to access that appropriately
(exactly how will depend on what you did to your original array).

Fred

On Feb 21, 11:11 am, Cyrus D. [email protected]
wrote:

thanks fred

but in o/p

when we use find method and print that object it shows like that

[#, #, #, #]

That’s probably because of exactly what I said. the default to_s/
inspect produces output like #<ClassName: …> but that’s not legal
html so it displays as just #. If you want something useful to be
displayed that’s up to you, eg <%= records.collect {|r| r.name}.join
(', ') %> (assuming what you wanted to display was the name attribute
of the records. If you want to display all the attributes you could
probably just call inspect on the attributes property of each of your
records.

Fred

I think there is some confusion in this conversation
first of all: When you do that

@users = User.find(:all)

@users will be one array of users objects, and if user object answer
to the method name you can do:

@users.each do |u| puts u.name end

and you will get all users names,

But if you have somthing like this:

@users = [{:name => “john”}, {:name => “miguel”}]

you cannot do :

@users.each do |u| puts u.name end
because {:name => “jonh”} is an hash and it do not respond to method
name, although you can get the name if you do this {:name => “jonh”}
[:name]

So summarizing it:

a = User.new :name => “foo”
a.name # WORKS

a = {:name => “foo”}
a.name # DO NOT WORK
a[:name] # WORK

I hope that helps you

On Feb 21, 12:06 pm, Cyrus D. [email protected]

Yes fred

I modify that array and hash that is my question…

Can I do that array like original like activ record array ? using any
ruby or rails functions ?

thanks

Frederick C. wrote:

On Feb 21, 12:06�pm, Cyrus D. [email protected]
wrote:

but after modifying this array with
[{zip=>‘d’,{po => ‘d’}}]

then we can’t use that directly in view it should some thing i have to
write in file file liek

Well you’ve changed your array of active record objects into a hash or
an array of hashes - you just need to access that appropriately
(exactly how will depend on what you did to your original array).

Fred

On 23 Feb 2009, at 08:48, Cyrus D. wrote:

Yes fred

I modify that array and hash that is my question…

Can I do that array like original like activ record array ? using any
ruby or rails functions ?

Only if you create new objects from those hashes.

Fred

How can we create new objects from hashes ?

thanks …

Frederick C. wrote:

On 23 Feb 2009, at 08:48, Cyrus D. wrote:

Yes fred

I modify that array and hash that is my question…

Can I do that array like original like activ record array ? using any
ruby or rails functions ?

Only if you create new objects from those hashes.

Fred

On Feb 24, 3:52 am, Cyrus D. [email protected]
wrote:

How can we create new objects from hashes ?

depends what kind of object you want to create. You can’t call
user.foo unless the user object has a foo method, and hashes don’t. So
create an instance of something that does have a foo method. Exactly
what you create and how you use your hash when creating it is entirely
up to you.

Fred