るりまサーチ

最速Rubyリファレンスマニュアル検索!
33件ヒット [1-33件を表示] (0.029秒)
トップページ > クエリ:|[x] > クエリ:-[x] > クエリ:$&[x]

別のキーワード

  1. _builtin |
  2. set |
  3. ipaddr |
  4. array |
  5. integer |

ライブラリ

モジュール

キーワード

検索結果

Kernel$$& -> String | nil (18308.0)

現在のスコープで最後に成功した正規表現のパターンマッチでマッチした文字列です。 最後のマッチが失敗していた場合には nil となります。

...プかつスレッドローカル、読み取り専用です。
Ruby起動時の初期値は nil です。

//emlist[例][ruby]{
str = '<p><a href="http://example.com">example.com</a></p>'
if %r[<a href="(.*?)">(.*?)</a>] =~ str
p $&
end
#=> "<a href=\"http://example.com\">example.com</a>"
//}...

Enumerable#chunk {|elt| ... } -> Enumerator (405.0)

要素を前から順にブロックで評価し、その結果によって 要素をチャンクに分けた(グループ化した)要素を持つ Enumerator を返します。

...つ配列のペアを各要素とします。
そのため、eachだと以下のようになります。

//emlist[][ruby]{
enum.chunk {|elt| key }.each {|key, ary| do_something }
//}

例として、整数列を連続する奇数/偶数に分ける例を見てみます。
「n.even?」の値が切...
...エンコーディングは実際のファイルに合わせてください。
open("/usr/share/dict/words", "r:iso-8859-1") {|f|
f.chunk {|line| line[0].upcase }.each {|ch, lines| p [ch, lines.length] }
}
# => ["A", 17096]
# ["B", 11070]
# ["C", 19901]
# ["D", 10896]
# ...
//}

さら...
...のみ
チャンク化し、それ以外は素通しします。

//emlist[例][ruby]{
pat = /\A[A-Z][A-Za-z0-9_]+\#/
open(filename) {|f|
f.chunk {|line| pat =~ line ? $& : :_alone }.each {|key, lines|
if key != :_alone
print lines.sort.join('')
else
print lines.join('')
en...

クラス/メソッドの定義 (114.0)

クラス/メソッドの定義 * クラス/メソッドの定義: * class * singleton_class * module * method * operator * nest_method * eval_method * singleton_method * class_method * limit * 定義に関する操作: * alias * undef * defined

...い場合は nil を返します。

===[a:method] メソッド定義

//emlist[例][ruby]{
def fact(n)
if n == 1 then
1
else
n * fact(n-1)
end
end
//}

文法:

def メソッド名 ['(' [arg0 ['=' default0]] ... [',' '*' rest_args [, post ...]] [',' key1: [val1]] ... [',' '*...
...トとブロックの
# 両方を受け付けるイテレータを定義する例
def foo(pr = nil, &block)
pr = pr || block
pr.call(1,2)
end
foo(proc {|a,b| p [a,b]})
foo {|a,b| p [a,b]}

# ブロック引数を使う
def baz(&block)
if block
block.call(1,2)
end
end
//}

またメソッ...
...f._val + other._val
end
end
//}

=== 定義に関する操作

====[a:alias] alias

//emlist[例][ruby]{
alias foo bar
alias :foo :bar
alias $MATCH $&
//}

文法:

alias 新メソッド名 旧メソッド名
alias 新グローバル変数名 旧グローバル変数名

メソ...