種類
- インスタンスメソッド (37)
- モジュール関数 (35)
- 文書 (11)
- クラス (11)
ライブラリ
- ビルトイン (83)
クラス
- Object (37)
モジュール
- Kernel (35)
キーワード
- Proc (11)
- lambda (17)
- proc (18)
- then (12)
-
yield
_ self (14) - 手続きオブジェクトの挙動の詳細 (11)
検索結果
先頭5件
-
Object
# tap {|x| . . . } -> self (21225.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}" }
//}......目的です。
//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... -
Kernel
. # lambda -> Proc (6260.0) -
与えられたブロックから手続きオブジェクト (Proc のインスタンス) を生成して返します。Proc.new に近い働きをします。
...い lambda は Ruby 2.6 までは警告メッセージ
「warning: tried to create Proc object without a block」
が出力され、Ruby 2.7 では
ArgumentError (tried to create Proc object without a block)
が発生します。
ブロックを指定しない proc は、Ruby 2.7 では
$VERBOSE =......[ruby]{
def test_proc
f = Proc.new { return :from_proc }
f.call
return :from_method
end
def test_lambda
f = lambda { return :from_lambda }
f.call
return :from_method
end
def test_block
tap { return :from_block }
return :from_method
end
p test_proc() #=> :from_proc
p test_lambda(......通り lambda で生成した手続きオブジェクトはメソッドと同じように振る舞う
ことを意図されているため、例外 LocalJumpError は発生しません。
//emlist[例][ruby]{
def foo
Proc.new { return }
end
foo.call
# => in `call': return from proc-closure (Loca... -
Kernel
. # lambda { . . . } -> Proc (6260.0) -
与えられたブロックから手続きオブジェクト (Proc のインスタンス) を生成して返します。Proc.new に近い働きをします。
...い lambda は Ruby 2.6 までは警告メッセージ
「warning: tried to create Proc object without a block」
が出力され、Ruby 2.7 では
ArgumentError (tried to create Proc object without a block)
が発生します。
ブロックを指定しない proc は、Ruby 2.7 では
$VERBOSE =......[ruby]{
def test_proc
f = Proc.new { return :from_proc }
f.call
return :from_method
end
def test_lambda
f = lambda { return :from_lambda }
f.call
return :from_method
end
def test_block
tap { return :from_block }
return :from_method
end
p test_proc() #=> :from_proc
p test_lambda(......通り lambda で生成した手続きオブジェクトはメソッドと同じように振る舞う
ことを意図されているため、例外 LocalJumpError は発生しません。
//emlist[例][ruby]{
def foo
Proc.new { return }
end
foo.call
# => in `call': return from proc-closure (Loca......また、lambda に & 引数を渡すのは推奨されません。& 引数ではなくてブロック記法で記述する必要があります。
& 引数を渡した lambda は Warning[:deprecated] = true のときに警告メッセージ
「warning: lambda without a literal block is deprecat... -
Object
# then {|x| . . . } -> object (3212.0) -
self を引数としてブロックを評価し、ブロックの結果を返します。
...uby]{
3.next.then {|x| x**x }.to_s # => "256"
"my string".yield_self {|s| s.upcase } # => "MY STRING"
//}
値をメソッドチェインのパイプラインに次々と渡すのは良い使い方です。
//emlist[メソッドチェインのパイプライン][ruby]{
require 'open-u......返します。
例えば条件によって値を捨てるのに使えます。
//emlist[][ruby]{
# 条件にあうので何もしない
1.yield_self.detect(&:odd?) # => 1
# 条件に合わないので値を捨てる
2.yield_self.detect(&:odd?) # => nil
//}
@see Object#tap... -
Object
# yield _ self {|x| . . . } -> object (3212.0) -
self を引数としてブロックを評価し、ブロックの結果を返します。
...uby]{
"my string".yield_self {|s| s.upcase } # => "MY STRING"
3.next.yield_self {|x| x**x }.to_s # => "256"
//}
値をメソッドチェインのパイプラインに次々と渡すのは良い使い方です。
//emlist[メソッドチェインのパイプライン][ruby]{
require 'open-u......返します。
例えば条件によって値を捨てるのに使えます。
//emlist[][ruby]{
# 条件にあうので何もしない
1.yield_self.detect(&:odd?) # => 1
# 条件に合わないので値を捨てる
2.yield_self.detect(&:odd?) # => nil
//}
@see Object#tap......uby]{
3.next.then {|x| x**x }.to_s # => "256"
"my string".yield_self {|s| s.upcase } # => "MY STRING"
//}
値をメソッドチェインのパイプラインに次々と渡すのは良い使い方です。
//emlist[メソッドチェインのパイプライン][ruby]{
require 'open-u... -
Kernel
. # proc -> Proc (3160.0) -
与えられたブロックから手続きオブジェクト (Proc のインスタンス) を生成して返します。Proc.new に近い働きをします。
...い lambda は Ruby 2.6 までは警告メッセージ
「warning: tried to create Proc object without a block」
が出力され、Ruby 2.7 では
ArgumentError (tried to create Proc object without a block)
が発生します。
ブロックを指定しない proc は、Ruby 2.7 では
$VERBOSE =......[ruby]{
def test_proc
f = Proc.new { return :from_proc }
f.call
return :from_method
end
def test_lambda
f = lambda { return :from_lambda }
f.call
return :from_method
end
def test_block
tap { return :from_block }
return :from_method
end
p test_proc() #=> :from_proc
p test_lambda(......通り lambda で生成した手続きオブジェクトはメソッドと同じように振る舞う
ことを意図されているため、例外 LocalJumpError は発生しません。
//emlist[例][ruby]{
def foo
Proc.new { return }
end
foo.call
# => in `call': return from proc-closure (Loca... -
Kernel
. # proc { . . . } -> Proc (3160.0) -
与えられたブロックから手続きオブジェクト (Proc のインスタンス) を生成して返します。Proc.new に近い働きをします。
...い lambda は Ruby 2.6 までは警告メッセージ
「warning: tried to create Proc object without a block」
が出力され、Ruby 2.7 では
ArgumentError (tried to create Proc object without a block)
が発生します。
ブロックを指定しない proc は、Ruby 2.7 では
$VERBOSE =......[ruby]{
def test_proc
f = Proc.new { return :from_proc }
f.call
return :from_method
end
def test_lambda
f = lambda { return :from_lambda }
f.call
return :from_method
end
def test_block
tap { return :from_block }
return :from_method
end
p test_proc() #=> :from_proc
p test_lambda(......通り lambda で生成した手続きオブジェクトはメソッドと同じように振る舞う
ことを意図されているため、例外 LocalJumpError は発生しません。
//emlist[例][ruby]{
def foo
Proc.new { return }
end
foo.call
# => in `call': return from proc-closure (Loca......また、lambda に & 引数を渡すのは推奨されません。& 引数ではなくてブロック記法で記述する必要があります。
& 引数を渡した lambda は Warning[:deprecated] = true のときに警告メッセージ
「warning: lambda without a literal block is deprecat... -
Object
# then -> Enumerator (3112.0) -
self を引数としてブロックを評価し、ブロックの結果を返します。
...uby]{
3.next.then {|x| x**x }.to_s # => "256"
"my string".yield_self {|s| s.upcase } # => "MY STRING"
//}
値をメソッドチェインのパイプラインに次々と渡すのは良い使い方です。
//emlist[メソッドチェインのパイプライン][ruby]{
require 'open-u......返します。
例えば条件によって値を捨てるのに使えます。
//emlist[][ruby]{
# 条件にあうので何もしない
1.yield_self.detect(&:odd?) # => 1
# 条件に合わないので値を捨てる
2.yield_self.detect(&:odd?) # => nil
//}
@see Object#tap... -
Object
# yield _ self -> Enumerator (3112.0) -
self を引数としてブロックを評価し、ブロックの結果を返します。
...uby]{
"my string".yield_self {|s| s.upcase } # => "MY STRING"
3.next.yield_self {|x| x**x }.to_s # => "256"
//}
値をメソッドチェインのパイプラインに次々と渡すのは良い使い方です。
//emlist[メソッドチェインのパイプライン][ruby]{
require 'open-u......返します。
例えば条件によって値を捨てるのに使えます。
//emlist[][ruby]{
# 条件にあうので何もしない
1.yield_self.detect(&:odd?) # => 1
# 条件に合わないので値を捨てる
2.yield_self.detect(&:odd?) # => nil
//}
@see Object#tap......uby]{
3.next.then {|x| x**x }.to_s # => "256"
"my string".yield_self {|s| s.upcase } # => "MY STRING"
//}
値をメソッドチェインのパイプラインに次々と渡すのは良い使い方です。
//emlist[メソッドチェインのパイプライン][ruby]{
require 'open-u... -
手続きオブジェクトの挙動の詳細 (3060.0)
-
手続きオブジェクトの挙動の詳細 * def * should_use_next * block * lambda_proc * orphan
...手続きオブジェクトの挙動の詳細
* def
* should_use_next
* block
* lambda_proc
* orphan
===[a:def] 手続きオブジェクトとは
手続きオブジェクトとはブロックをコンテキスト(ローカル変数のスコープやスタックフレーム)と
ともにオ......[ruby]{
def test_proc
f = Proc.new { return :from_proc }
f.call
return :from_method
end
def test_lambda
f = lambda { return :from_lambda }
f.call
return :from_method
end
def test_block
tap { return :from_block }
return :from_method
end
p test_proc() #=> :from_proc
p test_lambda(......通り lambda で生成した手続きオブジェクトはメソッドと同じように振る舞う
ことを意図されているため、例外 LocalJumpError は発生しません。
//emlist[例][ruby]{
def foo
Proc.new { return }
end
foo.call
# => in `call': return from proc-closure (Loca... -
Proc (60.0)
-
ブロックをコンテキスト(ローカル変数のスコープやスタックフ レーム)とともにオブジェクト化した手続きオブジェクトです。
...変数のスコープを保持していることは以下の例で
変数 var を参照できていることからわかります。
//emlist[例][ruby]{
var = 1
$foo = Proc.new { var }
var = 2
def foo
$foo.call
end
p foo # => 2
//}
===[a:should_use_next] 手続きを中断して値を......[ruby]{
def test_proc
f = Proc.new { return :from_proc }
f.call
return :from_method
end
def test_lambda
f = lambda { return :from_lambda }
f.call
return :from_method
end
def test_block
tap { return :from_block }
return :from_method
end
p test_proc() #=> :from_proc
p test_lambda(......通り lambda で生成した手続きオブジェクトはメソッドと同じように振る舞う
ことを意図されているため、例外 LocalJumpError は発生しません。
//emlist[例][ruby]{
def foo
Proc.new { return }
end
foo.call
# => in `call': return from proc-closure (Loca...