るりまサーチ

最速Rubyリファレンスマニュアル検索!
168件ヒット [1-100件を表示] (0.102秒)
トップページ > クエリ:t[x] > モジュール:Kernel[x] > クエリ:gets[x]

別のキーワード

  1. openssl t61string
  2. asn1 t61string
  3. matrix t
  4. t61string new
  5. fiddle type_size_t

ライブラリ

キーワード

検索結果

<< 1 2 > >>

Kernel.#gets(rs = $/) -> String | nil (24238.0)

ARGFから一行読み込んで、それを返します。 行の区切りは引数 rs で指定した文字列になります。

...
@return ファイルの終り(EOF)に到達した時、 nil を返します。
@raise Errno::EXXX 読み込みに失敗した場合に発生します。

//emlist[main.rb][ruby]{
ARGV << 'b.txt' << 'c.txt'
p gets #=> "hello\n"
p gets(nil) #=> "it\ncommon\n"
p gets("") #=> "ARGF\n\n"
p gets('、'...
...ct::ARGV を参照) をファイル名と\n# みなして、"
p gets #=> "それらのファイルを連結した 1 つの仮想ファイルを表すオブジェクトです。\n"
p gets #=> nil
p readline # end of file reached (EOFError)
//}

//emlist[b.txt][ruby]{
hello
it
common
//}

//emlist[c.t...
...xt][ruby]{
ARGF

# スクリプトに指定した引数 (Object::ARGV を参照) をファイル名と
# みなして、それらのファイルを連結した 1 つの仮想ファイルを表すオブジェクトです。
//}


@see $/,ARGF,Kernel.#readlines,Kernel.#readline...

Kernel$$stdin -> object (6143.0)

標準入力です。

...$stdin に代入すれば十分です。

//emlist[例][ruby]{
# 標準入力の入力元 /tmp/foo に変更
$stdin = File.open("/tmp/foo", "r")
gets
# 入力する
$stdin = STDIN # 元に戻す
//}

ただし、Kernel.#gets など、特定の組み込みメソッドは
$stdin オ...
...って、Kernel.#gets などが正しく動作するには、
$stdin オブジェクトに代入したオブジェクトが以下のメソッドを
正しく実装していなければいけません。

gets
, readline, readlines, getc, readchar, tell, seek,
pos=, rewind, fileno, to_io, eof,...
...byte,
binmode, closed?

//emlist[例][ruby]{
$stdin = Object.new
def $stdin.gets
"foo"
end
p gets() # => "foo"
//}

自プロセスだけでなく、子プロセスの標準入力もリダイレクトしたいときは
以下のように IO#reopen を使います。

//emlist[例][ruby]{
$std...

Kernel$$DEFAULT_INPUT -> IO (6107.0)

$< の別名

...$< の別名

require "English"
while line = $DEFAULT_INPUT.gets
p line
end
# end of sample.rb

ruby sample.rb < /etc/passwd
# => "hoge:x:500:501::/home/hoge:/bin/bash\n"
......

Kernel$$INPUT_RECORD_SEPARATOR -> String | nil (6107.0)

$/ の別名

...$/ の別名

require "English"

$INPUT_RECORD_SEPARATOR = '|'
array = []
while line = DATA.gets
array << line
end
p array #=> ["ugo|", "ego|", "fogo\n"]

__END__
ugo|ego|fogo...

Kernel$$LAST_PAREN_MATCH -> String | nil (6107.0)

$+ の別名

...c=(http:.+?)>")
r2 = Regexp.compile("<a href=(http|ftp).+?>(.+?)</a>")

while line = DATA.gets
[ r1, r2 ].each {|rep|
rep =~ line
p $+
}
end
__END__
<tr> <td><img src=http://localhost/a.jpg></td> <td>ikkou</td> <td><a href=http://localhost/link.html>link</a></td> </tr>...
...#enf of sample.rb

$ ruby sample.rb
"http://localhost/a.jpg"
"link"...

絞り込み条件を変える

Kernel$$RS -> String | nil (3107.0)

$/ の別名

...$/ の別名

require "English"

$INPUT_RECORD_SEPARATOR = '|'
array = []
while line = DATA.gets
array << line
end
p array #=> ["ugo|", "ego|", "fogo\n"]

__END__
ugo|ego|fogo...

Kernel$$_ -> String | nil (129.0)

最後に Kernel.#gets または Kernel.#readline で読み込んだ文字列です。 EOF に達した場合には、 nil になります。 (覚え方: Perlと同じ)

...最後に Kernel.#gets または Kernel.#readline で読み込んだ文字列です。
EOF に達した場合には、 nil になります。
(覚え方: Perlと同じ)

Kernel
.#print のような Perl 由来の幾つかのメソッドは、引数を省略すると代わりに $_ を利用します...
...ーカルです。
Ruby起動時の初期値は nil です。

@see Kernel.#print, Kernel.#gets, Kernel.#readline, Object::ARGF

=== 例
example.txt:
foo
bar
baz

このとき、コマンド ruby -e 'print while gets' example.txt は次を出力します
foo
bar
baz

ただし、このプロ...
...グラムは次のように書く方がよりRuby的です。
//emlist[例][ruby]{
ARGF.each do |line|
print line
end
//}...

Kernel.#readlines(rs = $/) -> [String] (123.0)

ARGFを Kernel.#gets(rs) でEOFまで読み込んで、その各行を要素としてもつ配列を返します。 行の区切りは引数 rs で指定した文字列になります。

...ARGFを Kernel.#gets(rs) でEOFまで読み込んで、その各行を要素としてもつ配列を返します。
行の区切りは引数 rs で指定した文字列になります。

rs に nil を指定すると行区切りなしとみなします。
空文字列 "" を指定すると連続...
...す。

//emlist[main.rb][ruby]{
ARGV << 'b.txt' << 'b.txt'
p readlines #=> ["hello\n", "it\n", "\n", "common\n", "hello\n", "it\n", "\n", "common\n"]

ARGV << 'b.txt' << 'b.txt'
p readlines(nil) #=> ["hello\nit\n\ncommon\n", "hello\nit\n\ncommon\n"]

ARGV << 'b.txt' << 'b.txt'
p readlines(""...
...) #=> ["hello\nit\n\n", "common\n", "hello\nit\n\n", "common\n"]

ARGV << 'b.txt' << 'b.txt'
p readlines('it') #=> ["hello\nit", "\n\ncommon\n", "hello\nit", "\n\ncommon\n"]
p readlines #=> []
//}

//emlist[b.txt][ruby]{
hello
it

common
//}

@see $/,ARGF,Kernel.#gets, Kernel.#readline...

Kernel#file(*args) { ... } -> Rake::FileTask (107.0)

ファイルタスクを定義します。

...ル名を指定します。

例:
file "config.cfg" => ["config.template"] do
open("config.cfg", "w") do |outfile|
open("config.template") do |infile|
while line = infile.gets
outfile.puts line
end
end
end
end

@see Rake::Task.define_task...
<< 1 2 > >>