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
Analyse.get("2010-01-1","2010-09-1") can get a table (date from"2010-01-1" to"2010-09-1"), then input Analyse.mean, to get the mean of close from "2010-01-1"to "2010-09-1" , how can i make Analyse.get("2010-01-1","2010-09-1").mean to run ?