るりまサーチ

最速Rubyリファレンスマニュアル検索!
3456件ヒット [101-200件を表示] (0.080秒)

別のキーワード

  1. _builtin puts
  2. csv puts
  3. stringio puts
  4. io puts
  5. xmp puts

ライブラリ

キーワード

検索結果

<< < 1 2 3 4 ... > >>

Encoding::Converter#convert(source_string) -> String (55.0)

与えられた文字列を変換して、変換できた結果を返します。 引数の末尾の文字がバイト列の途中で終わっている場合、そのバイト列は変換器内に取り置かれます。 変換を終了させるには Encoding::Converter#finish を呼びます。

...verter.new("utf-8", "euc-jp")
puts
ec.convert("\u3042").dump #=> "\xA4\xA2"
puts
ec.finish.dump #=> ""

ec = Encoding::Converter.new("euc-jp", "utf-8")
puts
ec.convert("\xA4").dump #=> ""
puts
ec.convert("\xA2").dump #=> "\xE3\x81\x82"
puts
ec.finish.dump...
...onverter.new("utf-8", "iso-2022-jp")
puts
ec.convert("\xE3").dump #=> "".force_encoding("ISO-2022-JP")
puts
ec.convert("\x81").dump #=> "".force_encoding("ISO-2022-JP")
puts
ec.convert("\x82").dump #=> "\e$B$\"".force_encoding("ISO-2022-JP")
puts
ec.finish.dump #=> "...

Object#respond_to?(name, include_all = false) -> bool (43.0)

オブジェクトがメソッド name を持つとき真を返します。

...D.new]

list.each{|it| puts it.hello if it.respond_to?(:hello)}
#=> Bonjour

list.each{|it| it.instance_eval("puts hello if it.respond_to?(:hello, true)")}
#=> Bonjour
# Guten Tag

module Template
def main
start
template_method
finish
end

def start
puts
"start"
end

def...
...mentedError.new
end

def finish
puts
"finish"
end
end

class ImplTemplateMethod
include Template
def template_method
"implement template_method"
end
end

class NotImplTemplateMethod
include Template

# not implement template_method
end

puts
ImplTemplateMethod.new.respond_to?...
...r が発生しているが、Rubyによる実装部のため true を返す
puts
NotImplTemplateMethod.new.respond_to?(:template_method) # => true
# GNU/Linux で実行。C言語による実装部のため false を返す
puts
File.respond_to?(:lchmod) # => false
//}

@see Module#method_defi...

Module#prepend(*modules) -> self (37.0)

指定したモジュールを self の継承チェインの先頭に「追加する」ことで self の定数、メソッド、モジュール変数を「上書き」します。

...prepended

//emlist[例][ruby]{
# super と prepend の組み合わせの例
module X
def foo
puts
"X1" # (1x)
super # (2x)
puts
"X2" # (3x)
end
end

class A
prepend X

def foo
puts
"A" #(1a)
end
end

A.new.foo
# (1x) (2x)(ここの super で A#foo を呼びだす) (1a) (3x...
...のモジュールを X, Y を prepend X, Y という順で指定したもの
module Y
def foo
puts
"Y1" #(1y)
super #(2y)
puts
"Y2" #(3y)
end
end

class B
prepend X, Y
def foo
puts
"B" # (1b)
end
end

B.new.foo
# (1x) (2x) (1y) (2y) (1b) (3y) (3x) の順に実行される...

Net::SMTP#open_message_stream(from_addr, *to_addrs) {|f| .... } -> () (37.0)

メール書き込みの準備をし、書き込み先のストリームオブジェクトを ブロックに渡します。ブロック終了後、書きこんだ結果が 送られます。

...後、書きこんだ結果が
送られます。

渡されるストリームオブジェクトは以下のメソッドを持っています。
* puts(str = '') strを出力して CR LFを出力
* print(str) strを出力
* printf(fmt, *args) sprintf(fmt,*args) を出力
* write(str)::...
...{|smtp|
smtp.open_message_stream('from@example.com', 'to@example.net') {|f|
f.puts 'From: from@example.com'
f.puts 'To: to@example.net'
f.puts 'Subject: test mail'
f.puts
f.puts 'This is test mail.'
}
}

ready は obsolete です。

@param from_addr 送信...

Net::SMTP#ready(from_addr, *to_addrs) {|f| .... } -> () (37.0)

メール書き込みの準備をし、書き込み先のストリームオブジェクトを ブロックに渡します。ブロック終了後、書きこんだ結果が 送られます。

...後、書きこんだ結果が
送られます。

渡されるストリームオブジェクトは以下のメソッドを持っています。
* puts(str = '') strを出力して CR LFを出力
* print(str) strを出力
* printf(fmt, *args) sprintf(fmt,*args) を出力
* write(str)::...
...{|smtp|
smtp.open_message_stream('from@example.com', 'to@example.net') {|f|
f.puts 'From: from@example.com'
f.puts 'To: to@example.net'
f.puts 'Subject: test mail'
f.puts
f.puts 'This is test mail.'
}
}

ready は obsolete です。

@param from_addr 送信...

絞り込み条件を変える

Regexp#~ -> Integer | nil (37.0)

変数 $_ の値との間でのマッチをとります。

..."hogehoge"

if /foo/
puts
"match"
else
puts
"no match"
end
# => no match
# ただし、警告がでる。warning: regex literal in condition

reg = Regexp.compile("foo")

if ~ reg
puts
"match"
else
puts
"no match"
end
# => no match

if reg
puts
"match"
else
puts
"no match"
end
# =>...

Date#deconstruct_keys(array_of_names_or_nil) -> Hash (31.0)

パターンマッチに使用する名前と値の Hash を返します。

...3, day: ..7 # deconstruct_keys が使われます
puts
"first Wednesday of the month"
end
#=> "first Wednesday of the month" が出力される

case d
in year: ...2022
puts
"too old"
in month: ..9
puts
"quarter 1-3"
in wday: 1..5, month:
puts
"working day in month #{month}"
end
#=> "working...
...day in month 10" が出力される

# クラスのチェックと組み合わせて利用することもできます
if d in Date(wday: 3, day: ..7)
puts
"first Wednesday of the month"
end
//}

@see d:spec/pattern_matching#matching_non_primitive_objects...

DateTime#deconstruct_keys(array_of_names_or_nil) -> Hash (31.0)

パターンマッチに使用する名前と値の Hash を返します。

...in wday: 1..5, hour: 10..18 # deconstruct_keys が使われます
puts
"Working time"
end
#=> "Working time" が出力される

case dt
in year: ...2022
puts
"too old"
in month: ..9
puts
"quarter 1-3"
in wday: 1..5, month:
puts
"working day in month #{month}"
end
#=> "working day in month 1...
...0" が出力される

# クラスのチェックと組み合わせて利用することもできます
if dt in DateTime(wday: 1..5, hour: 10..18, day: ..7)
puts
"Working time, first week of the month"
end
//}

@see d:spec/pattern_matching#matching_non_primitive_objects...

Enumerator#next -> object (31.0)

「次」のオブジェクトを返します。

...s do
puts
enum.next
end
# => 120
# 121
# 122
//}

//emlist[例2][ruby]{
str = "xyz"
enum = str.each_byte

begin
puts
enum.next while true
rescue StopIteration
puts
"iteration reached at end"
end
# => 120
# 121
# 122
# iteration reached at end
puts
enu...
...m.next
#=> 再度 StopIteration 例外が発生
//}

//emlist[例3: Kernel.#loop は StopIteration を捕捉します。][ruby]{
str = "xyz"
enum = str.each_byte
loop do
puts
enum.next
end
# => 120
# 121
# 122
//}...

File#flock(operation) -> 0 | false (31.0)

ファイルをロックします。

...File.open("/tmp/foo", "w")

f.flock(File::LOCK_EX)
puts
"locked by process1"

fork {
f = File.open("/tmp/foo", "r")
f.flock(File::LOCK_SH)
puts
"locked by process2"
sleep 5
puts
"unlocked by process2"
}

sleep 5

f.flock(File::LOCK_UN)
puts
"unlocked by process1"
sleep 1 # <- 子プロセ...
...スが確実に先にロックするための sleep
f.flock(File::LOCK_EX)
puts
"re-locked by process1"

# => locked by process1
# unlocked by process1
# locked by process2
# unlocked by process2
# re-locked by process1
//}...

絞り込み条件を変える

String#gsub(pattern, replace) -> String (31.0)

文字列中で pattern にマッチする部分全てを 文字列 replace で置き換えた文字列を生成して返します。

...ックスラッシュを倍にするときによくある間違い][ruby]{
puts
'\n'.gsub(/\\/, "\\\\") # => \n # NG
puts
'\n'.gsub(/\\/, '\\\\') # => \n # NG
puts
'\n'.gsub(/\\/, "\\\\\\\\") # => \\n # OK
puts
'\n'.gsub(/\\/, '\\\\\\\\') # => \\n # OK
//}

このような間違い...
...可読性を上げるには、
\& や \1 よりも下記のようにブロック付き形式の gsub を使うべきです。

//emlist[][ruby]{
p 'xbbb-xbbb'.gsub(/x(b+)/) { $1 } # => "bbb-bbb" # OK

puts
'\n'.gsub(/\\/) { '\\\\' } # => \\n # OK
//}

@see String#sub, String#gsub!...
<< < 1 2 3 4 ... > >>