ruby よく使う红宝石
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby よく使う红宝石相关的知识,希望对你有一定的参考价值。
#!/usr/local/bin/ruby
require "net/https"
require "digest"
require "uri"
require "open-uri"
require "fileutils"
require "pg"
================================================== File系
fetch_dir = File.expand_path "../../process/20140101-20140923", __FILE__
fetch_path = File.join fetch_dir, "#{code}.csv"
path_index = File.join dir, "#{code}_#{x}.html"
FileUtils.mkdir_p dir
load File.expand_path "../extract_page.rb", __FILE__
File.open("company_info.txt", 'w') {|file|
for x in $code
company = CompanyInfo.new(x)
file.write "#{x} #{company.mini_purchas_price} #{company.per}\n"
p company.mini_purchas_price
end
}
# ファイルへ書き込み処理
data = open(url).read
open(path_index, "w") do |f|
f.write(data)
sleep(3)
end
# ファイルへ読み込み
in_f = open("Mothers.txt", "r:utf-8")
$code = Array.new
$code = [0]
i = 0;
in_f.each {|line|
$code[i] = line.to_i
i = i + 1
}
in_f.close()
# ファイルへの追加書き込み
open(set_path, "a") do |f|
# 整形してcsvに保存
data = data.to_s
data = data.gsub(/\[|\]/,'').gsub(/ /,'')
f.writ
end
# ファイルの一覧を表示
fileList= Array.new()
fileList= Dir::entries(fetch_dir)
fileList.delete("."); fileList.delete(".."); fileList.delete(".DS_Store")
fileList.each do |file|
p file
end
# フォーマット
date << ary_date[0] << format("%02d", ary_date[1]) << format("%02d", ary_date[2])
================================================== DB系
# 証券コードを取得と利用
codes = Array.new()
conn = PG.connect( dbname: 'stock' )
i = 0
conn.exec( "SELECT * FROM company_profiles " ) do |result|
result.each do |row|
code = row.values_at('code')[0]
codes[i] = code.to_i
i = i + 1
end
end
# もし既存であれば削除する
conn.exec('drop table if exists stock_day1')
puts "table 'stock_day1' has dropped."
codes.each do |code|
for x in 1..4
dl_pages("http://info.finance.yahoo.co.jp/history/?code=#{code}.T&sy=2014&sm=1&sd=1&ey=2014&em=9&ed=23&tm=d&p=#{x}", code, x)
p "#{code}:#{x}"
end
end
# 現在時刻の取得
time=Time.now
year=time.year
month=time.month
day=time.day
# 例外処理
month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
wrong_number = [0, 1, 2, 3, 4, 5,
"6", "7", 8, 9, 10, 11]
wrong_number.each do |num|
begin
puts month[num]
rescue => ex
puts ex.message
puts num
next
end
end
# データを挿入するときidとdayの組み合わせがすでにあるとき[EXSIST]を表示させる
conn.prepare('stm1', 'insert into consTest (
id,
day)
values ($1, $2);')
conn.prepare('stm2', 'select id, day from consTest where id = $1 ;')
data = Array.new()
data = [[1, '2014_09_22'],[5, '2014_05_1']]
data.each do |ary|
# 既存のデータでないか確認
conn.exec("select id, day from consTest where id = #{ary[0]} and day = to_date( '#{ary[1]}', 'YYYY_MM_DD');") do |result|
$exsist = 0
result.each do |row|
if row.key?("id") then
p "[EXSIST] id:#{ary[0]} day:#{ary[1]}"
$exsist = 1
else
end
end
end
if $exsist != 1
conn.exec_prepared('stm1', [ ary[0], ary[1] ])
end
end
# 偏差値の取得 [[code, day],[code, day],[code, day]...]
hen_i = 0
cnne.exec( " select p.code as over70_code,p.day as over70_day, p.price_hensati, v.code, v.day, v.volume_hensati
from price_hensati p, volume_hensati v
where p.code = v.code and p.day = v.day and p.price_hensati >= 90 and v.volume_hensati >= 90; " ) do |result|
result_size = 5
@over70_data = Array.new(result_size){Array.new(2){0}}
result.each do |row|
@over70_data[hen_i][0] = row.values_at('over70_code')[0]
@over70_data[hen_i][1] = row.values_at('over70_day')[0]
hen_i = hen_i + 1
end
end
================================================== 判定系
place = "東京都"
if (/#{place}/.match("Go to 東京都"))
p "1"
else
p "2"
end
================================================== 整形系
sprintf( "%10.2f", $hv )
以上是关于ruby よく使う红宝石的主要内容,如果未能解决你的问题,请参考以下文章