[Ruby]ファイルを読み込む

ファイルを読み込む

サンプル

#!/usr/bin/ruby-1.9.3
# coding: utf-8
require 'json'

puts "Content-type: application/json\n\n"

filename="test.txt"
results=Array.new

if File.exist?(filename)
    f = open(filename)
    f.each{|line|
        results.push(line.force_encoding("utf-8"))
    }
    f.close
else
    results.push("no data")
end

puts JSON.generate({'time'=>Time.now.strftime("%Y-%m-%d %H:%M:%S"), 'data'=>results})