rbs 2.0.0 → 2.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/comments.yml +34 -0
- data/.github/workflows/ruby.yml +5 -0
- data/CHANGELOG.md +82 -0
- data/README.md +6 -1
- data/Rakefile +56 -21
- data/core/array.rbs +2866 -1086
- data/core/basic_object.rbs +150 -30
- data/core/binding.rbs +33 -0
- data/core/builtin.rbs +4 -4
- data/core/class.rbs +43 -5
- data/core/comparable.rbs +57 -0
- data/core/complex.rbs +170 -4
- data/core/constants.rbs +51 -0
- data/core/deprecated.rbs +7 -0
- data/core/dir.rbs +305 -20
- data/core/encoding.rbs +1214 -77
- data/core/enumerable.rbs +2173 -234
- data/core/enumerator.rbs +448 -182
- data/core/env.rbs +448 -1
- data/core/errno.rbs +1 -10
- data/core/errors.rbs +152 -2
- data/core/exception.rbs +201 -127
- data/core/false_class.rbs +27 -0
- data/core/fiber.rbs +118 -37
- data/core/fiber_error.rbs +8 -9
- data/core/file.rbs +1060 -142
- data/core/file_test.rbs +287 -32
- data/core/float.rbs +776 -300
- data/core/gc.rbs +185 -34
- data/core/global_variables.rbs +5 -1
- data/core/hash.rbs +1582 -649
- data/core/integer.rbs +974 -204
- data/core/io/buffer.rbs +710 -0
- data/core/io/wait.rbs +29 -8
- data/core/io.rbs +2438 -417
- data/core/kernel.rbs +2319 -318
- data/core/marshal.rbs +37 -2
- data/core/match_data.rbs +123 -6
- data/core/math.rbs +126 -6
- data/core/method.rbs +226 -102
- data/core/module.rbs +421 -45
- data/core/nil_class.rbs +64 -0
- data/core/numeric.rbs +620 -142
- data/core/object.rbs +453 -81
- data/core/object_space.rbs +92 -2
- data/core/proc.rbs +482 -285
- data/core/process.rbs +443 -34
- data/core/ractor.rbs +232 -9
- data/core/random.rbs +151 -52
- data/core/range.rbs +885 -160
- data/core/rational.rbs +122 -6
- data/core/rb_config.rbs +14 -4
- data/core/refinement.rbs +44 -0
- data/core/regexp.rbs +156 -14
- data/core/ruby_vm.rbs +42 -3
- data/core/signal.rbs +78 -39
- data/core/string.rbs +2123 -567
- data/core/string_io.rbs +204 -0
- data/core/struct.rbs +283 -28
- data/core/symbol.rbs +304 -30
- data/core/thread.rbs +1288 -688
- data/core/thread_group.rbs +66 -10
- data/core/time.rbs +643 -217
- data/core/trace_point.rbs +100 -12
- data/core/true_class.rbs +24 -0
- data/core/unbound_method.rbs +73 -7
- data/core/warning.rbs +37 -12
- data/docs/CONTRIBUTING.md +40 -34
- data/docs/stdlib.md +3 -102
- data/docs/syntax.md +54 -11
- data/ext/rbs_extension/extconf.rb +1 -0
- data/ext/rbs_extension/lexer.h +5 -0
- data/ext/rbs_extension/lexstate.c +6 -0
- data/ext/rbs_extension/parser.c +85 -10
- data/ext/rbs_extension/ruby_objs.c +4 -2
- data/ext/rbs_extension/ruby_objs.h +2 -2
- data/goodcheck.yml +0 -11
- data/lib/rbs/annotate/annotations.rb +197 -0
- data/lib/rbs/annotate/formatter.rb +80 -0
- data/lib/rbs/annotate/rdoc_annotator.rb +398 -0
- data/lib/rbs/annotate/rdoc_source.rb +120 -0
- data/lib/rbs/annotate.rb +6 -0
- data/lib/rbs/ast/members.rb +21 -13
- data/lib/rbs/buffer.rb +17 -11
- data/lib/rbs/cli.rb +48 -1
- data/lib/rbs/definition_builder/method_builder.rb +28 -16
- data/lib/rbs/definition_builder.rb +6 -2
- data/lib/rbs/environment.rb +8 -4
- data/lib/rbs/location_aux.rb +12 -0
- data/lib/rbs/namespace.rb +1 -1
- data/lib/rbs/prototype/rb.rb +12 -0
- data/lib/rbs/type_alias_regularity.rb +6 -4
- data/lib/rbs/type_name.rb +1 -1
- data/lib/rbs/types.rb +1 -1
- data/lib/rbs/validator.rb +6 -0
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs/writer.rb +54 -4
- data/lib/rbs.rb +0 -2
- data/schema/typeParam.json +3 -3
- data/sig/annotate/annotations.rbs +102 -0
- data/sig/annotate/formatter.rbs +24 -0
- data/sig/annotate/rdoc_annotater.rbs +82 -0
- data/sig/annotate/rdoc_source.rbs +30 -0
- data/sig/buffer.rbs +6 -2
- data/sig/cli.rbs +2 -0
- data/sig/collection/{collections.rbs → sources.rbs} +0 -0
- data/sig/location.rbs +6 -0
- data/sig/members.rbs +24 -18
- data/sig/method_builder.rbs +5 -4
- data/sig/method_types.rbs +5 -1
- data/sig/polyfill.rbs +78 -0
- data/sig/validator.rbs +3 -1
- data/sig/writer.rbs +79 -2
- data/stdlib/abbrev/0/abbrev.rbs +6 -0
- data/stdlib/abbrev/0/array.rbs +26 -0
- data/stdlib/base64/0/base64.rbs +31 -0
- data/stdlib/benchmark/0/benchmark.rbs +74 -3
- data/stdlib/bigdecimal/0/big_decimal.rbs +614 -165
- data/stdlib/bigdecimal-math/0/big_math.rbs +41 -64
- data/stdlib/cgi/0/core.rbs +649 -21
- data/stdlib/coverage/0/coverage.rbs +164 -2
- data/stdlib/csv/0/csv.rbs +2862 -398
- data/stdlib/date/0/date.rbs +483 -25
- data/stdlib/date/0/date_time.rbs +187 -12
- data/stdlib/dbm/0/dbm.rbs +152 -17
- data/stdlib/digest/0/digest.rbs +146 -0
- data/stdlib/erb/0/erb.rbs +65 -245
- data/stdlib/fiber/0/fiber.rbs +73 -91
- data/stdlib/fileutils/0/fileutils.rbs +301 -1
- data/stdlib/find/0/find.rbs +9 -0
- data/stdlib/forwardable/0/forwardable.rbs +65 -1
- data/stdlib/io-console/0/io-console.rbs +227 -15
- data/stdlib/ipaddr/0/ipaddr.rbs +161 -0
- data/stdlib/json/0/json.rbs +1147 -145
- data/stdlib/logger/0/formatter.rbs +24 -0
- data/stdlib/logger/0/log_device.rbs +64 -0
- data/stdlib/logger/0/logger.rbs +165 -13
- data/stdlib/logger/0/period.rbs +10 -0
- data/stdlib/logger/0/severity.rbs +26 -0
- data/stdlib/monitor/0/monitor.rbs +163 -0
- data/stdlib/mutex_m/0/mutex_m.rbs +35 -6
- data/stdlib/net-http/0/manifest.yaml +1 -0
- data/stdlib/net-http/0/net-http.rbs +1513 -683
- data/stdlib/nkf/0/nkf.rbs +372 -0
- data/stdlib/objspace/0/objspace.rbs +149 -90
- data/stdlib/openssl/0/openssl.rbs +8108 -71
- data/stdlib/optparse/0/optparse.rbs +487 -19
- data/stdlib/pathname/0/pathname.rbs +425 -124
- data/stdlib/prettyprint/0/prettyprint.rbs +120 -99
- data/stdlib/prime/0/integer-extension.rbs +20 -2
- data/stdlib/prime/0/prime.rbs +88 -21
- data/stdlib/pstore/0/pstore.rbs +102 -0
- data/stdlib/pty/0/pty.rbs +64 -14
- data/stdlib/resolv/0/resolv.rbs +420 -31
- data/stdlib/rubygems/0/basic_specification.rbs +4 -1
- data/stdlib/rubygems/0/config_file.rbs +33 -1
- data/stdlib/rubygems/0/dependency_installer.rbs +4 -3
- data/stdlib/rubygems/0/installer.rbs +13 -1
- data/stdlib/rubygems/0/path_support.rbs +4 -1
- data/stdlib/rubygems/0/platform.rbs +5 -1
- data/stdlib/rubygems/0/request_set.rbs +44 -2
- data/stdlib/rubygems/0/requirement.rbs +65 -2
- data/stdlib/rubygems/0/rubygems.rbs +407 -0
- data/stdlib/rubygems/0/source_list.rbs +13 -0
- data/stdlib/rubygems/0/specification.rbs +21 -1
- data/stdlib/rubygems/0/stream_ui.rbs +3 -1
- data/stdlib/rubygems/0/uninstaller.rbs +8 -1
- data/stdlib/rubygems/0/version.rbs +60 -157
- data/stdlib/securerandom/0/securerandom.rbs +44 -0
- data/stdlib/set/0/set.rbs +423 -109
- data/stdlib/shellwords/0/shellwords.rbs +55 -77
- data/stdlib/singleton/0/singleton.rbs +20 -0
- data/stdlib/socket/0/addrinfo.rbs +210 -9
- data/stdlib/socket/0/basic_socket.rbs +103 -11
- data/stdlib/socket/0/ip_socket.rbs +31 -9
- data/stdlib/socket/0/socket.rbs +586 -38
- data/stdlib/socket/0/tcp_server.rbs +22 -2
- data/stdlib/socket/0/tcp_socket.rbs +12 -1
- data/stdlib/socket/0/udp_socket.rbs +25 -2
- data/stdlib/socket/0/unix_server.rbs +22 -2
- data/stdlib/socket/0/unix_socket.rbs +45 -5
- data/stdlib/strscan/0/string_scanner.rbs +210 -9
- data/stdlib/tempfile/0/tempfile.rbs +58 -10
- data/stdlib/time/0/time.rbs +208 -116
- data/stdlib/timeout/0/timeout.rbs +10 -0
- data/stdlib/tmpdir/0/tmpdir.rbs +13 -4
- data/stdlib/tsort/0/cyclic.rbs +1 -0
- data/stdlib/tsort/0/interfaces.rbs +1 -0
- data/stdlib/tsort/0/tsort.rbs +42 -0
- data/stdlib/uri/0/common.rbs +57 -8
- data/stdlib/uri/0/file.rbs +55 -109
- data/stdlib/uri/0/ftp.rbs +6 -3
- data/stdlib/uri/0/generic.rbs +558 -329
- data/stdlib/uri/0/http.rbs +60 -114
- data/stdlib/uri/0/https.rbs +8 -102
- data/stdlib/uri/0/ldap.rbs +143 -137
- data/stdlib/uri/0/ldaps.rbs +8 -102
- data/stdlib/uri/0/mailto.rbs +3 -0
- data/stdlib/uri/0/rfc2396_parser.rbs +66 -26
- data/stdlib/uri/0/ws.rbs +6 -3
- data/stdlib/uri/0/wss.rbs +5 -3
- data/stdlib/yaml/0/dbm.rbs +151 -87
- data/stdlib/yaml/0/store.rbs +6 -0
- data/stdlib/zlib/0/zlib.rbs +90 -31
- metadata +18 -6
- data/lib/rbs/location.rb +0 -221
- data/sig/char_scanner.rbs +0 -9
data/core/proc.rbs
CHANGED
|
@@ -1,205 +1,210 @@
|
|
|
1
|
-
#
|
|
2
|
-
#
|
|
3
|
-
#
|
|
4
|
-
#
|
|
5
|
-
#
|
|
1
|
+
# <!-- rdoc-file=proc.c -->
|
|
2
|
+
# A `Proc` object is an encapsulation of a block of code, which can be stored in
|
|
3
|
+
# a local variable, passed to a method or another Proc, and can be called. Proc
|
|
4
|
+
# is an essential concept in Ruby and a core of its functional programming
|
|
5
|
+
# features.
|
|
6
6
|
#
|
|
7
|
-
#
|
|
8
|
-
# square = Proc.new {|x| x**2 }
|
|
7
|
+
# square = Proc.new {|x| x**2 }
|
|
9
8
|
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
14
|
-
# ```
|
|
9
|
+
# square.call(3) #=> 9
|
|
10
|
+
# # shorthands:
|
|
11
|
+
# square.(3) #=> 9
|
|
12
|
+
# square[3] #=> 9
|
|
15
13
|
#
|
|
16
|
-
#
|
|
17
|
-
#
|
|
14
|
+
# Proc objects are *closures*, meaning they remember and can use the entire
|
|
15
|
+
# context in which they were created.
|
|
18
16
|
#
|
|
19
|
-
#
|
|
20
|
-
#
|
|
21
|
-
#
|
|
22
|
-
# end
|
|
17
|
+
# def gen_times(factor)
|
|
18
|
+
# Proc.new {|n| n*factor } # remembers the value of factor at the moment of creation
|
|
19
|
+
# end
|
|
23
20
|
#
|
|
24
|
-
#
|
|
25
|
-
#
|
|
21
|
+
# times3 = gen_times(3)
|
|
22
|
+
# times5 = gen_times(5)
|
|
26
23
|
#
|
|
27
|
-
#
|
|
28
|
-
#
|
|
29
|
-
#
|
|
30
|
-
# ```
|
|
24
|
+
# times3.call(12) #=> 36
|
|
25
|
+
# times5.call(5) #=> 25
|
|
26
|
+
# times3.call(times5.call(4)) #=> 60
|
|
31
27
|
#
|
|
28
|
+
# ## Creation
|
|
32
29
|
#
|
|
33
|
-
# There are several methods to create a
|
|
30
|
+
# There are several methods to create a Proc
|
|
34
31
|
#
|
|
35
|
-
#
|
|
32
|
+
# * Use the Proc class constructor:
|
|
36
33
|
#
|
|
37
|
-
#
|
|
38
|
-
# proc1 = Proc.new {|x| x**2 }
|
|
39
|
-
# ```
|
|
34
|
+
# proc1 = Proc.new {|x| x**2 }
|
|
40
35
|
#
|
|
41
|
-
#
|
|
42
|
-
# [Kernel\#proc](https://ruby-doc.org/core-2.6.3/Kernel.html#method-i-proc)
|
|
43
|
-
# method as a shorthand of
|
|
44
|
-
# [::new](Proc#method-c-new):
|
|
36
|
+
# * Use the Kernel#proc method as a shorthand of Proc.new:
|
|
45
37
|
#
|
|
46
|
-
#
|
|
47
|
-
# proc2 = proc {|x| x**2 }
|
|
48
|
-
# ```
|
|
38
|
+
# proc2 = proc {|x| x**2 }
|
|
49
39
|
#
|
|
50
|
-
#
|
|
40
|
+
# * Receiving a block of code into proc argument (note the `&`):
|
|
51
41
|
#
|
|
52
|
-
#
|
|
53
|
-
#
|
|
54
|
-
#
|
|
55
|
-
#
|
|
42
|
+
# def make_proc(&block)
|
|
43
|
+
# block
|
|
44
|
+
# end
|
|
45
|
+
#
|
|
46
|
+
# proc3 = make_proc {|x| x**2 }
|
|
56
47
|
#
|
|
57
|
-
#
|
|
58
|
-
#
|
|
48
|
+
# * Construct a proc with lambda semantics using the Kernel#lambda method (see
|
|
49
|
+
# below for explanations about lambdas):
|
|
59
50
|
#
|
|
60
|
-
#
|
|
61
|
-
# [Kernel\#lambda](https://ruby-doc.org/core-2.6.3/Kernel.html#method-i-lambda)
|
|
62
|
-
# method (see below for explanations about lambdas):
|
|
51
|
+
# lambda1 = lambda {|x| x**2 }
|
|
63
52
|
#
|
|
64
|
-
#
|
|
65
|
-
#
|
|
66
|
-
#
|
|
53
|
+
# * Use the [Lambda proc
|
|
54
|
+
# literal](doc/syntax/literals_rdoc.html#label-Lambda+Proc+Literals) syntax
|
|
55
|
+
# (also constructs a proc with lambda semantics):
|
|
67
56
|
#
|
|
68
|
-
#
|
|
69
|
-
# semantics):
|
|
57
|
+
# lambda2 = ->(x) { x**2 }
|
|
70
58
|
#
|
|
71
|
-
# ```ruby
|
|
72
|
-
# lambda2 = ->(x) { x**2 }
|
|
73
|
-
# ```
|
|
74
59
|
#
|
|
60
|
+
# ## Lambda and non-lambda semantics
|
|
75
61
|
#
|
|
76
62
|
# Procs are coming in two flavors: lambda and non-lambda (regular procs).
|
|
77
63
|
# Differences are:
|
|
78
64
|
#
|
|
79
|
-
#
|
|
80
|
-
#
|
|
81
|
-
#
|
|
82
|
-
#
|
|
83
|
-
#
|
|
84
|
-
#
|
|
85
|
-
#
|
|
86
|
-
#
|
|
65
|
+
# * In lambdas, `return` and `break` means exit from this lambda;
|
|
66
|
+
# * In non-lambda procs, `return` means exit from embracing method (and will
|
|
67
|
+
# throw `LocalJumpError` if invoked outside the method);
|
|
68
|
+
# * In non-lambda procs, `break` means exit from the method which the block
|
|
69
|
+
# given for. (and will throw `LocalJumpError` if invoked after the method
|
|
70
|
+
# returns);
|
|
71
|
+
# * In lambdas, arguments are treated in the same way as in methods: strict,
|
|
72
|
+
# with `ArgumentError` for mismatching argument number, and no additional
|
|
73
|
+
# argument processing;
|
|
74
|
+
# * Regular procs accept arguments more generously: missing arguments are
|
|
75
|
+
# filled with `nil`, single Array arguments are deconstructed if the proc
|
|
76
|
+
# has multiple arguments, and there is no error raised on extra arguments.
|
|
87
77
|
#
|
|
88
|
-
# - Regular procs accept arguments more generously: missing arguments
|
|
89
|
-
# are filled with `nil`, single
|
|
90
|
-
# [Array](https://ruby-doc.org/core-2.6.3/Array.html) arguments are
|
|
91
|
-
# deconstructed if the proc has multiple arguments, and there is no
|
|
92
|
-
# error raised on extra arguments.
|
|
93
78
|
#
|
|
94
79
|
# Examples:
|
|
95
80
|
#
|
|
96
|
-
#
|
|
97
|
-
#
|
|
98
|
-
#
|
|
99
|
-
#
|
|
100
|
-
#
|
|
101
|
-
#
|
|
102
|
-
#
|
|
103
|
-
#
|
|
104
|
-
#
|
|
105
|
-
#
|
|
106
|
-
#
|
|
107
|
-
#
|
|
108
|
-
#
|
|
109
|
-
#
|
|
110
|
-
#
|
|
111
|
-
#
|
|
112
|
-
#
|
|
113
|
-
# end
|
|
81
|
+
# # +return+ in non-lambda proc, +b+, exits +m2+.
|
|
82
|
+
# # (The block +{ return }+ is given for +m1+ and embraced by +m2+.)
|
|
83
|
+
# $a = []; def m1(&b) b.call; $a << :m1 end; def m2() m1 { return }; $a << :m2 end; m2; p $a
|
|
84
|
+
# #=> []
|
|
85
|
+
#
|
|
86
|
+
# # +break+ in non-lambda proc, +b+, exits +m1+.
|
|
87
|
+
# # (The block +{ break }+ is given for +m1+ and embraced by +m2+.)
|
|
88
|
+
# $a = []; def m1(&b) b.call; $a << :m1 end; def m2() m1 { break }; $a << :m2 end; m2; p $a
|
|
89
|
+
# #=> [:m2]
|
|
90
|
+
#
|
|
91
|
+
# # +next+ in non-lambda proc, +b+, exits the block.
|
|
92
|
+
# # (The block +{ next }+ is given for +m1+ and embraced by +m2+.)
|
|
93
|
+
# $a = []; def m1(&b) b.call; $a << :m1 end; def m2() m1 { next }; $a << :m2 end; m2; p $a
|
|
94
|
+
# #=> [:m1, :m2]
|
|
95
|
+
#
|
|
96
|
+
# # Using +proc+ method changes the behavior as follows because
|
|
97
|
+
# # The block is given for +proc+ method and embraced by +m2+.
|
|
98
|
+
# $a = []; def m1(&b) b.call; $a << :m1 end; def m2() m1(&proc { return }); $a << :m2 end; m2; p $a
|
|
99
|
+
# #=> []
|
|
100
|
+
# $a = []; def m1(&b) b.call; $a << :m1 end; def m2() m1(&proc { break }); $a << :m2 end; m2; p $a
|
|
101
|
+
# # break from proc-closure (LocalJumpError)
|
|
102
|
+
# $a = []; def m1(&b) b.call; $a << :m1 end; def m2() m1(&proc { next }); $a << :m2 end; m2; p $a
|
|
103
|
+
# #=> [:m1, :m2]
|
|
104
|
+
#
|
|
105
|
+
# # +return+, +break+ and +next+ in the stubby lambda exits the block.
|
|
106
|
+
# # (+lambda+ method behaves same.)
|
|
107
|
+
# # (The block is given for stubby lambda syntax and embraced by +m2+.)
|
|
108
|
+
# $a = []; def m1(&b) b.call; $a << :m1 end; def m2() m1(&-> { return }); $a << :m2 end; m2; p $a
|
|
109
|
+
# #=> [:m1, :m2]
|
|
110
|
+
# $a = []; def m1(&b) b.call; $a << :m1 end; def m2() m1(&-> { break }); $a << :m2 end; m2; p $a
|
|
111
|
+
# #=> [:m1, :m2]
|
|
112
|
+
# $a = []; def m1(&b) b.call; $a << :m1 end; def m2() m1(&-> { next }); $a << :m2 end; m2; p $a
|
|
113
|
+
# #=> [:m1, :m2]
|
|
114
|
+
#
|
|
115
|
+
# p = proc {|x, y| "x=#{x}, y=#{y}" }
|
|
116
|
+
# p.call(1, 2) #=> "x=1, y=2"
|
|
117
|
+
# p.call([1, 2]) #=> "x=1, y=2", array deconstructed
|
|
118
|
+
# p.call(1, 2, 8) #=> "x=1, y=2", extra argument discarded
|
|
119
|
+
# p.call(1) #=> "x=1, y=", nil substituted instead of error
|
|
120
|
+
#
|
|
121
|
+
# l = lambda {|x, y| "x=#{x}, y=#{y}" }
|
|
122
|
+
# l.call(1, 2) #=> "x=1, y=2"
|
|
123
|
+
# l.call([1, 2]) # ArgumentError: wrong number of arguments (given 1, expected 2)
|
|
124
|
+
# l.call(1, 2, 8) # ArgumentError: wrong number of arguments (given 3, expected 2)
|
|
125
|
+
# l.call(1) # ArgumentError: wrong number of arguments (given 1, expected 2)
|
|
126
|
+
#
|
|
127
|
+
# def test_return
|
|
128
|
+
# -> { return 3 }.call # just returns from lambda into method body
|
|
129
|
+
# proc { return 4 }.call # returns from method
|
|
130
|
+
# return 5
|
|
131
|
+
# end
|
|
114
132
|
#
|
|
115
|
-
#
|
|
116
|
-
# ```
|
|
133
|
+
# test_return # => 4, return from proc
|
|
117
134
|
#
|
|
118
135
|
# Lambdas are useful as self-sufficient functions, in particular useful as
|
|
119
136
|
# arguments to higher-order functions, behaving exactly like Ruby methods.
|
|
120
137
|
#
|
|
121
138
|
# Procs are useful for implementing iterators:
|
|
122
139
|
#
|
|
123
|
-
#
|
|
124
|
-
#
|
|
125
|
-
#
|
|
126
|
-
#
|
|
127
|
-
#
|
|
128
|
-
#
|
|
129
|
-
#
|
|
130
|
-
#
|
|
131
|
-
#
|
|
132
|
-
#
|
|
133
|
-
#
|
|
134
|
-
#
|
|
135
|
-
#
|
|
136
|
-
#
|
|
137
|
-
#
|
|
138
|
-
#
|
|
139
|
-
#
|
|
140
|
-
#
|
|
141
|
-
#
|
|
142
|
-
#
|
|
143
|
-
#
|
|
144
|
-
#
|
|
145
|
-
#
|
|
146
|
-
#
|
|
147
|
-
#
|
|
148
|
-
#
|
|
149
|
-
#
|
|
150
|
-
#
|
|
151
|
-
#
|
|
152
|
-
# ```ruby
|
|
153
|
-
# class C
|
|
154
|
-
# define_method(:e, &proc {})
|
|
155
|
-
# end
|
|
156
|
-
# C.new.e(1,2) #=> ArgumentError
|
|
157
|
-
# C.new.method(:e).to_proc.lambda? #=> true
|
|
158
|
-
# ```
|
|
140
|
+
# def test
|
|
141
|
+
# [[1, 2], [3, 4], [5, 6]].map {|a, b| return a if a + b > 10 }
|
|
142
|
+
# # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
143
|
+
# end
|
|
144
|
+
#
|
|
145
|
+
# Inside `map`, the block of code is treated as a regular (non-lambda) proc,
|
|
146
|
+
# which means that the internal arrays will be deconstructed to pairs of
|
|
147
|
+
# arguments, and `return` will exit from the method `test`. That would not be
|
|
148
|
+
# possible with a stricter lambda.
|
|
149
|
+
#
|
|
150
|
+
# You can tell a lambda from a regular proc by using the #lambda? instance
|
|
151
|
+
# method.
|
|
152
|
+
#
|
|
153
|
+
# Lambda semantics is typically preserved during the proc lifetime, including
|
|
154
|
+
# `&`-deconstruction to a block of code:
|
|
155
|
+
#
|
|
156
|
+
# p = proc {|x, y| x }
|
|
157
|
+
# l = lambda {|x, y| x }
|
|
158
|
+
# [[1, 2], [3, 4]].map(&p) #=> [1, 3]
|
|
159
|
+
# [[1, 2], [3, 4]].map(&l) # ArgumentError: wrong number of arguments (given 1, expected 2)
|
|
160
|
+
#
|
|
161
|
+
# The only exception is dynamic method definition: even if defined by passing a
|
|
162
|
+
# non-lambda proc, methods still have normal semantics of argument checking.
|
|
163
|
+
#
|
|
164
|
+
# class C
|
|
165
|
+
# define_method(:e, &proc {})
|
|
166
|
+
# end
|
|
167
|
+
# C.new.e(1,2) #=> ArgumentError
|
|
168
|
+
# C.new.method(:e).to_proc.lambda? #=> true
|
|
159
169
|
#
|
|
160
170
|
# This exception ensures that methods never have unusual argument passing
|
|
161
|
-
# conventions, and makes it easy to have wrappers defining methods that
|
|
162
|
-
#
|
|
171
|
+
# conventions, and makes it easy to have wrappers defining methods that behave
|
|
172
|
+
# as usual.
|
|
163
173
|
#
|
|
164
|
-
#
|
|
165
|
-
#
|
|
166
|
-
#
|
|
167
|
-
#
|
|
168
|
-
# end
|
|
174
|
+
# class C
|
|
175
|
+
# def self.def2(name, &body)
|
|
176
|
+
# define_method(name, &body)
|
|
177
|
+
# end
|
|
169
178
|
#
|
|
170
|
-
#
|
|
171
|
-
#
|
|
172
|
-
#
|
|
173
|
-
# ```
|
|
179
|
+
# def2(:f) {}
|
|
180
|
+
# end
|
|
181
|
+
# C.new.f(1,2) #=> ArgumentError
|
|
174
182
|
#
|
|
175
|
-
# The wrapper
|
|
176
|
-
#
|
|
183
|
+
# The wrapper `def2` receives *body* as a non-lambda proc, yet defines a method
|
|
184
|
+
# which has normal semantics.
|
|
177
185
|
#
|
|
186
|
+
# ## Conversion of other objects to procs
|
|
178
187
|
#
|
|
179
|
-
# Any object that implements the `to_proc` method can be converted into a
|
|
180
|
-
#
|
|
188
|
+
# Any object that implements the `to_proc` method can be converted into a proc
|
|
189
|
+
# by the `&` operator, and therefore can be consumed by iterators.
|
|
181
190
|
#
|
|
182
|
-
#
|
|
183
|
-
#
|
|
184
|
-
#
|
|
185
|
-
#
|
|
186
|
-
# end
|
|
191
|
+
# class Greeter
|
|
192
|
+
# def initialize(greeting)
|
|
193
|
+
# @greeting = greeting
|
|
194
|
+
# end
|
|
187
195
|
#
|
|
188
|
-
#
|
|
189
|
-
#
|
|
190
|
-
#
|
|
191
|
-
#
|
|
196
|
+
# def to_proc
|
|
197
|
+
# proc {|name| "#{@greeting}, #{name}!" }
|
|
198
|
+
# end
|
|
199
|
+
# end
|
|
192
200
|
#
|
|
193
|
-
#
|
|
194
|
-
#
|
|
195
|
-
#
|
|
196
|
-
#
|
|
197
|
-
# ```
|
|
201
|
+
# hi = Greeter.new("Hi")
|
|
202
|
+
# hey = Greeter.new("Hey")
|
|
203
|
+
# ["Bob", "Jane"].map(&hi) #=> ["Hi, Bob!", "Hi, Jane!"]
|
|
204
|
+
# ["Bob", "Jane"].map(&hey) #=> ["Hey, Bob!", "Hey, Jane!"]
|
|
198
205
|
#
|
|
199
|
-
# Of the Ruby core classes, this method is implemented by
|
|
200
|
-
#
|
|
201
|
-
# [Method](https://ruby-doc.org/core-2.6.3/Method.html), and
|
|
202
|
-
# [Hash](https://ruby-doc.org/core-2.6.3/Hash.html).
|
|
206
|
+
# Of the Ruby core classes, this method is implemented by Symbol, Method, and
|
|
207
|
+
# Hash.
|
|
203
208
|
#
|
|
204
209
|
# :to_s.to_proc.call(1) #=> "1"
|
|
205
210
|
# [1, 2].map(&:to_s) #=> ["1", "2"]
|
|
@@ -209,19 +214,96 @@
|
|
|
209
214
|
#
|
|
210
215
|
# {test: 1}.to_proc.call(:test) #=> 1
|
|
211
216
|
# %i[test many keys].map(&{test: 1}) #=> [1, nil, nil]
|
|
217
|
+
#
|
|
218
|
+
# ## Orphaned Proc
|
|
219
|
+
#
|
|
220
|
+
# `return` and `break` in a block exit a method. If a Proc object is generated
|
|
221
|
+
# from the block and the Proc object survives until the method is returned,
|
|
222
|
+
# `return` and `break` cannot work. In such case, `return` and `break` raises
|
|
223
|
+
# LocalJumpError. A Proc object in such situation is called as orphaned Proc
|
|
224
|
+
# object.
|
|
225
|
+
#
|
|
226
|
+
# Note that the method to exit is different for `return` and `break`. There is a
|
|
227
|
+
# situation that orphaned for `break` but not orphaned for `return`.
|
|
228
|
+
#
|
|
229
|
+
# def m1(&b) b.call end; def m2(); m1 { return } end; m2 # ok
|
|
230
|
+
# def m1(&b) b.call end; def m2(); m1 { break } end; m2 # ok
|
|
231
|
+
#
|
|
232
|
+
# def m1(&b) b end; def m2(); m1 { return }.call end; m2 # ok
|
|
233
|
+
# def m1(&b) b end; def m2(); m1 { break }.call end; m2 # LocalJumpError
|
|
234
|
+
#
|
|
235
|
+
# def m1(&b) b end; def m2(); m1 { return } end; m2.call # LocalJumpError
|
|
236
|
+
# def m1(&b) b end; def m2(); m1 { break } end; m2.call # LocalJumpError
|
|
237
|
+
#
|
|
238
|
+
# Since `return` and `break` exits the block itself in lambdas, lambdas cannot
|
|
239
|
+
# be orphaned.
|
|
240
|
+
#
|
|
241
|
+
# ## Numbered parameters
|
|
242
|
+
#
|
|
243
|
+
# Numbered parameters are implicitly defined block parameters intended to
|
|
244
|
+
# simplify writing short blocks:
|
|
245
|
+
#
|
|
246
|
+
# # Explicit parameter:
|
|
247
|
+
# %w[test me please].each { |str| puts str.upcase } # prints TEST, ME, PLEASE
|
|
248
|
+
# (1..5).map { |i| i**2 } # => [1, 4, 9, 16, 25]
|
|
249
|
+
#
|
|
250
|
+
# # Implicit parameter:
|
|
251
|
+
# %w[test me please].each { puts _1.upcase } # prints TEST, ME, PLEASE
|
|
252
|
+
# (1..5).map { _1**2 } # => [1, 4, 9, 16, 25]
|
|
253
|
+
#
|
|
254
|
+
# Parameter names from `_1` to `_9` are supported:
|
|
255
|
+
#
|
|
256
|
+
# [10, 20, 30].zip([40, 50, 60], [70, 80, 90]).map { _1 + _2 + _3 }
|
|
257
|
+
# # => [120, 150, 180]
|
|
258
|
+
#
|
|
259
|
+
# Though, it is advised to resort to them wisely, probably limiting yourself to
|
|
260
|
+
# `_1` and `_2`, and to one-line blocks.
|
|
261
|
+
#
|
|
262
|
+
# Numbered parameters can't be used together with explicitly named ones:
|
|
263
|
+
#
|
|
264
|
+
# [10, 20, 30].map { |x| _1**2 }
|
|
265
|
+
# # SyntaxError (ordinary parameter is defined)
|
|
266
|
+
#
|
|
267
|
+
# To avoid conflicts, naming local variables or method arguments `_1`, `_2` and
|
|
268
|
+
# so on, causes a warning.
|
|
269
|
+
#
|
|
270
|
+
# _1 = 'test'
|
|
271
|
+
# # warning: `_1' is reserved as numbered parameter
|
|
272
|
+
#
|
|
273
|
+
# Using implicit numbered parameters affects block's arity:
|
|
274
|
+
#
|
|
275
|
+
# p = proc { _1 + _2 }
|
|
276
|
+
# l = lambda { _1 + _2 }
|
|
277
|
+
# p.parameters # => [[:opt, :_1], [:opt, :_2]]
|
|
278
|
+
# p.arity # => 2
|
|
279
|
+
# l.parameters # => [[:req, :_1], [:req, :_2]]
|
|
280
|
+
# l.arity # => 2
|
|
281
|
+
#
|
|
282
|
+
# Blocks with numbered parameters can't be nested:
|
|
283
|
+
#
|
|
284
|
+
# %w[test me].each { _1.each_char { p _1 } }
|
|
285
|
+
# # SyntaxError (numbered parameter is already used in outer block here)
|
|
286
|
+
# # %w[test me].each { _1.each_char { p _1 } }
|
|
287
|
+
# # ^~
|
|
288
|
+
#
|
|
289
|
+
# Numbered parameters were introduced in Ruby 2.7.
|
|
290
|
+
#
|
|
212
291
|
class Proc < Object
|
|
213
292
|
def clone: () -> self
|
|
214
293
|
|
|
215
|
-
#
|
|
216
|
-
#
|
|
217
|
-
#
|
|
218
|
-
#
|
|
219
|
-
#
|
|
220
|
-
# arguments
|
|
221
|
-
#
|
|
222
|
-
# mandatory
|
|
223
|
-
#
|
|
224
|
-
# arguments
|
|
294
|
+
# <!--
|
|
295
|
+
# rdoc-file=proc.c
|
|
296
|
+
# - prc.arity -> integer
|
|
297
|
+
# -->
|
|
298
|
+
# Returns the number of mandatory arguments. If the block is declared to take no
|
|
299
|
+
# arguments, returns 0. If the block is known to take exactly n arguments,
|
|
300
|
+
# returns n. If the block has optional arguments, returns -n-1, where n is the
|
|
301
|
+
# number of mandatory arguments, with the exception for blocks that are not
|
|
302
|
+
# lambdas and have only a finite number of optional arguments; in this latter
|
|
303
|
+
# case, returns n. Keyword arguments will be considered as a single additional
|
|
304
|
+
# argument, that argument being mandatory if any keyword argument is mandatory.
|
|
305
|
+
# A #proc with no argument declarations is the same as a block declaring `||` as
|
|
306
|
+
# its arguments.
|
|
225
307
|
#
|
|
226
308
|
# proc {}.arity #=> 0
|
|
227
309
|
# proc { || }.arity #=> 0
|
|
@@ -246,185 +328,300 @@ class Proc < Object
|
|
|
246
328
|
# lambda { |(a, b), c=0| }.arity #=> -2
|
|
247
329
|
# proc { |a, x:0, y:0| }.arity #=> 1
|
|
248
330
|
# lambda { |a, x:0, y:0| }.arity #=> -2
|
|
331
|
+
#
|
|
249
332
|
def arity: () -> Integer
|
|
250
333
|
|
|
251
|
-
#
|
|
334
|
+
# <!--
|
|
335
|
+
# rdoc-file=proc.c
|
|
336
|
+
# - prc.binding -> binding
|
|
337
|
+
# -->
|
|
338
|
+
# Returns the binding associated with *prc*.
|
|
339
|
+
#
|
|
340
|
+
# def fred(param)
|
|
341
|
+
# proc {}
|
|
342
|
+
# end
|
|
252
343
|
#
|
|
253
|
-
#
|
|
254
|
-
#
|
|
255
|
-
# proc {}
|
|
256
|
-
# end
|
|
344
|
+
# b = fred(99)
|
|
345
|
+
# eval("param", b.binding) #=> 99
|
|
257
346
|
#
|
|
258
|
-
# b = fred(99)
|
|
259
|
-
# eval("param", b.binding) #=> 99
|
|
260
|
-
# ```
|
|
261
347
|
def binding: () -> Binding
|
|
262
348
|
|
|
349
|
+
# <!--
|
|
350
|
+
# rdoc-file=proc.c
|
|
351
|
+
# - prc.call(params,...) -> obj
|
|
352
|
+
# - prc[params,...] -> obj
|
|
353
|
+
# - prc.(params,...) -> obj
|
|
354
|
+
# - prc.yield(params,...) -> obj
|
|
355
|
+
# -->
|
|
356
|
+
# Invokes the block, setting the block's parameters to the values in *params*
|
|
357
|
+
# using something close to method calling semantics. Returns the value of the
|
|
358
|
+
# last expression evaluated in the block.
|
|
359
|
+
#
|
|
360
|
+
# a_proc = Proc.new {|scalar, *values| values.map {|value| value*scalar } }
|
|
361
|
+
# a_proc.call(9, 1, 2, 3) #=> [9, 18, 27]
|
|
362
|
+
# a_proc[9, 1, 2, 3] #=> [9, 18, 27]
|
|
363
|
+
# a_proc.(9, 1, 2, 3) #=> [9, 18, 27]
|
|
364
|
+
# a_proc.yield(9, 1, 2, 3) #=> [9, 18, 27]
|
|
365
|
+
#
|
|
366
|
+
# Note that `prc.()` invokes `prc.call()` with the parameters given. It's
|
|
367
|
+
# syntactic sugar to hide "call".
|
|
368
|
+
#
|
|
369
|
+
# For procs created using #lambda or `->()` an error is generated if the wrong
|
|
370
|
+
# number of parameters are passed to the proc. For procs created using Proc.new
|
|
371
|
+
# or Kernel.proc, extra parameters are silently discarded and missing parameters
|
|
372
|
+
# are set to `nil`.
|
|
373
|
+
#
|
|
374
|
+
# a_proc = proc {|a,b| [a,b] }
|
|
375
|
+
# a_proc.call(1) #=> [1, nil]
|
|
376
|
+
#
|
|
377
|
+
# a_proc = lambda {|a,b| [a,b] }
|
|
378
|
+
# a_proc.call(1) # ArgumentError: wrong number of arguments (given 1, expected 2)
|
|
379
|
+
#
|
|
380
|
+
# See also Proc#lambda?.
|
|
381
|
+
#
|
|
263
382
|
def call: (*untyped arg0) -> untyped
|
|
264
383
|
|
|
384
|
+
# <!-- rdoc-file=proc.c -->
|
|
385
|
+
# Invokes the block, setting the block's parameters to the values in *params*
|
|
386
|
+
# using something close to method calling semantics. Returns the value of the
|
|
387
|
+
# last expression evaluated in the block.
|
|
388
|
+
#
|
|
389
|
+
# a_proc = Proc.new {|scalar, *values| values.map {|value| value*scalar } }
|
|
390
|
+
# a_proc.call(9, 1, 2, 3) #=> [9, 18, 27]
|
|
391
|
+
# a_proc[9, 1, 2, 3] #=> [9, 18, 27]
|
|
392
|
+
# a_proc.(9, 1, 2, 3) #=> [9, 18, 27]
|
|
393
|
+
# a_proc.yield(9, 1, 2, 3) #=> [9, 18, 27]
|
|
394
|
+
#
|
|
395
|
+
# Note that `prc.()` invokes `prc.call()` with the parameters given. It's
|
|
396
|
+
# syntactic sugar to hide "call".
|
|
397
|
+
#
|
|
398
|
+
# For procs created using #lambda or `->()` an error is generated if the wrong
|
|
399
|
+
# number of parameters are passed to the proc. For procs created using Proc.new
|
|
400
|
+
# or Kernel.proc, extra parameters are silently discarded and missing parameters
|
|
401
|
+
# are set to `nil`.
|
|
402
|
+
#
|
|
403
|
+
# a_proc = proc {|a,b| [a,b] }
|
|
404
|
+
# a_proc.call(1) #=> [1, nil]
|
|
405
|
+
#
|
|
406
|
+
# a_proc = lambda {|a,b| [a,b] }
|
|
407
|
+
# a_proc.call(1) # ArgumentError: wrong number of arguments (given 1, expected 2)
|
|
408
|
+
#
|
|
409
|
+
# See also Proc#lambda?.
|
|
410
|
+
#
|
|
265
411
|
def []: (*untyped arg0) -> untyped
|
|
266
412
|
|
|
413
|
+
# <!--
|
|
414
|
+
# rdoc-file=proc.c
|
|
415
|
+
# - prc.curry -> a_proc
|
|
416
|
+
# - prc.curry(arity) -> a_proc
|
|
417
|
+
# -->
|
|
418
|
+
# Returns a curried proc. If the optional *arity* argument is given, it
|
|
419
|
+
# determines the number of arguments. A curried proc receives some arguments. If
|
|
420
|
+
# a sufficient number of arguments are supplied, it passes the supplied
|
|
421
|
+
# arguments to the original proc and returns the result. Otherwise, returns
|
|
422
|
+
# another curried proc that takes the rest of arguments.
|
|
423
|
+
#
|
|
424
|
+
# b = proc {|x, y, z| (x||0) + (y||0) + (z||0) }
|
|
425
|
+
# p b.curry[1][2][3] #=> 6
|
|
426
|
+
# p b.curry[1, 2][3, 4] #=> 6
|
|
427
|
+
# p b.curry(5)[1][2][3][4][5] #=> 6
|
|
428
|
+
# p b.curry(5)[1, 2][3, 4][5] #=> 6
|
|
429
|
+
# p b.curry(1)[1] #=> 1
|
|
430
|
+
#
|
|
431
|
+
# b = proc {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) }
|
|
432
|
+
# p b.curry[1][2][3] #=> 6
|
|
433
|
+
# p b.curry[1, 2][3, 4] #=> 10
|
|
434
|
+
# p b.curry(5)[1][2][3][4][5] #=> 15
|
|
435
|
+
# p b.curry(5)[1, 2][3, 4][5] #=> 15
|
|
436
|
+
# p b.curry(1)[1] #=> 1
|
|
437
|
+
#
|
|
438
|
+
# b = lambda {|x, y, z| (x||0) + (y||0) + (z||0) }
|
|
439
|
+
# p b.curry[1][2][3] #=> 6
|
|
440
|
+
# p b.curry[1, 2][3, 4] #=> wrong number of arguments (given 4, expected 3)
|
|
441
|
+
# p b.curry(5) #=> wrong number of arguments (given 5, expected 3)
|
|
442
|
+
# p b.curry(1) #=> wrong number of arguments (given 1, expected 3)
|
|
443
|
+
#
|
|
444
|
+
# b = lambda {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) }
|
|
445
|
+
# p b.curry[1][2][3] #=> 6
|
|
446
|
+
# p b.curry[1, 2][3, 4] #=> 10
|
|
447
|
+
# p b.curry(5)[1][2][3][4][5] #=> 15
|
|
448
|
+
# p b.curry(5)[1, 2][3, 4][5] #=> 15
|
|
449
|
+
# p b.curry(1) #=> wrong number of arguments (given 1, expected 3)
|
|
450
|
+
#
|
|
451
|
+
# b = proc { :foo }
|
|
452
|
+
# p b.curry[] #=> :foo
|
|
453
|
+
#
|
|
267
454
|
def curry: (?_ToInt arity) -> Proc
|
|
268
455
|
|
|
456
|
+
# <!--
|
|
457
|
+
# rdoc-file=proc.c
|
|
458
|
+
# - prc.hash -> integer
|
|
459
|
+
# -->
|
|
269
460
|
# Returns a hash value corresponding to proc body.
|
|
270
461
|
#
|
|
271
|
-
# See also Object
|
|
462
|
+
# See also Object#hash.
|
|
463
|
+
#
|
|
272
464
|
def hash: () -> Integer
|
|
273
465
|
|
|
466
|
+
# <!--
|
|
467
|
+
# rdoc-file=proc.c
|
|
468
|
+
# - Proc.new {|...| block } -> a_proc
|
|
469
|
+
# -->
|
|
470
|
+
# Creates a new Proc object, bound to the current context.
|
|
471
|
+
#
|
|
472
|
+
# proc = Proc.new { "hello" }
|
|
473
|
+
# proc.call #=> "hello"
|
|
474
|
+
#
|
|
475
|
+
# Raises ArgumentError if called without a block.
|
|
476
|
+
#
|
|
477
|
+
# Proc.new #=> ArgumentError
|
|
478
|
+
#
|
|
274
479
|
def initialize: () { (*untyped) -> untyped } -> void
|
|
275
480
|
|
|
276
|
-
#
|
|
277
|
-
#
|
|
278
|
-
#
|
|
481
|
+
# <!--
|
|
482
|
+
# rdoc-file=proc.c
|
|
483
|
+
# - prc.lambda? -> true or false
|
|
484
|
+
# -->
|
|
485
|
+
# Returns `true` if a Proc object is lambda. `false` if non-lambda.
|
|
279
486
|
#
|
|
280
|
-
#
|
|
281
|
-
#
|
|
487
|
+
# The lambda-ness affects argument handling and the behavior of `return` and
|
|
488
|
+
# `break`.
|
|
282
489
|
#
|
|
283
|
-
#
|
|
284
|
-
#
|
|
285
|
-
#
|
|
490
|
+
# A Proc object generated by `proc` ignores extra arguments.
|
|
491
|
+
#
|
|
492
|
+
# proc {|a,b| [a,b] }.call(1,2,3) #=> [1,2]
|
|
286
493
|
#
|
|
287
494
|
# It provides `nil` for missing arguments.
|
|
288
495
|
#
|
|
289
|
-
#
|
|
290
|
-
# proc {|a,b| [a,b] }.call(1) #=> [1,nil]
|
|
291
|
-
# ```
|
|
496
|
+
# proc {|a,b| [a,b] }.call(1) #=> [1,nil]
|
|
292
497
|
#
|
|
293
498
|
# It expands a single array argument.
|
|
294
499
|
#
|
|
295
|
-
#
|
|
296
|
-
# proc {|a,b| [a,b] }.call([1,2]) #=> [1,2]
|
|
297
|
-
# ```
|
|
500
|
+
# proc {|a,b| [a,b] }.call([1,2]) #=> [1,2]
|
|
298
501
|
#
|
|
299
|
-
# A
|
|
300
|
-
# have such tricks.
|
|
502
|
+
# A Proc object generated by `lambda` doesn't have such tricks.
|
|
301
503
|
#
|
|
302
|
-
#
|
|
303
|
-
#
|
|
304
|
-
#
|
|
305
|
-
# lambda {|a,b| [a,b] }.call([1,2]) #=> ArgumentError
|
|
306
|
-
# ```
|
|
504
|
+
# lambda {|a,b| [a,b] }.call(1,2,3) #=> ArgumentError
|
|
505
|
+
# lambda {|a,b| [a,b] }.call(1) #=> ArgumentError
|
|
506
|
+
# lambda {|a,b| [a,b] }.call([1,2]) #=> ArgumentError
|
|
307
507
|
#
|
|
308
|
-
#
|
|
309
|
-
#
|
|
508
|
+
# Proc#lambda? is a predicate for the tricks. It returns `true` if no tricks
|
|
509
|
+
# apply.
|
|
310
510
|
#
|
|
311
|
-
#
|
|
312
|
-
#
|
|
313
|
-
# proc {}.lambda? #=> false
|
|
314
|
-
# ```
|
|
511
|
+
# lambda {}.lambda? #=> true
|
|
512
|
+
# proc {}.lambda? #=> false
|
|
315
513
|
#
|
|
316
|
-
#
|
|
514
|
+
# Proc.new is the same as `proc`.
|
|
317
515
|
#
|
|
318
|
-
#
|
|
319
|
-
# Proc.new {}.lambda? #=> false
|
|
320
|
-
# ```
|
|
516
|
+
# Proc.new {}.lambda? #=> false
|
|
321
517
|
#
|
|
322
|
-
# `lambda`, `proc` and
|
|
323
|
-
#
|
|
324
|
-
# by `&` argument.
|
|
518
|
+
# `lambda`, `proc` and Proc.new preserve the tricks of a Proc object given by
|
|
519
|
+
# `&` argument.
|
|
325
520
|
#
|
|
326
|
-
#
|
|
327
|
-
#
|
|
328
|
-
#
|
|
329
|
-
# Proc.new(&lambda {}).lambda? #=> true
|
|
521
|
+
# lambda(&lambda {}).lambda? #=> true
|
|
522
|
+
# proc(&lambda {}).lambda? #=> true
|
|
523
|
+
# Proc.new(&lambda {}).lambda? #=> true
|
|
330
524
|
#
|
|
331
|
-
#
|
|
332
|
-
#
|
|
333
|
-
#
|
|
334
|
-
# ```
|
|
525
|
+
# lambda(&proc {}).lambda? #=> false
|
|
526
|
+
# proc(&proc {}).lambda? #=> false
|
|
527
|
+
# Proc.new(&proc {}).lambda? #=> false
|
|
335
528
|
#
|
|
336
|
-
# A
|
|
337
|
-
# the tricks
|
|
529
|
+
# A Proc object generated by `&` argument has the tricks
|
|
338
530
|
#
|
|
339
|
-
#
|
|
340
|
-
#
|
|
341
|
-
# n {} #=> false
|
|
342
|
-
# ```
|
|
531
|
+
# def n(&b) b.lambda? end
|
|
532
|
+
# n {} #=> false
|
|
343
533
|
#
|
|
344
|
-
# The `&` argument preserves the tricks if a
|
|
345
|
-
#
|
|
534
|
+
# The `&` argument preserves the tricks if a Proc object is given by `&`
|
|
535
|
+
# argument.
|
|
346
536
|
#
|
|
347
|
-
#
|
|
348
|
-
#
|
|
349
|
-
#
|
|
350
|
-
# n(&Proc.new {}) #=> false
|
|
351
|
-
# ```
|
|
537
|
+
# n(&lambda {}) #=> true
|
|
538
|
+
# n(&proc {}) #=> false
|
|
539
|
+
# n(&Proc.new {}) #=> false
|
|
352
540
|
#
|
|
353
|
-
# A
|
|
354
|
-
# tricks.
|
|
541
|
+
# A Proc object converted from a method has no tricks.
|
|
355
542
|
#
|
|
356
|
-
#
|
|
357
|
-
#
|
|
358
|
-
# method(:m).to_proc.lambda? #=> true
|
|
543
|
+
# def m() end
|
|
544
|
+
# method(:m).to_proc.lambda? #=> true
|
|
359
545
|
#
|
|
360
|
-
#
|
|
361
|
-
#
|
|
362
|
-
# ```
|
|
546
|
+
# n(&method(:m)) #=> true
|
|
547
|
+
# n(&method(:m).to_proc) #=> true
|
|
363
548
|
#
|
|
364
|
-
# `define_method` is treated the same as method definition. The defined
|
|
365
|
-
#
|
|
549
|
+
# `define_method` is treated the same as method definition. The defined method
|
|
550
|
+
# has no tricks.
|
|
366
551
|
#
|
|
367
|
-
#
|
|
368
|
-
#
|
|
369
|
-
#
|
|
370
|
-
#
|
|
371
|
-
#
|
|
372
|
-
# C.new.method(:d).to_proc.lambda? #=> true
|
|
373
|
-
# ```
|
|
552
|
+
# class C
|
|
553
|
+
# define_method(:d) {}
|
|
554
|
+
# end
|
|
555
|
+
# C.new.d(1,2) #=> ArgumentError
|
|
556
|
+
# C.new.method(:d).to_proc.lambda? #=> true
|
|
374
557
|
#
|
|
375
558
|
# `define_method` always defines a method without the tricks, even if a
|
|
376
|
-
# non-lambda
|
|
377
|
-
#
|
|
378
|
-
#
|
|
379
|
-
#
|
|
380
|
-
#
|
|
381
|
-
#
|
|
382
|
-
#
|
|
383
|
-
#
|
|
384
|
-
#
|
|
385
|
-
#
|
|
386
|
-
#
|
|
387
|
-
#
|
|
388
|
-
#
|
|
389
|
-
#
|
|
390
|
-
#
|
|
391
|
-
#
|
|
392
|
-
#
|
|
393
|
-
#
|
|
394
|
-
#
|
|
395
|
-
#
|
|
396
|
-
# def2(:f) {}
|
|
397
|
-
# end
|
|
398
|
-
# C.new.f(1,2) #=> ArgumentError
|
|
399
|
-
# ```
|
|
559
|
+
# non-lambda Proc object is given. This is the only exception for which the
|
|
560
|
+
# tricks are not preserved.
|
|
561
|
+
#
|
|
562
|
+
# class C
|
|
563
|
+
# define_method(:e, &proc {})
|
|
564
|
+
# end
|
|
565
|
+
# C.new.e(1,2) #=> ArgumentError
|
|
566
|
+
# C.new.method(:e).to_proc.lambda? #=> true
|
|
567
|
+
#
|
|
568
|
+
# This exception ensures that methods never have tricks and makes it easy to
|
|
569
|
+
# have wrappers to define methods that behave as usual.
|
|
570
|
+
#
|
|
571
|
+
# class C
|
|
572
|
+
# def self.def2(name, &body)
|
|
573
|
+
# define_method(name, &body)
|
|
574
|
+
# end
|
|
575
|
+
#
|
|
576
|
+
# def2(:f) {}
|
|
577
|
+
# end
|
|
578
|
+
# C.new.f(1,2) #=> ArgumentError
|
|
400
579
|
#
|
|
401
580
|
# The wrapper *def2* defines a method which has no tricks.
|
|
581
|
+
#
|
|
402
582
|
def lambda?: () -> bool
|
|
403
583
|
|
|
584
|
+
# <!--
|
|
585
|
+
# rdoc-file=proc.c
|
|
586
|
+
# - prc.parameters -> array
|
|
587
|
+
# -->
|
|
404
588
|
# Returns the parameter information of this proc.
|
|
405
589
|
#
|
|
406
|
-
#
|
|
407
|
-
#
|
|
408
|
-
#
|
|
409
|
-
# ```
|
|
590
|
+
# prc = lambda{|x, y=42, *other|}
|
|
591
|
+
# prc.parameters #=> [[:req, :x], [:opt, :y], [:rest, :other]]
|
|
592
|
+
#
|
|
410
593
|
def parameters: () -> ::Array[[ Symbol, Symbol ]]
|
|
411
594
|
|
|
412
|
-
#
|
|
413
|
-
#
|
|
595
|
+
# <!--
|
|
596
|
+
# rdoc-file=proc.c
|
|
597
|
+
# - prc.source_location -> [String, Integer]
|
|
598
|
+
# -->
|
|
599
|
+
# Returns the Ruby source filename and line number containing this proc or `nil`
|
|
600
|
+
# if this proc was not defined in Ruby (i.e. native).
|
|
601
|
+
#
|
|
414
602
|
def source_location: () -> [ String, Integer ]
|
|
415
603
|
|
|
416
|
-
#
|
|
417
|
-
#
|
|
604
|
+
# <!--
|
|
605
|
+
# rdoc-file=proc.c
|
|
606
|
+
# - prc.to_proc -> proc
|
|
607
|
+
# -->
|
|
608
|
+
# Part of the protocol for converting objects to Proc objects. Instances of
|
|
609
|
+
# class Proc simply return themselves.
|
|
610
|
+
#
|
|
418
611
|
def to_proc: () -> self
|
|
419
612
|
|
|
420
|
-
#
|
|
421
|
-
#
|
|
422
|
-
#
|
|
613
|
+
# <!--
|
|
614
|
+
# rdoc-file=proc.c
|
|
615
|
+
# - prc.to_s -> string
|
|
616
|
+
# -->
|
|
617
|
+
# Returns the unique identifier for this proc, along with an indication of where
|
|
618
|
+
# the proc was defined.
|
|
423
619
|
#
|
|
424
|
-
#
|
|
425
|
-
# Also aliased as: [inspect](Proc.downloaded.ruby_doc#method-i-inspect)
|
|
426
620
|
def to_s: () -> String
|
|
427
621
|
|
|
428
|
-
#
|
|
622
|
+
# <!-- rdoc-file=proc.c -->
|
|
623
|
+
# Returns the unique identifier for this proc, along with an indication of where
|
|
624
|
+
# the proc was defined.
|
|
625
|
+
#
|
|
429
626
|
def inspect: () -> String
|
|
430
627
|
end
|