dear all
a_array=[2,4,6,8,10]
I want to sum the elements of the array:
2+4+6+8+10
how to write the script?
many thanks!
best regards
Edward
dear all
a_array=[2,4,6,8,10]
I want to sum the elements of the array:
2+4+6+8+10
how to write the script?
many thanks!
best regards
Edward
Hi,
In newer versions of Ruby (1.9) you can use
sum = a_array.reduce :+
See
And of course you could also use the classical approach of looping
through the array and adding the elements to a variable:
sum = 0
a_array.each {|e| sum += e}
On Tue, Sep 4, 2012 at 10:19 AM, Edward QU [email protected] wrote:
dear all
a_array=[2,4,6,8,10]
I want to sum the elements of the array:
2+4+6+8+10
how to write the script?
Open the text editor, start typing…
Seriously: how would you do it on paper if you would not know
beforehand how many items there are to add? Think about it. Then
transcribe that algorithm to code - it’s not that difficult.
Documentation of Ruby’s core classes will help:
http://rdoc.info/stdlib/core/frames
Kind regards
robert
thanks Jan, very cool.
On 4/09/2012, at 8:33 PM, Robert K. wrote:
Seriously: how would you do it on paper if you would not know
beforehand how many items there are to add? Think about it.
They want you to do their homework, not teach them something.
Henry
On Tue, Sep 4, 2012 at 10:37 AM, Henry M. [email protected]
wrote:
Open the text editor, start typing…
Seriously: how would you do it on paper if you would not know
beforehand how many items there are to add? Think about it.They want you to do their homework, not teach them something.
That seems quite apparent. But what is your point?
Cheers
robert
Am 04.09.2012 10:19, schrieb Edward QU:
a_array=[2,4,6,8,10]
I want to sum the elements of the array:
2+4+6+8+10
how to write the script?
If I have no idea how to start, I usually ask Google first…
try “sum elements array ruby”.
You will often find your questions already answered on Stack Overflow:
(In my case, it was the second link from the search results.)
Edward QU wrote in post #1074559:
dear all
a_array=[2,4,6,8,10]
I want to sum the elements of the array:
2+4+6+8+10
how to write the script?many thanks!
best regards
Edward
Hi Edward,
You could also use the inject method from the Enumerable module like
this:
array.inject(0) { |sum, i| sum+i }
Cheers.
2012/9/4 Edward QU [email protected]
–
Posted via http://www.ruby-forum.com/.One suggestion. I’m beginner in Ruby too, and I find
Index of Files, Classes & Methods in Ruby 1.9.3 (Ruby 1.9.3) very useful.
On Mon, Sep 17, 2012 at 9:27 PM, Padma J. [email protected] wrote:
Edward
–
Posted via http://www.ruby-forum.com/.
You could also use a shorter version of inject:
array.inject(:+)
On Mon, Sep 17, 2012 at 11:03 PM, Brandon W.
[email protected]wrote:
array.inject(:+)
You still need to give it an initial value or it will be wrong when
there
are no elements.
array.inject 0, :+
Padma J. wrote in post #1076389:
array.inject(0) { |sum, i| sum+i }
Cheers.
You think i is a good, descriptive variable name there?
On Sep 17, 2012, at 22:19 , 7stud – [email protected] wrote:
Padma J. wrote in post #1076389:
array.inject(0) { |sum, i| sum+i }
Cheers.
You think i is a good, descriptive variable name there?
Q: Does it REALLY matter?
A: No… no, it does not.
On Mon, Sep 17, 2012 at 11:28 PM, Josh C. [email protected]
wrote:
You could also use a shorter version of inject:
array.inject(:+)
You still need to give it an initial value or it will be wrong when there
are no elements.array.inject 0, :+
Granted and noted, nice catch
On 18 September 2012 18:10, Matthew K. [email protected]
wrote:
No, Padma’s right. I propose:
Sorry, I meant 7stud is right.
Padma remains wrong.
On 18 September 2012 17:05, Ryan D. [email protected] wrote:
You think i is a good, descriptive variable name there?
Q: Does it REALLY matter?
A: No… no, it does not.
No, Padma’s right. I propose:
array.inject(0) { |sum, banana_fritter| sum+banana_fritter }
# boy, that sure was sum banana fritter!
Or a bit more serious:
The smaller the scope, the less descriptive do variable names need to
be. I mean, in this case you can see where the variable comes from, so
why give it a long name like “integer_element”?
Nontheless, “i” actually isn’t a good choice, because it’s mostly used
for indices. But “e” should be fine.
On 09/18/2012 10:18 AM, Jan E. wrote:
Or a bit more serious:
The smaller the scope, the less descriptive do variable names need to
be. I mean, in this case you can see where the variable comes from, so
why give it a long name like “integer_element”?Nontheless, “i” actually isn’t a good choice, because it’s mostly used
for indices. But “e” should be fine.
i for item, e for element, a for addend, n for number, w for whatever…
@Matthew: that was funny
And I agree, should have used more descriptive variable names and ‘i’
isn’t a good choice as it’s mostly used for indices.
array.inject(0) { |sum, element| sum + element }
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs