標準入力から、読み込むには、以下の例のようにします。以下の例では、標準入力から入力させて、codeという変数に値を読みこみ、もし空白であった場合には、9631という値をcodeに代入しています。
print "please input code(9631):" code = STDIN.gets.chomp if code == "" then code = "9631" end
print "please input code(9631):" code = STDIN.gets.chomp if code == "" then code = "9631" end
File.open("foo.txt","w"){|file|
file.puts("lien1")
file.puts("line2")
}
flags = File::CREAT | File::TRUNC | File::WRONLY
mode = 0600
File.open("foo.txt", flags, mode){|file|
file.write("data0")
file.write("data1")
}
File.open("foo.txt","a"){|file|
file.puts "2 line"
}
| フラグ | 意味 |
|---|---|
| "r" | 読みこみ専用でファイルを開く |
| "w" | 書き込み専用でファイルを開き、ファイルサイズを0にする |
| "w+" | 読み書き両用にファイルを開き、ファイルサイズを0にする |
| "a" | 追加書き込み用にファイルを開く |
| "a+" | 追加書き込みと読みこみの両用にファイルを開く |
| "b" | バイナリモードにする |
data = File.read("foo.txt")
puts data
data = File.read("foo.txt",5,6)
puts data
data = File.open("foo.txt"){|file|
file.binmode
file.read
}
puts data
data1 = data2 = data3 = nil
File.open("foo.txt"){|file|
file.binmode
data1 = file.read(6)
data2 = file.read(6)
data3 = file.read(6)
}
puts data1
puts data2
puts data3
File.open("foo.txt"){|file|
while line = file.gets
p line
end
}
def dump_lines(enumerable)
enumerable.each{|line|
puts line.chomp.dump
}
end
File.open("foo.txt"){|io|
dump_lines(io)
}
●例えば上記で定義したdef_linesは、配列や文字列を処理するに流用することができます。
ary = [
"element 1 of array",
"element 2 of array",
]
str = <<_end_of_text_
line 1 of string
line 2 of string
_end_of_text_
dump_lines(ary)
dump_lines(str)
File.readlines("foo.txt").each{|line|
line.chomp!
puts line
}
require "date" require "date/holiday" p Date.new(2012,1,9).national_holiday? #=> true
date2ライブラリは以下のサイトから入手できます。
http://raa.ruby-lang.org/list.rhtml?name=date2
●date/holidayライブラリを使うと、今月の第3月曜日などという日付を求めることもできます。以下の例では、今月の第3月曜日を求めています。2012年1月16日が返ってきます。
require "date" require "date/holiday" today = Date.today d = Date.nth_kday(today.year, today.month , 3, 1 ) puts d.to_s #=> 2012.1.16
require 'rubygems'
require 'nokogiri'
doc = Nokogiri::HTML.parse(<<-eohtml)
<title>Hello World</title>
<h1>
This is an awesome document</h1>
I am a paragraph
<a href="http://google.ca/">I am a link</a>
eohtml
####
# Search for nodes by css
doc.css('p > a').each do |a_tag|
puts a_tag.content
end
####
# Search for nodes by xpath
doc.xpath('//p/a').each do |a_tag|
puts a_tag.content
end
####
# Or mix and match.
doc.search('//p/a', 'p > a').each do |a_tag|
puts a_tag.content
end
###
# Find attributes and their values
# doc.search('a').first['href']
●例2
require 'rubygems'
require 'open-uri'
require 'nokogiri'
# Perform a google search
doc = Nokogiri::HTML(open('http://google.com/search?q=tenderlove'))
puts doc
# Print out each link using a CSS selector
# doc.css('h3.r > a.l').each do |link|
doc.css('h3').each do |link|
puts link.content
end
require "open-uri"
open("http://www.example.com/"){|f|
print f.read
}
●open-uriライブラリよりも細かい操作を行ないたいときには、net/httpライブラリを使用します。(例えばPOSTリクエストを送りたいとき)
require "net/http"
require "uri"
url = URI.parse("http://www.ruby-lang.org/ja/")
p url.scheme
p url.host
p url.port
p url.path
p url.to_s
http = Net::HTTP.start(url.host, url.port)
doc = http.get(url.path)
puts doc
●nokogiriを使ってHTMLを解析するには例えば以下のようにします。
require 'rubygems'
require 'open-uri'
require 'nokogiri'
doc = Nokogiri::HTML(open('http://www.google.co.jp/search?q=cofe_arabi'))
#doc.xpath('//h3/a[@class="l"]').each do |link|
doc.xpath('//h3/a').each do |link|
puts link.content
end
require 'rubygems'
require 'open-uri'
require 'nokogiri'
doc = Nokogiri::HTML(open("http://www.ruby-lang.org/ja/"),nil,"utf-8")
doc.css("h3").each do |h3|
puts h3.text
end