From: Glen Holcomb Date: 2009-10-10T11:50:55+09:00 Subject: Re: Array help --0003255550f2c163a804758bc3b0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable On Fri, Oct 9, 2009 at 5:56 PM, Nick Wade wrote: > I am a brand spankin' new wannabe ruby programmer. The attached is my > first real program outside of the infamous Hello World app. I was told > that one who wants to start learning a program is to start out with a > simple real world program that interests me. > > Because I like to golf and I keeping track of my scores is something I > do, I figured I'd give this a whirl and then build on it > later...possibly on a web page with Rails. > > Anyhow, I have the prompt and storage of scores working properly but I > don't know how to add up the scores that are entered. Any help would be > greatly appreciated. > > Attachments: > http://www.ruby-forum.com/attachment/4133/golfscoreentry > > -- > Posted via http://www.ruby-forum.com/. > > I haven't looked at your code so probably shouldn't be answering this but..= . To add up the values in an array you can do something like this: total =3D 0 score_array.each do |score| total +=3D score end puts total The each method will step through your score_array one entry at a time and put that value into score. The bit between the score_array.each do |score| and end just adds the score (value of each array position) to the total. The last line prints the total. NOTE if total doesn't exist before the each method is invoked it will disappear when each finishes, that can be a bit surprising when you are jus= t starting out. This happens because Ruby automatically generates a variable the first time it sees it, so it would get created inside the each call. However each is using a block here which creates it's own scope so as soon as it finishes total goes out of scope and is no longer available. --=20 "Hey brother Christian with your high and mighty errand, Your actions speak so loud, I can=92t hear a word you=92re saying." -Greg Graffin (Bad Religion) --0003255550f2c163a804758bc3b0--