るりまサーチ

最速Rubyリファレンスマニュアル検索!
160件ヒット [1-100件を表示] (0.040秒)

別のキーワード

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

クラス

キーワード

検索結果

<< 1 2 > >>

Proc#<<(callable) -> Proc (18127.0)

self と引数を合成した Proc を返します。

...(f << g).call(3) # => 36
//}

//emlist[call を定義したオブジェクトを渡す例][ruby]{
class WordScanner
def self.call(str)
str.scan(/\w+/)
end
end

File.write('testfile', <<~TEXT)
Hello, World!
Hello, Ruby!
TEXT

pipeline = proc { |data| puts "word count: #{data.size}" } << W...
...ordScanner << File.method(:read)
pipeline.call('testfile') # => word count: 4
//}

@see Method#<<, Method#>>...

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

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

...

//emlist[例][ruby]{
p 'abcdefg'.gsub(/def/, '!!') # => "abc!!g"
p 'abcabc'.gsub(/b/, '<<\&>>') # => "a<<b>>ca<<b>>c"
p 'xxbbxbb'.gsub(/x+(b+)/, 'X<<\1>>') # => "X<<bb>>X<<bb>>"
p '2.5'.gsub('.', ',') # => "2,5"
//}

注意:

第 2 引数 replace に $1 を埋め込んでも...
...ックスラッシュを倍にするときによくある間違い][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!...

String#sub(pattern, replace) -> String (45.0)

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

...る文字列

//emlist[例][ruby]{
p 'abcdefg'.sub(/def/, '!!') # => "abc!!g"
p 'abcabc'.sub(/b/, '<<\&>>') # => "a<<b>>cabc"
p 'xxbbxbb'.sub(/x+(b+)/, 'X<<\1>>') # => "X<<bb>>xbb"
//}

注意:

第 2 引数 replace に $1 を埋め込んでも意図した結果にはなりませ...
...ックスラッシュを倍にするときによくある間違い][ruby]{
puts
'\n'.sub(/\\/, "\\\\") # => \n # NG
puts
'\n'.sub(/\\/, '\\\\') # => \n # NG
puts
'\n'.sub(/\\/, "\\\\\\\\") # => \\n # OK
puts
'\n'.sub(/\\/, '\\\\\\\\') # => \\n # OK
//}

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

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

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

@see String#gsub...

Object#initialize_copy(obj) -> object (21.0)

(拡張ライブラリによる) ユーザ定義クラスのオブジェクトコピーの初期化メソッド。

...異メソッドのコピーも行います。

//emlist[][ruby]{
obj = Object.new
class <<obj
attr_accessor :foo
def bar
:bar
end
end

def check(obj)
puts
"instance variables: #{obj.inspect}"
puts
"tainted?: #{obj.tainted?}"
print "singleton methods: "
begin
p obj.bar
rescue...

Regexp#match(str, pos = 0) -> MatchData | nil (21.0)

指定された文字列 str に対して位置 pos から自身が表す正規表現によるマッ チングを行います。マッチした場合には結果を MatchData オブジェクトで返し ます。 マッチしなかった場合 nil を返します。

...しなかった場合は nil を返します。

//emlist[例][ruby]{
results = []
/((.)\2)/.match("foo") {|m| results << m[0] } # => ["oo"]
/((.)\2)/.match("bar") {|m| results << m[0] } # => nil
results # => ["oo"]
//}

@param str 文字列を指定します。str との正規表現マッチを行...
...pos から行うよう制御できます(pos のデフォルト値は 0)。

//emlist[例][ruby]{
reg = Regexp.new("foo")

if reg.match("foobar")
puts
"match"
end
# => match

p reg.match("foobar") # => #<MatchData:0x29403fc>
p reg.match("bar") # => nil

p /(foo)(bar)(baz)/.match("foobarbaz").t...

絞り込み条件を変える

Regexp#match(str, pos = 0) {|m| ... } -> object | nil (21.0)

指定された文字列 str に対して位置 pos から自身が表す正規表現によるマッ チングを行います。マッチした場合には結果を MatchData オブジェクトで返し ます。 マッチしなかった場合 nil を返します。

...しなかった場合は nil を返します。

//emlist[例][ruby]{
results = []
/((.)\2)/.match("foo") {|m| results << m[0] } # => ["oo"]
/((.)\2)/.match("bar") {|m| results << m[0] } # => nil
results # => ["oo"]
//}

@param str 文字列を指定します。str との正規表現マッチを行...
...pos から行うよう制御できます(pos のデフォルト値は 0)。

//emlist[例][ruby]{
reg = Regexp.new("foo")

if reg.match("foobar")
puts
"match"
end
# => match

p reg.match("foobar") # => #<MatchData:0x29403fc>
p reg.match("bar") # => nil

p /(foo)(bar)(baz)/.match("foobarbaz").t...

BasicObject#singleton_method_removed(name) -> object (15.0)

特異メソッドが Module#remove_method に より削除された時にインタプリタから呼び出されます。

...Symbol で渡されます。

//emlist[例][ruby]{
class Foo
def singleton_method_removed(name)
puts
"singleton method \"#{name}\" was removed"
end
end

obj = Foo.new
def obj.foo
end

class << obj
remove_method :foo
end

#=> singleton method "foo" was removed
//}

@see Module#method_remov...

BasicObject#singleton_method_undefined(name) -> object (15.0)

特異メソッドが Module#undef_method または undef により未定義にされた時にインタプリタから呼び出されます。

...渡されます。

//emlist[例][ruby]{
class Foo
def singleton_method_undefined(name)
puts
"singleton method \"#{name}\" was undefined"
end
end

obj = Foo.new
def obj.foo
end
def obj.bar
end

class << obj
undef_method :foo
end
obj.instance_eval {undef bar}

#=> singleton method "foo" was...

Enumerator#with_object(obj) -> Enumerator (15.0)

繰り返しの各要素に obj を添えてブロックを繰り返し、obj を返り値として返します。

...2 と呼びだす enumeratorを作る
to_three = Enumerator.new do |y|
3.times do |x|
y << x
end
end

to_three_with_string = to_three.with_object("foo")
to_three_with_string.each do |x,string|
puts
"#{string}: #{x}"
end
# => foo:0
# => foo:1
# => foo:2
//}

@param obj 繰り返しの各要...

Enumerator#with_object(obj) {|(*args), memo_obj| ... } -> object (15.0)

繰り返しの各要素に obj を添えてブロックを繰り返し、obj を返り値として返します。

...2 と呼びだす enumeratorを作る
to_three = Enumerator.new do |y|
3.times do |x|
y << x
end
end

to_three_with_string = to_three.with_object("foo")
to_three_with_string.each do |x,string|
puts
"#{string}: #{x}"
end
# => foo:0
# => foo:1
# => foo:2
//}

@param obj 繰り返しの各要...

絞り込み条件を変える

<< 1 2 > >>