# Array help

**URL:** <https://www.ruby-forum.com/t/array-help/177031>\
**Category:** Ruby\
**Created:** [October 10, 2009, 1:56am UTC](https://www.ruby-forum.com/t/array-help/177031 "2009-10-10T01:56:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jegsjegs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8797f3/32.png) [@jegsjegs](https://www.ruby-forum.com/u/jegsjegs)\
**Post date:** [October 10, 2009, 1:56am UTC](https://www.ruby-forum.com/t/array-help/177031/1 "2009-10-10T01:56:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jegsjegs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8797f3/32.png) [@jegsjegs](https://www.ruby-forum.com/u/jegsjegs)\
**Post date:** [October 10, 2009, 4:51am UTC](https://www.ruby-forum.com/t/array-help/177031/2 "2009-10-10T04:51:14Z")

</div>

On Fri, Oct 9, 2009 at 5:56 PM, Nick W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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](http://www.ruby-forum.com/attachment/4133/golfscoreentry)
> 
> –  
> Posted via [http://www.ruby-forum.com/](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 = 0

score\_array.each do |score|  
total += 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  
just  
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.

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I canâ€™t hear a word youâ€™re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![jegsjegs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8797f3/32.png) [@jegsjegs](https://www.ruby-forum.com/u/jegsjegs)\
**Post date:** [October 10, 2009, 5:08am UTC](https://www.ruby-forum.com/t/array-help/177031/3 "2009-10-10T05:08:12Z")

</div>

Hi Nick,

1st of all, in Ruby you do not need to declare or define each of your  
variables at the head of a method definition.

# counter=0 # no need for a counter

# round=0 # seems not to be used

# player=“nick” # gets defines

# holesplayed=0 # gets defines

# score=0 # not needed

totalscore = 0

puts “What is your name”  
player = gets

puts “How many holes did you play today”

# Convert to Fixnum here, so you can call .times on holesplayed.

# See [times (Integer) - APIdock](http://apidock.com/ruby/Integer/times)…

holesplayed = Integer gets  
holesplayed.times do  
puts “Enter the next hole’s score”

# add up with +=, ‘gets.to\_i’ was originally score and score was

‘gets.to\_i’  
totalscore += gets.to\_i # x += y is like x = x + y  
end

puts “your total score was #{ totalscore }”

# lil bonus

puts ‘How many holes did you play today?’  
holes = gets.to\_i

total = (1…holes).inject(0) { |total, \*|  
print 'Enter the next score: ’  
total + gets.to\_i  
}

printf “Your total score was %i.\n”, total

Florian

Am 10.10.2009 um 01:56 schrieb Nick W.:
