るりまサーチ

最速Rubyリファレンスマニュアル検索!
55件ヒット [1-55件を表示] (0.122秒)
トップページ > クエリ:File.open[x] > クエリ:print[x] > 種類:ライブラリ[x]

別のキーワード

  1. argf.class lines
  2. argf.class each
  3. argf.class each_line
  4. argf.class gets
  5. argf.class to_a

キーワード

検索結果

logger (109.0)

ログを記録するためのライブラリです。

...er.level = Logger::WARN

logger.debug("Created logger")
logger.info("Program started")
logger.warn("Nothing to do!")

path = "a_non_existent_file"

begin
File
.foreach(path) do |line|
unless line =~ /^(\w+) = (.*)$/
logger.error("Line in wrong format: #{line.chomp}")
end
end
rescue...
...-07T02:22:53.649172 #11601] FATAL -- : Caught exception; exiting
F, [2017-12-07T02:22:53.649222 #11601] FATAL -- : No such file or directory @ rb_sysopen - a_non_existent_file (Errno::ENOENT)
logger_sample.rb:12:in `foreach'
logger_sample.rb:12:in `<main>'

これは log.level が Logger::WARN...
...gfile.log')
//}

3. File オブジェクトを指定

//emlist[][ruby]{
require 'logger'
file
= File.open('foo.log', File::WRONLY | File::APPEND)
# (古いファイルを削除する)新しいログファイルを作成する場合、以下のよ
# うに File::CREAT を指定。
# file = File.open(...

rss (85.0)

RSS を扱うためのライブラリです。

...合は RSS::Parser クラスを使います。
RSS::Parser.parse は String の RSSを パースします(使用するXMLパー
サによっては File や IO オブジェクトなどでもパース可能です)。
* RSS 1.0をパースした場合は RSS::RDF オブジェクト
* RSS 0.9x/2.0を...
...begin
feed = RSS::Parser.parse(File.read(fname), false)
rescue RSS::Error
end

if feed.nil?
puts "#{fname}はRSS 0.9x/1.0/2.0, Atom 1.0のいずれでもありません。"
else
print
_items(feed)
end
end

あとはprint_itemsというメソッドを定義す...
...* RSS::RDF: /rdf:RDF/image要素
* RSS::Rss: /rss/channel/image要素

ここでは、itemsを使って各項目を表示します。

def print_items(feed)
feed.items.each do |item|
puts "#{item.title} : #{item.description}"
end
end

これは、RSSフィードに対して...

net/pop (79.0)

このライブラリは、POP3 (Post Office Protocol version 3) を 用いてPOPサーバからメールを受信する機能を提供するライブラリです。

...if pop.mails.empty?
$stderr.puts 'no mail.'
else
pop.mails.each_with_index do |m, idx| # 各メッセージにアクセスする
File.open
("inbox/#{idx + 1}", 'w') {|f|
f.write m.pop
}
m.delete
end
$stderr.puts "#{pop.mails.size} mails popped."
end...
...urAccount', 'YourPassword') {|pop|
if pop.mails.empty?
$stderr.puts 'no mail.'
else
pop.mails.each_with_index do |m, idx|
File.open
("inbox/#{idx + 1}", 'w') {|f|
f.write m.pop
}
m.delete
end
$stderr.puts "#{pop.mails.size} mails poppe...
...'YourAccount', 'YourPassword') {|pop|
if pop.mails.empty?
$stderr.puts 'no mail.'
else
i = 0
pop.delete_all do |m|
File.open
("inbox/#{i}", 'w') {|f|
f.write m.pop
}
i += 1
end
end
}

クラスメソッドの Net::POP3.delete_al...

bigdecimal (61.0)

bigdecimal は浮動小数点数演算ライブラリです。 任意の精度で 10 進表現された浮動小数点数を扱えます。

...数点数を扱えます。

//emlist[][ruby]{
require 'bigdecimal'
a = BigDecimal("0.123456789123456789")
b = BigDecimal("123456.78912345678", 40)
print
a + b # => 0.123456912580245903456789e6
//}

一般的な 10 進数の計算でも有用です。2 進数の浮動小数点演算には微小な...
...i in (1..10000)
sum = sum + 0.0001
end
print
sum # => 0.9999999999999062
//}

//emlist[例2: 0.0001 を 10000 回足す場合。(BigDecimal)][ruby]{
require 'bigdecimal'

sum = BigDecimal("0")
for i in (1..10000)
sum = sum + BigDecimal("0.0001")
end
print
sum # => 0.1e1
//}

//emlist[例3: 1.2...
...イル file の合計数値を求める例です。

//emlist[digits.txt][ruby]{
0.1
0.1
0.1
//}

//emlist[][ruby]{
require "bigdecimal"
File
::open("digits.txt", "r") do |file|
s = BigDecimal("0")
while line = file.gets
s = s + BigDecimal(line)
end
puts s # => 0.3e0
end

File
::open("dig...

net/smtp (37.0)

メールを送信するためのプロトコル SMTP (Simple Mail Transfer Protocol) を扱うライブラリです。

...t でセッションを開きます。
第一引数がサーバのアドレスで第二引数がポート番号です。
ブロックを使うと File.open と同じように終端処理を自動的にやってくれる
のでおすすめです。

require 'net/smtp'
Net::SMTP.start( 'smtp.examp...
...=== セッションを終了する

メールを送ったら Net::SMTP#finish を呼んで
セッションを終了しなければいけません。
File
のように GC 時に勝手に close されることもありません。

# using SMTP#finish
require 'net/smtp'
smtp = Net::SMTP.start('s...
...ジェクトからならなんでも送ることができます。
以下は File オブジェクトから直接送信する例です。

require 'net/smtp'

Net::SMTP.start('your.smtp.server', 25) {|smtp|
File.open
('Mail/draft/1') {|f|
smtp.send_message f, 'from@example.com', 'to@exam...

絞り込み条件を変える