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
···
--
Posted via http://www.ruby-forum.com/.
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
--
Posted via http://www.ruby-forum.com/.
Hi,
In newer versions of Ruby (1.9) you can use
sum = a_array.reduce :+
See
http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-reduce
--
Posted via http://www.ruby-forum.com/.
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
On Tue, Sep 4, 2012 at 10:19 AM, Edward QU <lists@ruby-forum.com> 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?
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
thanks Jan, very cool.
--
Posted via http://www.ruby-forum.com/.
Index of Files, Classes & Methods in Ruby 1.9.3 (Ruby 1.9.3) very useful.
2012/9/4 Edward QU <lists@ruby-forum.com>
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--
Posted via http://www.ruby-forum.com/\.One suggestion. I'm beginner in Ruby too, and I find
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.)
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?
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.
--
Posted via http://www.ruby-forum.com/\.
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.
--
Posted via http://www.ruby-forum.com/.
@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 }
--
Posted via http://www.ruby-forum.com/.
They want you to do their homework, not teach them something.
Henry
On 4/09/2012, at 8:33 PM, Robert Klemme wrote:
On Tue, Sep 4, 2012 at 10:19 AM, Edward QU <lists@ruby-forum.com> 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.
You could also use a shorter version of inject:
array.inject(:+)
On Mon, Sep 17, 2012 at 9:27 PM, Padma J. <lists@ruby-forum.com> wrote:
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
> EdwardHi Edward,
You could also use the inject method from the Enumerable module like
this:array.inject(0) { |sum, i| sum+i }
Cheers.
--
Posted via http://www.ruby-forum.com/\.
Padma J. wrote in post #1076389:
array.inject(0) { |sum, i| sum+i }
Cheers.
You think i is a good, descriptive variable name there?
--
Posted via http://www.ruby-forum.com/\.
i for item, e for element, a for addend, n for number, w for whatever...
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.
--
Lars Haugseth
That seems quite apparent. But what is your point?
Cheers
robert
On Tue, Sep 4, 2012 at 10:37 AM, Henry Maddocks <hmaddocks@me.com> wrote:
On 4/09/2012, at 8:33 PM, Robert Klemme wrote:
On Tue, Sep 4, 2012 at 10:19 AM, Edward QU <lists@ruby-forum.com> 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.They want you to do their homework, not teach them something.
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
You still need to give it an initial value or it will be wrong when there
are no elements.
array.inject 0, :+
On Mon, Sep 17, 2012 at 11:03 PM, Brandon Weaver <keystonelemur@gmail.com>wrote:
array.inject(0) { |sum, i| sum+i }
Cheers.
--
Posted via http://www.ruby-forum.com/\.You could also use a shorter version of inject:
array.inject(:+)
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?
On Sep 17, 2012, at 22:19 , 7stud -- <lists@ruby-forum.com> wrote:
A: No... no, it does not.
Granted and noted, nice catch
On Mon, Sep 17, 2012 at 11:28 PM, Josh Cheek <josh.cheek@gmail.com> wrote:
On Mon, Sep 17, 2012 at 11:03 PM, Brandon Weaver <keystonelemur@gmail.com>wrote:
array.inject(0) { |sum, i| sum+i }
Cheers.
--
Posted via http://www.ruby-forum.com/\.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, :+
No, Padma's right. I propose:
array.inject(0) { |sum, banana_fritter| sum+banana_fritter }
# boy, that sure was sum banana fritter!
On 18 September 2012 17:05, Ryan Davis <ryand-ruby@zenspider.com> wrote:
On Sep 17, 2012, at 22:19 , 7stud -- <lists@ruby-forum.com> 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.
Sorry, I meant 7stud is right.
Padma remains wrong.
On 18 September 2012 18:10, Matthew Kerwin <matthew@kerwin.net.au> wrote:
No, Padma's right. I propose: