tags:

views:

20

answers:

1

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 ?

A: 

Your code above cant run it like that. Analyse.get() should return an object that responds to the method mean().

Wouter de Bie
a frind tell me how to solve it , add object = Analyse.new and return object in my code,thinks . it's my first to use stckover ,i don't know to format my code,i will learn how to do it .i still have question to ask ,but the question is more complicated than this ,i can't express ,i need people who know chinese to help me,would you mind to do that?my friend can't solve it,if you want to give me help,please send me email [email protected],i will express my problem in chinese.