るりまサーチ

最速Rubyリファレンスマニュアル検索!
599件ヒット [501-599件を表示] (0.040秒)
トップページ > クエリ:@[x] > クエリ:yield[x]

別のキーワード

  1. _builtin yield
  2. object yield_self
  3. _builtin yield_self
  4. proc yield
  5. fiber yield

ライブラリ

クラス

モジュール

検索結果

<< < ... 4 5 6 >>

Proc#[](*arg) -> () (13.0)

手続きオブジェクトを実行してその結果を返します。

...|
case n
when 0 then 0
when 1 then 1
else
fib.(n - 2) + fib.(n - 1)
end
}
fib.(10) # => 55
//}



@
param arg 手続きオブジェクトに与える引数を指定します。

@
raise LocalJumpError Procを生成したメソッドからリターンしてしまった場合に発生し...

Proc#call(*arg) -> () (13.0)

手続きオブジェクトを実行してその結果を返します。

...|
case n
when 0 then 0
when 1 then 1
else
fib.(n - 2) + fib.(n - 1)
end
}
fib.(10) # => 55
//}



@
param arg 手続きオブジェクトに与える引数を指定します。

@
raise LocalJumpError Procを生成したメソッドからリターンしてしまった場合に発生し...

Enumerable#sort_by -> Enumerator (12.0)

ブロックの評価結果を <=> メソッドで比較することで、self を昇 順にソートします。ソートされた配列を新たに生成して返します。

...成して返します。

つまり、以下とほぼ同じ動作をします。

//emlist[例][ruby]{
class Array
def sort_by
self.map {|i| [yield(i), i] }.
sort {|a, b| a[0] <=> b[0] }.
map {|i| i[1]}
end
end
//}

Enumerable#sort と比較して sort_by が優れている点...
...できます。

//emlist[][ruby]{
i = 0
ary.sort_by {|v| [v, i += 1] }
//}

※ 比較結果が同じ要素は元の順序通りに並ぶソートを
「安定なソート (stable sort)」と言います。

ブロックを省略した場合は Enumerator を返します。


@
see Enumerable#sort...

Enumerable#sort_by {|item| ... } -> [object] (12.0)

ブロックの評価結果を <=> メソッドで比較することで、self を昇 順にソートします。ソートされた配列を新たに生成して返します。

...成して返します。

つまり、以下とほぼ同じ動作をします。

//emlist[例][ruby]{
class Array
def sort_by
self.map {|i| [yield(i), i] }.
sort {|a, b| a[0] <=> b[0] }.
map {|i| i[1]}
end
end
//}

Enumerable#sort と比較して sort_by が優れている点...
...できます。

//emlist[][ruby]{
i = 0
ary.sort_by {|v| [v, i += 1] }
//}

※ 比較結果が同じ要素は元の順序通りに並ぶソートを
「安定なソート (stable sort)」と言います。

ブロックを省略した場合は Enumerator を返します。


@
see Enumerable#sort...

Exception#==(other) -> bool (12.0)

自身と指定された other のクラスが同じであり、 message と backtrace が == メソッドで比較して 等しい場合に true を返します。そうでない場合に false を返します。

...message と backtrace が == メソッドで比較して
等しい場合に true を返します。そうでない場合に false を返します。

@
param other 自身と比較したいオブジェクトを指定します。
自身と異なるクラスのオブジェクトを指定...
...heck_long_month(month)
return if Date.new(2000, month, -1).day == 31
raise "#{month} is not long month"
end

def get_exception
return begin
yield

rescue => e
e
end
end

results = [2, 2, 4].map { |e | get_exception { check_long_month(e) } }
p results.map { |e| e.class }
# => [Runtim...

絞り込み条件を変える

Exception#backtrace_locations -> [Thread::Backtrace::Location] (12.0)

バックトレース情報を返します。Exception#backtraceに似ていますが、 Thread::Backtrace::Location の配列を返す点が異なります。

...et_exception
return begin
yield

rescue => e
e
end
end

e = get_exception { check_long_month(2) }
p e.backtrace_locations
# => ["test.rb:4:in `check_long_month'", "test.rb:15:in `block in <main>'", "test.rb:9:in `get_exception'", "test.rb:15:in `<main>'"]
//}

@
see Exception#backtrace...

Object#tap {|x| ... } -> self (12.0)

self を引数としてブロックを評価し、self を返します。

...目的です。

//emlist[][ruby]{
(1..10) .tap {|x| puts "original: #{x}" }
.to_a .tap {|x| puts "array: #{x}" }
.select {|x| x.even? } .tap {|x| puts "evens: #{x}" }
.map {|x| x*x } .tap {|x| puts "squares: #{x}" }
//}

@
see Object#yield_self...

Pathname#find -> Enumerator (12.0)

self 配下のすべてのファイルやディレクトリを 一つずつ引数 pathname に渡してブロックを実行します。

...すべてのファイルやディレクトリを
一つずつ引数 pathname に渡してブロックを実行します。

require 'find'
Find.find(self.to_s) {|f| yield Pathname.new(f)}

と同じです。

ブロックを省略した場合は Enumerator を返します。


@
see Find.#find...

Pathname#find {|pathname| ...} -> nil (12.0)

self 配下のすべてのファイルやディレクトリを 一つずつ引数 pathname に渡してブロックを実行します。

...すべてのファイルやディレクトリを
一つずつ引数 pathname に渡してブロックを実行します。

require 'find'
Find.find(self.to_s) {|f| yield Pathname.new(f)}

と同じです。

ブロックを省略した場合は Enumerator を返します。


@
see Find.#find...

Proc.new -> Proc (12.0)

ブロックをコンテキストとともにオブジェクト化して返します。

...ていません。呼び出し元のメソッドで指定されたブロック
を得たい場合は明示的に & 引数でうけるべきです。

@
raise ArgumentError スタック上にブロックがないのにブロックを省略した呼び出しを行ったときに発生します。

//em...
...][ruby]{
def foo
pr = Proc.new
pr.call(1)
end
foo {|arg| p arg }
# => 1
//}

これは以下と同じです。

//emlist[例][ruby]{
def foo
yield
(1)
end
foo {|arg| p arg }
# => 1
//}

呼び出し元のメソッドがブロックを伴わなければ、例外
ArgumentError が発生します...

絞り込み条件を変える

Proc.new { ... } -> Proc (12.0)

ブロックをコンテキストとともにオブジェクト化して返します。

...ていません。呼び出し元のメソッドで指定されたブロック
を得たい場合は明示的に & 引数でうけるべきです。

@
raise ArgumentError スタック上にブロックがないのにブロックを省略した呼び出しを行ったときに発生します。

//em...
...][ruby]{
def foo
pr = Proc.new
pr.call(1)
end
foo {|arg| p arg }
# => 1
//}

これは以下と同じです。

//emlist[例][ruby]{
def foo
yield
(1)
end
foo {|arg| p arg }
# => 1
//}

呼び出し元のメソッドがブロックを伴わなければ、例外
ArgumentError が発生します...

セキュリティモデル (12.0)

セキュリティモデル RubyにはCGI等のプログラミングを安全に行うことを助ける為に、セキュリティ 機構が備わっています。

...ベルで実行することが可
能です。

//emlist[例][ruby]{
def safe(level)
result = nil
Thread.start {
$SAFE = level
result = yield
}.join
result
end

lib = "insecure_library".taint # 外部から受け取った文字列(仮)
safe(1) { require lib } # $SAFE が 1 な...
...の扱い

* 拡張ライブラリではオブジェクトの汚染状態を適切に伝播させる必要があります。
* グローバルな状態を変更する場合や外部とのやりとりの前にセキュリティレベルを
チェックする必要があります。

@
see 37407...
...でセーフレベルを
戻す必要があります。

//emlist{
def safe(level)
result = nil
Thread.start do
$SAFE = level
result = yield
ensure
$SAFE = 0
end.join
result
end

lib = "insecure_library".taint # 外部から受け取った文字列(仮)
safe(1) { require lib }...
<< < ... 4 5 6 >>