種類
- インスタンスメソッド (459)
- 文書 (53)
- 特異メソッド (50)
- モジュール関数 (37)
クラス
- Enumerator (84)
-
Enumerator
:: Lazy (12) - Exception (24)
- Fiber (54)
-
Net
:: POP3 (12) - Object (86)
- Pathname (39)
- Proc (62)
- Thread (24)
モジュール
- Enumerable (100)
- Kernel (49)
キーワード
-
1
. 6 . 8から1 . 8 . 0への変更点(まとめ) (12) - == (12)
- === (12)
-
NEWS for Ruby 3
. 0 . 0 (5) - [] (24)
-
backtrace
_ locations (12) - call (12)
- compact (4)
-
create
_ makefile (12) - each (48)
-
each
_ entry (39) -
enum
_ for (24) - feed (12)
- find (24)
- foreach (12)
- lambda (18)
-
max
_ by (48) - new (26)
-
next
_ values (12) -
peek
_ values (12) - proc (19)
- raise (18)
- resume (12)
-
ruby 1
. 6 feature (12) -
sort
_ by (24) - tap (8)
- then (14)
-
thread
_ variable _ get (12) -
to
_ enum (24) - transfer (12)
-
yield
_ self (16) - クラス/メソッドの定義 (12)
- セキュリティモデル (12)
検索結果
先頭5件
-
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 }...