るりまサーチ

最速Rubyリファレンスマニュアル検索!
2739件ヒット [1-100件を表示] (0.041秒)
トップページ > クエリ:Proc[x] > ライブラリ:ビルトイン[x]

別のキーワード

  1. _builtin to_proc
  2. _builtin proc
  3. proc curry
  4. httpserver mount_proc
  5. readline completion_proc

クラス

モジュール

オブジェクト

キーワード

検索結果

<< 1 2 3 ... > >>

Proc (38212.0)

ブロックをコンテキスト(ローカル変数のスコープやスタックフ レーム)とともにオブジェクト化した手続きオブジェクトです。

...手続きオブジェクトです。

Proc
は ローカル変数のスコープを導入しないことを除いて
名前のない関数のように使えます。ダイナミックローカル変数は
Proc
ローカルの変数として使えます。

Proc
がローカル変数のスコープ...
...[ruby]{
var = 1
$foo = Proc.new { var }
var = 2

def foo
$foo.call
end

p foo # => 2
//}

===[a:should_use_next] 手続きを中断して値を返す

手続きオブジェクトを中断して、呼出し元(呼び出しブロックでは yield、それ以外では Proc#call)
へジャン...
...ruby]{
def foo
f = Proc.new{
next 1
2 # この行に到達することはない
}
end

p foo().call #=> 1
//}

===[a:block] Proc オブジェクトをブロック付きメソッド呼び出しに使う

ブロック付きメソッドに対して Proc オブジェクト...

Proc#to_proc -> self (27115.0)

self を返します。

...self を返します。

//emlist[例][ruby]{
pr = proc {}
p pr == pr.to_proc # => true
//}...

Proc#ruby2_keywords -> proc (21179.0)

Marks the proc as passing keywords through a normal argument splat. This should only be called on procs that accept an argument splat (`*args`) but not explicit keywords or a keyword splat. It marks the proc such that if the proc is called with keyword arguments, the final hash argument is marked with a special flag such that if it is the final element of a normal argument splat to another method call, and that method call does not include explicit keywords or a keyword splat, the final element is interpreted as keywords. In other words, keywords will be passed through the proc to other methods.

...Marks the proc as passing keywords through a normal argument splat. This
should only be called on procs that accept an argument splat (`*args`)
but not explicit keywords or a keyword splat. It marks the proc such
that if the proc is called with keyword arguments, the final hash
argument is marked...
...ds or a keyword splat, the
final element is interpreted as keywords. In other words, keywords will
be passed through the proc to other methods.

This should only be used for procs that delegate keywords to another
method, and only for backwards compatibility with Ruby versions before
2.7.

This met...
...backwards compatibility. As it does not exist in Ruby versions
before 2.7, check that the proc responds to this method before calling
it. Also, be aware that if this method is removed, the behavior of the
proc
will change so that it does not pass through keywords.

//emlist[][ruby]{
module Mod
fo...

Proc#curry -> Proc (21162.0)

Procをカリー化します

...Procをカリー化します

カリー化したProcはいくつかの引数をとります。十分な数の引数が与えられると、元のProcに引数を渡し
て実行し、結果を返します。引数の個数が足りないときは、部分適用したカリー化Procを返します...
...Procオブジェクトを返します

//emlist[例][ruby]{
b = proc {|x, y, z| (x||0) + (y||0) + (z||0) }
p b.curry[1][2][3] #=> 6
p b.curry[1, 2][3, 4] #=> 6
p b.curry(5)[1][2][3][4][5] #=> 6
p b.curry(5)[1, 2][3, 4][5] #=> 6
p b.curry(1)[1] #=> 1

b = proc {|x,...
...0) + w.sum }
p b.curry[1][2][3] #=> 6
p b.curry[1, 2][3, 4] #=> 10
p b.curry(5)[1][2][3][4][5] #=> 15
p b.curry(5)[1, 2][3, 4][5] #=> 15
p b.curry(1) #=> wrong number of arguments (given 1, expected 3+)

b = proc { :foo }
p b.curry[] #=> :foo
//}...

Proc#curry(arity) -> Proc (21162.0)

Procをカリー化します

...Procをカリー化します

カリー化したProcはいくつかの引数をとります。十分な数の引数が与えられると、元のProcに引数を渡し
て実行し、結果を返します。引数の個数が足りないときは、部分適用したカリー化Procを返します...
...Procオブジェクトを返します

//emlist[例][ruby]{
b = proc {|x, y, z| (x||0) + (y||0) + (z||0) }
p b.curry[1][2][3] #=> 6
p b.curry[1, 2][3, 4] #=> 6
p b.curry(5)[1][2][3][4][5] #=> 6
p b.curry(5)[1, 2][3, 4][5] #=> 6
p b.curry(1)[1] #=> 1

b = proc {|x,...
...0) + w.sum }
p b.curry[1][2][3] #=> 6
p b.curry[1, 2][3, 4] #=> 10
p b.curry(5)[1][2][3][4][5] #=> 15
p b.curry(5)[1, 2][3, 4][5] #=> 15
p b.curry(1) #=> wrong number of arguments (given 1, expected 3+)

b = proc { :foo }
p b.curry[] #=> :foo
//}...

絞り込み条件を変える

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

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

...成した Proc を返します。

戻り値の Proc は可変長の引数を受け取ります。
戻り値の Proc を呼び出すと、まず受け取った引数を callable に渡して呼び出し、
その戻り値を self に渡して呼び出した結果を返します。

Proc
#>> とは呼...
...び出しの順序が逆になります。

@param callable Proc、Method、もしくは任意の call メソッドを持ったオブジェクト。

//emlist[例][ruby]{
f = proc { |x| x * x }
g = proc { |x| x + x }

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

//emlist[call を定義した...
...lass 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}" } << WordScanner << File.method(:read)
pipeline.call('testfile') # => word count: 4
//}

@see Method#<<,...

Proc#>>(callable) -> Proc (21161.0)

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

...成した Proc を返します。

戻り値の Proc は可変長の引数を受け取ります。
戻り値の Proc を呼び出すと、まず受け取った引数を self に渡して呼び出し、
その戻り値を callable に渡して呼び出した結果を返します。

Proc
#<< とは呼...
...び出しの順序が逆になります。

@param callable Proc、Method、もしくは任意の call メソッドを持ったオブジェクト。

//emlist[例][ruby]{
f = proc { |x| x * x }
g = proc { |x| x + x }

# (3 * 3) + (3 * 3)
p (f >> g).call(3) # => 18
//}

//emlist[call を定義した...
...lass WordScanner
def self.call(str)
str.scan(/\w+/)
end
end

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

pipeline = proc { |fname| File.read(fname) } >> WordScanner >> method(:p)
pipeline.call('testfile') # => ["Hello", "World", "Hello", "Ruby"]
//}

@see Method#<<,...

Proc#lambda? -> bool (21158.0)

手続きオブジェクトの引数の取扱が厳密であるならば true を返します。

...list[例][ruby]{
# lambda で生成した Proc オブジェクトでは true
lambda{}.lambda? # => true
# proc で生成した Proc オブジェクトでは false
proc
{}.lambda? # => false
# Proc.new で生成した Proc オブジェクトでは false
Proc
.new{}.lambda? # => false

# 以下、lam...
...bda?が偽である場合
# 余分な引数を無視する
proc
{|a,b| [a,b]}.call(1,2,3) # => [1,2]
# 足りない引数には nil が渡される
proc
{|a,b| [a,b]}.call(1) # => [1, nil]
# 配列1つだと展開される
proc
{|a,b| [a,b]}.call([1,2]) # => [1,2]
# lambdaの場合これらはすべて...
...生成される Proc は lambda? が偽となる
def n(&b) b.lambda? end
n {} # => false

# &が付いた実引数によるものは、lambda?が元の Procオブジェクトから
# 引き継がれる
lambda(&lambda {}).lambda? #=> true
proc
(&lambda {}).lambda? #=> true
Proc
.new(&lambda {}).l...

Proc.new -> Proc (21158.0)

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

...OSE = true のときには警告メッセージ
「warning: Capturing the given block using Proc.new is deprecated; use `&block` instead」
が出力され、Ruby 3.0 では
ArgumentError (tried to create Proc object without a block)
が発生します。

ブロックを指定しなければ、こ...
...上にブロックがないのにブロックを省略した呼び出しを行ったときに発生します。

//emlist[例][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 ar...
...entError が発生します。

//emlist[例][ruby]{
def foo
Proc
.new
end
foo
# => -:2:in `new': tried to create Proc object without a block (ArgumentError)
# from -:2:in `foo'
# from -:4:in `<main>'
//}

Proc
.new は、Proc#initialize が定義されていれば
オブジェクト...

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

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

...OSE = true のときには警告メッセージ
「warning: Capturing the given block using Proc.new is deprecated; use `&block` instead」
が出力され、Ruby 3.0 では
ArgumentError (tried to create Proc object without a block)
が発生します。

ブロックを指定しなければ、こ...
...上にブロックがないのにブロックを省略した呼び出しを行ったときに発生します。

//emlist[例][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 ar...
...entError が発生します。

//emlist[例][ruby]{
def foo
Proc
.new
end
foo
# => -:2:in `new': tried to create Proc object without a block (ArgumentError)
# from -:2:in `foo'
# from -:4:in `<main>'
//}

Proc
.new は、Proc#initialize が定義されていれば
オブジェクト...
...行ったときに発生します。

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

//emlist[][ruby]{
Proc
.new # => -e:1:in `new': tried to create Proc object without a block (ArgumentError)
//}

Proc
.new は、Proc#initialize が定義されていれば
オブジェクトの...
...初期化のためにこれを呼び出します。このことを
除けば、Kernel.#proc と同じです。...

絞り込み条件を変える

Proc#parameters(lambda: nil) -> [object] (21042.0)

Proc オブジェクトの引数の情報を返します。

...Proc オブジェクトの引数の情報を返します。

Proc
オブジェクトが引数を取らなければ空の配列を返します。引数を取る場合は、配列の配列を返し、
各配列の要素は引数の種類に対応した以下のような Symbol と、引数名を表す...
...
: :block
& で指定されたブロック引数

@param lambda true なら lambda として扱ったとき、false なら lambda ではない Proc として
扱ったときの引数の情報を返します。

//emlist[例][ruby]{
prc = lambda{|x, y=42, *other, k_x:, k_y: 42, **k_...
..._other], [:block, :b
//}

//emlist[lambda: の例][ruby]{
prc = proc{|x, y=42, *other|}
p prc.parameters # => x], [:opt, :y], [:rest, :other
prc = lambda{|x, y=42, *other|}
p prc.parameters # => x], [:opt, :y], [:rest, :other
prc = proc{|x, y=42, *other|}
p prc.parameters(lambda: true) # => x], [...

Proc#binding -> Binding (21036.0)

Proc オブジェクトが保持するコンテキストを Binding オブジェクトで返します。

...
Proc
オブジェクトが保持するコンテキストを
Binding オブジェクトで返します。

//emlist[例][ruby]{
def fred(param)
proc
{}
end

sample_proc = fred(99)
eval("param", sample_proc.binding) # => 99
//}...
<< 1 2 3 ... > >>