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

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?

···

--
Posted via http://www.ruby-forum.com/.

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 Ttt <myocean135@yahoo.cn>

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?
--
Posted via http://www.ruby-forum.com/\.

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

···

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

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

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

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

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

···

--
Posted via http://www.ruby-forum.com/.

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|