Appending to an array inside a loop

for some reason, i am having a hard time with some thing REALLY simple…

i am going through an array of objects, making a one element hash of the
data, and appending that hash to an array…

here’s the code…

@places.each do |p|
  city_string['city_text'] = p.city.titleize + ', ' + p.state.upcase
  @city_list << city_string
end

after some investigation, what i am finding is that the output of this
is an n element array where n is the size of @places… the bad part is
that each element is a hash with the key-value pair of the last object
in @places

i did some tracing through the loop, and i found that during each
iteration of the loop, the entire array up to that point takes on
key-value values for the current element…

i am stumped…

anyone have any ideas?

thanks!

Sergio R. wrote:

  @city_list << city_string

i am stumped…

anyone have any ideas?

I’m not quite sure what you want to be in @city_list but try this:

@places.each do |p|
city_string = Hash.new
city_string[‘city_text’] = p.city.titleize + ', ’ + p.state.upcase
@city_list << city_string
end


Michael W.

I’m not quite sure what you want to be in @city_list but try this:

@places.each do |p|
city_string = Hash.new
city_string[‘city_text’] = p.city.titleize + ', ’ + p.state.upcase
@city_list << city_string
end

perfect!

i was trying to do:

city_string.clear

which i thought was the same thing, but it didn’t work…

Sergio R. wrote:

perfect!

i was trying to do:

city_string.clear

which i thought was the same thing, but it didn’t work…

clear will delete the contents of city_string but it’s still the same
object
(i.e. it keeps the same object id) while city_string = Hash.new creates
a new
object each time through the loop. That’s why @city_list had copies of
the
last state of city_string. Each element of the array was pointing to the
same
object_id.


Michael W.

can anyone tell me how to install ruby and then rails on ubuntu linux?
thanks
arjun

YES. I am sure someone can tell you. But, I hope no one does.

I DON’T MEAN TO BE RUDE BUT…

  1. Do not hijack a thread.
  2. Get off your **** and type “install ruby rails on ubuntu linux” into
    google (which I am sure you are aware of) and be surprised at what it
    shows you.

best,
Mohit.

Mr. Mohit S.,
I was lead to believe that RoR community was a helpful lot. Yes i am
aware that Google exist and that does gives results. But please use your
“Superior Intellect” and put a little pressure on upstairs and then
think why will i ask that question if i was getting all the help in
google. What will be the use of these(and similar) forums(which unlike,
does have lot of helpful people) i would be asking for “help”. And i
always believed that developer though famously don’t have lot of social
traits but still have basic decency in them.right?..anyway thank for
ALL your help. I was just a newbie in this forum…been to lot but never
got such a splendid welcome…
[p.s: sorry to others to have put the query in this thread…was not
very clear where to put it and was in trouble…but anyway thanks but no
thanks solved my prob from other sources]
Regards
Arjun
Mohit S. wrote:

YES. I am sure someone can tell you. But, I hope no one does.

I DON’T MEAN TO BE RUDE BUT…

  1. Do not hijack a thread.
  2. Get off your **** and type “install ruby rails on ubuntu linux” into
    google (which I am sure you are aware of) and be surprised at what it
    shows you.

best,
Mohit.

yeh arjun u can ask question…

On 19/03/07, Arjun G. [email protected] wrote:

traits but still have basic decency in them.right?..anyway thank for

I DON’T MEAN TO BE RUDE BUT…

Posted via http://www.ruby-forum.com/.


Arun A.

Hi Arjun,

If you refer to your original post (which included you hijacking a
thread), you don’t seem to have provided any detail at all that you
referred to Google and found that it didn’t work for you. Sarcasm is
good if you can back it up. In any case, if you would be kind enough to
tell us why you felt that Google has let you down and that the fantastic
guides it connects to you are not working for you, perhaps someone would
help.

You are most welcome to the forum here. People here are very welcoming
and I apologize to them that I may have given them a bad name in your
little world. All the same, there is basic etiquette that all newbies
should follow (and I’m a relative newbie myself) -
(1) Google your problem,
(2) Start a new thread and post to a forum what you have done and why it
doesn’t work for you

You are likely to find people much more helpful.

Cheers
Mohit.

Arjun, You are right,thats an interesting question on ruby on rails and
not only interesting but common for all who getting started with ruby on
rails. I think its really bad to break the thread but it’s not a big
issue to communicate like this i.e. Mr. Mohit S.,really he is a
great personality?,he may be well known in his community but i don’t
know him…,there is three kind of people in the
world,(1)literate(2)Illiterate and last one is (3)literate-type
illiterate.
Now you have to think about that in which type Mr. Mohit S.
belongs to???.
Any way all the men belonging to this forum are not bad as this is a big
forum on ruby on rails. All members of this forum are not Mr. Mohit
Sindhwani,so u may ask your questions frequently .I think Mr. Mohit
Sindhwani have to take this communication in cool manner.

Thanks & best regards
Ajit Jha

Arjun G. wrote:

Mr. Mohit S.,
I was lead to believe that RoR community was a helpful lot. Yes i am
aware that Google exist and that does gives results. But please use your
“Superior Intellect” and put a little pressure on upstairs and then
think why will i ask that question if i was getting all the help in
google. What will be the use of these(and similar) forums(which unlike,
does have lot of helpful people) i would be asking for “help”. And i
always believed that developer though famously don’t have lot of social
traits but still have basic decency in them.right?..anyway thank for
ALL your help. I was just a newbie in this forum…been to lot but never
got such a splendid welcome…
[p.s: sorry to others to have put the query in this thread…was not
very clear where to put it and was in trouble…but anyway thanks but no
thanks solved my prob from other sources]
Regards
Arjun
Mohit S. wrote:

YES. I am sure someone can tell you. But, I hope no one does.

I DON’T MEAN TO BE RUDE BUT…

  1. Do not hijack a thread.
  2. Get off your **** and type “install ruby rails on ubuntu linux” into
    google (which I am sure you are aware of) and be surprised at what it
    shows you.

best,
Mohit.

Hi Mohit,
We got of on wrong foot. Anyway thanks.
ciao
Arjun

Hi Ajit

I am not a big personality and don’t consider myself to be.

I’m merely pointing out that any individual should try to search an
answer to her/his question before asking a forum and should explain to
the forum what s/he’s tried and what problems s/he is facing with it.
Merely saying “can anyone tell me how to install ruby and then rails on
ubuntu linux?” is too generic a question and there are many guides
online that cover this topic.

Anyway, this is my last post on this topic.
Cheers
Mohit.

Arjun G. wrote:

I was lead to believe that RoR community was a helpful lot.

RoR only exists because Google can unify its scattered and fragmented
documentation. Hence, we need the threads here to start with a summary
what Google said on a topic, so we know which gaps to fill. Consider
those here who will Google your question, and to cross-check their
answers.

Next, Ubuntu is downstream from Debian, and Debian’s packagers have a
severely broken Ruby system. For example, rubygems is the most
important Ruby library (because it installs all the others), but the
Debian packages don’t include it. Then if you install it, rubygems
breaks for no apparent reason, in various ways.

Last year I tried to get Debian working on Ubuntu on my notebook, and
I gave up and switched to Mandriva (which is downstream from RedHat
and different base packages).

Last month I successfully installed Kubuntu on the same notebook and
then got all of Rails working correctly. I don’t know how I did it -
do not ask me.

Then I tried to install Kubuntu and Rails on a desktop computer, and I
couldn’t get it working with > twice the effort. I couldn’t build Ruby
from scratch and then install everything all the way up, and I
couldn’t apt-get a basic Ruby and then add rubygems on top of that.

So I got CentOS (yet another of RedHat’s “we’re not really RedHat”
project, like Fedora), then yum f—ed up and locked up my GUI over
and over again. So I built Ruby from scratch, installed rubygem, and
everything started working.

If you are a newb (I have >cough< 20 years programming experience, 10
of those on a Un*x), then erase that f—ing Ubuntu and get a
RedHat-derived distro, such as Mandriva or a RedHat.

I DON’T MEAN TO BE RUDE BUT…

  1. Do not hijack a thread.

If you reply to a thread and change the Subject, as a convenience to
get the To line, something in this maze of forum systems will change
the subject back. If the newb could have done that, don’t flame, when
we seniors should be taking better care of our maze.


Phlip
http://c2.com/cgi/wiki?ZeekLand ← NOT a blog!!

Hi Mohit S. ,
its not a matter of big personality,but its matter- some one ask
about his problem,because he is facing that. if ruby on rail-forum
doesn’t help him then there is no meaning of the existence of ruby
forum. if you hearted than sorry for that. but my intension is not like
this.
Anyway
Cheers

Mohit S. wrote:

Hi Ajit

I am not a big personality and don’t consider myself to be.

I’m merely pointing out that any individual should try to search an
answer to her/his question before asking a forum and should explain to
the forum what s/he’s tried and what problems s/he is facing with it.
Merely saying “can anyone tell me how to install ruby and then rails on
ubuntu linux?” is too generic a question and there are many guides
online that cover this topic.

Anyway, this is my last post on this topic.
Cheers
Mohit.

Phlip wrote:

I DON’T MEAN TO BE RUDE BUT…

  1. Do not hijack a thread.

If you reply to a thread and change the Subject, as a convenience to
get the To line, something in this maze of forum systems will change
the subject back. If the newb could have done that, don’t flame, when
we seniors should be taking better care of our maze.

I agree & apologize.
Cheers
Mohit.

arjun ghosh wrote:

Hi Mohit,
We got of on wrong foot. Anyway thanks.
ciao
Arjun

I agree! Welcome to the group.
Cheers
Mohit.

Sergio R. wrote:

@places.each do |p|
  city_string['city_text'] = p.city.titleize + ', ' + p.state.upcase
  @city_list << city_string
end

after some investigation, what i am finding is that the output of this
is an n element array where n is the size of @places… the bad part is
that each element is a hash with the key-value pair of the last object
in @places

Sergio,
The problem is that you keep appending the same hash to the array:
@city_list as was mentioned above! So basically, each element in the
array is pointing to the SAME hash and will take on the key, value pair
of the last assignment.

try:
@places.each do {|p| @city_list << {‘city_text’==>p.city.titlelize +
‘,’ + p.state.upcase} }

Phlip,

If you are a newb (I have >cough< 20 years programming experience,
10
of those on a Un*x), then erase that f—ing Ubuntu and get a
RedHat-derived distro, such as Mandriva or a RedHat.

Actually, installing the Rails “stack” (incl. MySql, Apache, Mongrel,
Subversion) on Ubuntu is a no-brainer, thanks to deprec.

There’s a free screencast that shows the whole process in one go:
http://topfunky.com/clients/peepcode/free-episodes/peepcode-free-deprec.mov

, and it’s documented elsewhere :
http://wiki.slicehost.com/doku.php?id=automated_rails_install_and_deployment_with_deprec_capistrano

Alain

hi all,
thanks every one above in the thread for the helpful links
-ciao
arjun

The following link will point you to the book on Amazon that describes
the process of intalling Ruby on Rails on Ubuntu in detail. I
followed it and had no problems:
http://www.amazon.com/Beginning-Ruby-Rails-E-Commerce-Professional/dp/1590597362/ref=sr_1_6/002-4785372-7363265?ie=UTF8&s=books&qid=1174431620&sr=1-6
Hope this helps.
Bharat