# How to get Mycompute.sum(\[1,2,3,4\]).mean=2.5?

**URL:** <https://rubytalk.org/t/how-to-get-mycompute-sum-1-2-3-4-mean-2-5/60118>\
**Category:** ruby-talk\
**Created:** [20 September 2010 03:47 UTC](https://rubytalk.org/t/how-to-get-mycompute-sum-1-2-3-4-mean-2-5/60118 "2010-09-20T03:47:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pen\_Ttt](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@Pen\_Ttt](https://rubytalk.org/u/Pen_Ttt)\
**Post date:** [20 September 2010 03:47 UTC](https://rubytalk.org/t/how-to-get-mycompute-sum-1-2-3-4-mean-2-5/60118/1 "2010-09-20T03:47:30Z")

</div>

class Mycompute  
&nbsp;&nbsp;def self.sum(a)  
&nbsp;&nbsp;&nbsp;&nbsp;a.inject(0){|s,i|s+i}  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def self.mean(a)  
&nbsp;&nbsp;&nbsp;&nbsp;a.inject(0){|s,i|s+i}/(a.size.to\_f)  
&nbsp;&nbsp;end  
end  
Mycompute.sum([1,2,3,4])  
=\> 10  
Mycompute.mean([1,2,3,4])  
=\> 2.5

how can i get Mycompute.sum([1,2,3,4]).mean=2.5?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Daniel\_G](https://avatars.discourse-cdn.com/v4/letter/d/ecccb3/32.png) [@Daniel\_G](https://rubytalk.org/u/Daniel_G)\
**Post date:** [20 September 2010 04:54 UTC](https://rubytalk.org/t/how-to-get-mycompute-sum-1-2-3-4-mean-2-5/60118/2 "2010-09-20T04:54:49Z")

</div>

I don't know if you tried to say how you can write a code that accepts  
Mycompute.sum([1,2,3,4]).mean ... not to know if the mean of an array is the  
specified as argument: "Mycompute.sum([1,2,3,4]).mean=2.5?"

If you need to accept the first approach, you should better extend the Array  
class:

class Array  
&nbsp;&nbsp;def mean  
&nbsp;&nbsp;&nbsp;&nbsp;self.inject(0){|s,i|s+i}/(self.size.to\_f)  
&nbsp;&nbsp;end  
end

[1,2,3,4].mean # =\> 2.5

Since the sum doesn't have mean, but a set of values.

I hope I have helped in a way with my answer.

Daniel Gaytán

> **···**
>
> 2010/9/19 Pen Ttt \<myocean135@yahoo.cn\>
> 
> > class Mycompute  
> > def self.sum(a)  
> > &nbsp;&nbsp;&nbsp;a.inject(0){|s,i|s+i}  
> > end  
> > def self.mean(a)  
> > &nbsp;&nbsp;&nbsp;a.inject(0){|s,i|s+i}/(a.size.to\_f)  
> > end  
> > end  
> > Mycompute.sum([1,2,3,4])  
> > =\> 10  
> > Mycompute.mean([1,2,3,4])  
> > =\> 2.5
> > 
> > how can i get Mycompute.sum([1,2,3,4]).mean=2.5?  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![botp1](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@botp1](https://rubytalk.org/u/botp1)\
**Post date:** [20 September 2010 05:34 UTC](https://rubytalk.org/t/how-to-get-mycompute-sum-1-2-3-4-mean-2-5/60118/3 "2010-09-20T05:34:41Z")

</div>

> class Mycompute  
> def self.sum(a)  
> a.inject(0){|s,i|s+i}  
> end  
> def self.mean(a)  
> a.inject(0){|s,i|s+i}/(a.size.to\_f)  
> end  
> end  
> Mycompute.sum([1,2,3,4])  
> =\> 10  
> Mycompute.mean([1,2,3,4])  
> =\> 2.5
> 
> how can i get Mycompute.sum([1,2,3,4]).mean=2.5?

uh,uh, you cant because you get a conflict: your sum returns a value  
(not array), and your mean is chaining on sum's return value, but mean  
in turn requires its values coming fr its arguments.... in short:  
confusing...

my suggestion is, start simple as possible, eg

$ irb

> > class Mycompute  
> > &nbsp;&nbsp;def initialize(arr)  
> > &nbsp;&nbsp;&nbsp;&nbsp;@arr=arr  
> > &nbsp;&nbsp;end

?\> def values

> > &nbsp;&nbsp;&nbsp;&nbsp;@arr  
> > &nbsp;&nbsp;end

?\> def size

> > &nbsp;&nbsp;&nbsp;&nbsp;@arr.size  
> > &nbsp;&nbsp;end

?\> def sum

> > &nbsp;&nbsp;&nbsp;&nbsp;@arr.inject(0.0){|s,i|s+i}  
> > &nbsp;&nbsp;end

?\> def mean

> > &nbsp;&nbsp;&nbsp;&nbsp;sum/size  
> > &nbsp;&nbsp;end  
> > end

=\> nil

> >

?\> x = Mycompute.new([1,2,3,4])  
=\> #\<Mycompute:0x0000000102cca8 @arr=[1, 2, 3, 4]\>

> > p x.sum

10.0  
=\> 10.0

> > p x.mean

2.5  
=\> 2.5

> > p x.values

[1, 2, 3, 4]  
=\> [1, 2, 3, 4]

> > p x.values.min

1  
=\> 1

> > p x.values.count

4  
=\> 4

> > p x.values.map{|x| x\*x}

[1, 4, 9, 16]  
=\> [1, 4, 9, 16]

kind regards -botp

> **···**
>
> On Mon, Sep 20, 2010 at 11:47 AM, Pen Ttt \<myocean135@yahoo.cn\> wrote:

---

<div class="post-metadata">

**Author:** ![Pen\_Ttt](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@Pen\_Ttt](https://rubytalk.org/u/Pen_Ttt)\
**Post date:** [21 September 2010 12:21 UTC](https://rubytalk.org/t/how-to-get-mycompute-sum-1-2-3-4-mean-2-5/60118/4 "2010-09-21T12:21:23Z")

</div>

here is my code:  
def mean(n)  
s=0  
m=0  
self.each{|item|  
if item\>n then  
puts item  
s=s+n  
m=m+1  
else  
end}  
return s/(m.to\_f)  
end  
here is output  
[1,2,3,4].mean(2)  
=\> 2.0  
why i can't get  
[1,2,3,4].mean(2)  
=\> 3.5

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Pen\_Ttt](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@Pen\_Ttt](https://rubytalk.org/u/Pen_Ttt)\
**Post date:** [21 September 2010 12:31 UTC](https://rubytalk.org/t/how-to-get-mycompute-sum-1-2-3-4-mean-2-5/60118/5 "2010-09-21T12:31:29Z")

</div>

sorry,i correct it  
def mean(n)  
s=0  
m=0  
self.each{|item|  
if item\>n then  
puts item  
s=s+item  
m=m+1  
else  
end}  
return s/(m.to\_f)  
end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Adam\_Prescott1](https://avatars.discourse-cdn.com/v4/letter/a/2acd7d/32.png) [@Adam\_Prescott1](https://rubytalk.org/u/Adam_Prescott1)\
**Post date:** [21 September 2010 12:40 UTC](https://rubytalk.org/t/how-to-get-mycompute-sum-1-2-3-4-mean-2-5/60118/6 "2010-09-21T12:40:25Z")

</div>

data = [1, 2, 3, 4]  
sum = data.inject { |a, b| a + b } #=\> 10  
sum/data.size.to\_f #=\> 2.5

---

<div class="post-metadata">

**Author:** ![Pen\_Ttt](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@Pen\_Ttt](https://rubytalk.org/u/Pen_Ttt)\
**Post date:** [22 September 2010 00:40 UTC](https://rubytalk.org/t/how-to-get-mycompute-sum-1-2-3-4-mean-2-5/60118/7 "2010-09-22T00:40:00Z")

</div>

require 'rubygems'  
require 'mysql'  
class Analyse  
def self.get(from,to)  
&nbsp;&nbsp;open('/tmp/result','w') do |wfile|  
&nbsp;&nbsp;dbh = Mysql.real\_connect("localhost", "root", "\*\*\*\*\*\*")  
&nbsp;&nbsp;dbh.query("use stock;")  
&nbsp;&nbsp;result=dbh.query("select symbol,date,open,high,low,close \  
from `#{symbol}` where (date \> from and date\< to) order by date  
asc;")  
&nbsp;&nbsp;compute=[]  
&nbsp;&nbsp;result.each{|row| wfile.puts row.join(";")}  
&nbsp;&nbsp;end  
end

&nbsp;&nbsp;def self.mean  
&nbsp;&nbsp;&nbsp;amean=[]  
&nbsp;&nbsp;&nbsp;open('/tmp/result','r'){|rfile|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line=rfile.readlines  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line.each{|item|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;amean\<\< item.chomp.split(";")[-1].to\_f  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;amean=amean.compact  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;amean.inject(0){|s,n| s+n}/(amean.size.to\_f)  
&nbsp;&nbsp;&nbsp;end  
end  
end  
input  
Analyse.get("2010-01-1","2010-09-1") can get a file /tmp/result  
again input  
Analyse.mean can get what i need,  
but Analyse.get("2010-01-1","2010-09-1").mean can't work,  
would you mind to tell me how to fix the mean method to make

Analyse.get("2010-01-1","2010-09-1").mean can work??

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Pen\_Ttt](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@Pen\_Ttt](https://rubytalk.org/u/Pen_Ttt)\
**Post date:** [23 September 2010 05:39 UTC](https://rubytalk.org/t/how-to-get-mycompute-sum-1-2-3-4-mean-2-5/60118/8 "2010-09-23T05:39:04Z")

</div>

a friend help me solve it,

require 'rubygems'  
require 'mysql'  
class Analyse  
def self.get(from,to)  
&nbsp;&nbsp;object = Analyse.new  
&nbsp;&nbsp;open('/tmp/result','w') do |wfile|  
&nbsp;&nbsp;dbh = Mysql.real\_connect("localhost", "root", "\*\*\*\*\*\*")  
&nbsp;&nbsp;dbh.query("use stock;")  
&nbsp;&nbsp;result=dbh.query("select symbol,date,open,high,low,close \  
from `#{symbol}` where (date \> from and date\< to) order by date  
asc;")  
&nbsp;&nbsp;compute=[]  
&nbsp;&nbsp;result.each{|row| wfile.puts row.join(";")}  
&nbsp;&nbsp;end  
&nbsp;&nbsp;return object  
end

&nbsp;&nbsp;def self.mean  
&nbsp;&nbsp;&nbsp;amean=[]  
&nbsp;&nbsp;&nbsp;open('/tmp/result','r'){|rfile|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line=rfile.readlines  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line.each{|item|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;amean\<\< item.chomp.split(";")[-1].to\_f  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;amean=amean.compact  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;amean.inject(0){|s,n| s+n}/(amean.size.to\_f)  
&nbsp;&nbsp;&nbsp;end  
end  
end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
