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

**URL:** <https://www.ruby-forum.com/t/how-to-get-mycompute-sum-1-2-3-4-mean-2-5/195223>\
**Category:** Ruby\
**Created:** [September 20, 2010, 5:47am UTC](https://www.ruby-forum.com/t/how-to-get-mycompute-sum-1-2-3-4-mean-2-5/195223 "2010-09-20T05:47:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [September 20, 2010, 5:47am UTC](https://www.ruby-forum.com/t/how-to-get-mycompute-sum-1-2-3-4-mean-2-5/195223/1 "2010-09-20T05:47:28Z")

</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?

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [September 20, 2010, 6:55am UTC](https://www.ruby-forum.com/t/how-to-get-mycompute-sum-1-2-3-4-mean-2-5/195223/2 "2010-09-20T06:55:06Z")

</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  
def mean  
self.inject(0){|s,i|s+i}/(self.size.to\_f)  
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 T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [September 20, 2010, 7:36am UTC](https://www.ruby-forum.com/t/how-to-get-mycompute-sum-1-2-3-4-mean-2-5/195223/3 "2010-09-20T07:36:39Z")

</div>

On Mon, Sep 20, 2010 at 11:47 AM, Pen T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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  
> > def initialize(arr)  
> > @arr=arr  
> > end

?\> def values

> > ```
> > @arr
> > 
> > ```
> > 
> > end

?\> def size

> > ```
> > @arr.size
> > 
> > ```
> > 
> > end

?\> def sum

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

?\> def mean

> > ```
> > sum/size
> > 
> > ```
> > 
> > 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

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [September 21, 2010, 2:31pm UTC](https://www.ruby-forum.com/t/how-to-get-mycompute-sum-1-2-3-4-mean-2-5/195223/4 "2010-09-21T14:31:28Z")

</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

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [September 21, 2010, 2:41pm UTC](https://www.ruby-forum.com/t/how-to-get-mycompute-sum-1-2-3-4-mean-2-5/195223/5 "2010-09-21T14:41:04Z")

</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:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [September 21, 2010, 2:21pm UTC](https://www.ruby-forum.com/t/how-to-get-mycompute-sum-1-2-3-4-mean-2-5/195223/6 "2010-09-21T14:21:21Z")

</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

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [September 22, 2010, 2:39am UTC](https://www.ruby-forum.com/t/how-to-get-mycompute-sum-1-2-3-4-mean-2-5/195223/7 "2010-09-22T02:39:59Z")

</div>

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

def self.mean  
amean=[]  
open(’/tmp/result’,‘r’){|rfile|  
line=rfile.readlines  
line.each{|item|  
amean\<\< item.chomp.split(";")[-1].to\_f  
}}  
amean=amean.compact  
amean.inject(0){|s,n| s+n}/(amean.size.to\_f)  
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??

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [September 23, 2010, 7:39am UTC](https://www.ruby-forum.com/t/how-to-get-mycompute-sum-1-2-3-4-mean-2-5/195223/8 "2010-09-23T07:39:02Z")

</div>

a friend help me solve it,

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

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