rbs 0.2.0
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 +7 -0
- data/.github/workflows/ruby.yml +28 -0
- data/.gitignore +12 -0
- data/.rubocop.yml +15 -0
- data/BSDL +22 -0
- data/CHANGELOG.md +9 -0
- data/COPYING +56 -0
- data/Gemfile +6 -0
- data/README.md +93 -0
- data/Rakefile +142 -0
- data/bin/annotate-with-rdoc +157 -0
- data/bin/console +14 -0
- data/bin/query-rdoc +103 -0
- data/bin/setup +10 -0
- data/bin/sort +89 -0
- data/bin/test_runner.rb +16 -0
- data/docs/CONTRIBUTING.md +97 -0
- data/docs/sigs.md +148 -0
- data/docs/stdlib.md +152 -0
- data/docs/syntax.md +528 -0
- data/exe/rbs +7 -0
- data/lib/rbs.rb +64 -0
- data/lib/rbs/ast/annotation.rb +27 -0
- data/lib/rbs/ast/comment.rb +27 -0
- data/lib/rbs/ast/declarations.rb +395 -0
- data/lib/rbs/ast/members.rb +362 -0
- data/lib/rbs/buffer.rb +50 -0
- data/lib/rbs/builtin_names.rb +55 -0
- data/lib/rbs/cli.rb +558 -0
- data/lib/rbs/constant.rb +26 -0
- data/lib/rbs/constant_table.rb +150 -0
- data/lib/rbs/definition.rb +170 -0
- data/lib/rbs/definition_builder.rb +919 -0
- data/lib/rbs/environment.rb +281 -0
- data/lib/rbs/environment_loader.rb +136 -0
- data/lib/rbs/environment_walker.rb +124 -0
- data/lib/rbs/errors.rb +187 -0
- data/lib/rbs/location.rb +102 -0
- data/lib/rbs/method_type.rb +123 -0
- data/lib/rbs/namespace.rb +91 -0
- data/lib/rbs/parser.y +1344 -0
- data/lib/rbs/prototype/rb.rb +553 -0
- data/lib/rbs/prototype/rbi.rb +587 -0
- data/lib/rbs/prototype/runtime.rb +381 -0
- data/lib/rbs/substitution.rb +46 -0
- data/lib/rbs/test.rb +26 -0
- data/lib/rbs/test/errors.rb +61 -0
- data/lib/rbs/test/hook.rb +294 -0
- data/lib/rbs/test/setup.rb +58 -0
- data/lib/rbs/test/spy.rb +325 -0
- data/lib/rbs/test/test_helper.rb +183 -0
- data/lib/rbs/test/type_check.rb +254 -0
- data/lib/rbs/type_name.rb +70 -0
- data/lib/rbs/types.rb +936 -0
- data/lib/rbs/variance_calculator.rb +138 -0
- data/lib/rbs/vendorer.rb +47 -0
- data/lib/rbs/version.rb +3 -0
- data/lib/rbs/writer.rb +269 -0
- data/lib/ruby/signature.rb +7 -0
- data/rbs.gemspec +46 -0
- data/stdlib/abbrev/abbrev.rbs +60 -0
- data/stdlib/base64/base64.rbs +71 -0
- data/stdlib/benchmark/benchmark.rbs +372 -0
- data/stdlib/builtin/array.rbs +1997 -0
- data/stdlib/builtin/basic_object.rbs +280 -0
- data/stdlib/builtin/binding.rbs +177 -0
- data/stdlib/builtin/builtin.rbs +45 -0
- data/stdlib/builtin/class.rbs +145 -0
- data/stdlib/builtin/comparable.rbs +116 -0
- data/stdlib/builtin/complex.rbs +400 -0
- data/stdlib/builtin/constants.rbs +37 -0
- data/stdlib/builtin/data.rbs +5 -0
- data/stdlib/builtin/deprecated.rbs +2 -0
- data/stdlib/builtin/dir.rbs +413 -0
- data/stdlib/builtin/encoding.rbs +607 -0
- data/stdlib/builtin/enumerable.rbs +404 -0
- data/stdlib/builtin/enumerator.rbs +260 -0
- data/stdlib/builtin/errno.rbs +781 -0
- data/stdlib/builtin/errors.rbs +582 -0
- data/stdlib/builtin/exception.rbs +194 -0
- data/stdlib/builtin/false_class.rbs +40 -0
- data/stdlib/builtin/fiber.rbs +68 -0
- data/stdlib/builtin/fiber_error.rbs +12 -0
- data/stdlib/builtin/file.rbs +1076 -0
- data/stdlib/builtin/file_test.rbs +59 -0
- data/stdlib/builtin/float.rbs +696 -0
- data/stdlib/builtin/gc.rbs +243 -0
- data/stdlib/builtin/hash.rbs +1029 -0
- data/stdlib/builtin/integer.rbs +707 -0
- data/stdlib/builtin/io.rbs +683 -0
- data/stdlib/builtin/kernel.rbs +576 -0
- data/stdlib/builtin/marshal.rbs +161 -0
- data/stdlib/builtin/match_data.rbs +271 -0
- data/stdlib/builtin/math.rbs +369 -0
- data/stdlib/builtin/method.rbs +185 -0
- data/stdlib/builtin/module.rbs +1104 -0
- data/stdlib/builtin/nil_class.rbs +82 -0
- data/stdlib/builtin/numeric.rbs +409 -0
- data/stdlib/builtin/object.rbs +824 -0
- data/stdlib/builtin/proc.rbs +429 -0
- data/stdlib/builtin/process.rbs +1227 -0
- data/stdlib/builtin/random.rbs +267 -0
- data/stdlib/builtin/range.rbs +226 -0
- data/stdlib/builtin/rational.rbs +424 -0
- data/stdlib/builtin/rb_config.rbs +57 -0
- data/stdlib/builtin/regexp.rbs +1083 -0
- data/stdlib/builtin/ruby_vm.rbs +14 -0
- data/stdlib/builtin/signal.rbs +55 -0
- data/stdlib/builtin/string.rbs +1901 -0
- data/stdlib/builtin/string_io.rbs +284 -0
- data/stdlib/builtin/struct.rbs +40 -0
- data/stdlib/builtin/symbol.rbs +228 -0
- data/stdlib/builtin/thread.rbs +1108 -0
- data/stdlib/builtin/thread_group.rbs +23 -0
- data/stdlib/builtin/time.rbs +1047 -0
- data/stdlib/builtin/trace_point.rbs +290 -0
- data/stdlib/builtin/true_class.rbs +46 -0
- data/stdlib/builtin/unbound_method.rbs +153 -0
- data/stdlib/builtin/warning.rbs +17 -0
- data/stdlib/coverage/coverage.rbs +62 -0
- data/stdlib/csv/csv.rbs +773 -0
- data/stdlib/erb/erb.rbs +392 -0
- data/stdlib/find/find.rbs +40 -0
- data/stdlib/ipaddr/ipaddr.rbs +247 -0
- data/stdlib/json/json.rbs +335 -0
- data/stdlib/pathname/pathname.rbs +1093 -0
- data/stdlib/prime/integer-extension.rbs +23 -0
- data/stdlib/prime/prime.rbs +188 -0
- data/stdlib/securerandom/securerandom.rbs +9 -0
- data/stdlib/set/set.rbs +301 -0
- data/stdlib/tmpdir/tmpdir.rbs +53 -0
- metadata +292 -0
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
# Document-class: TracePoint
|
|
2
|
+
#
|
|
3
|
+
# A class that provides the functionality of Kernel#set_trace_func in a nice
|
|
4
|
+
# Object-Oriented API.
|
|
5
|
+
#
|
|
6
|
+
# ## Example
|
|
7
|
+
#
|
|
8
|
+
# We can use TracePoint to gather information specifically for exceptions:
|
|
9
|
+
#
|
|
10
|
+
# trace = TracePoint.new(:raise) do |tp|
|
|
11
|
+
# p [tp.lineno, tp.event, tp.raised_exception]
|
|
12
|
+
# end
|
|
13
|
+
# #=> #<TracePoint:disabled>
|
|
14
|
+
#
|
|
15
|
+
# trace.enable
|
|
16
|
+
# #=> false
|
|
17
|
+
#
|
|
18
|
+
# 0 / 0
|
|
19
|
+
# #=> [5, :raise, #<ZeroDivisionError: divided by 0>]
|
|
20
|
+
#
|
|
21
|
+
# ## Events
|
|
22
|
+
#
|
|
23
|
+
# If you don't specify the type of events you want to listen for, TracePoint
|
|
24
|
+
# will include all available events.
|
|
25
|
+
#
|
|
26
|
+
# **Note** do not depend on current event set, as this list is subject to
|
|
27
|
+
# change. Instead, it is recommended you specify the type of events you want to
|
|
28
|
+
# use.
|
|
29
|
+
#
|
|
30
|
+
# To filter what is traced, you can pass any of the following as `events`:
|
|
31
|
+
#
|
|
32
|
+
# `:line`
|
|
33
|
+
# : execute code on a new line
|
|
34
|
+
# `:class`
|
|
35
|
+
# : start a class or module definition
|
|
36
|
+
# `:end`
|
|
37
|
+
# : finish a class or module definition
|
|
38
|
+
# `:call`
|
|
39
|
+
# : call a Ruby method
|
|
40
|
+
# `:return`
|
|
41
|
+
# : return from a Ruby method
|
|
42
|
+
# `:c_call`
|
|
43
|
+
# : call a C-language routine
|
|
44
|
+
# `:c_return`
|
|
45
|
+
# : return from a C-language routine
|
|
46
|
+
# `:raise`
|
|
47
|
+
# : raise an exception
|
|
48
|
+
# `:b_call`
|
|
49
|
+
# : event hook at block entry
|
|
50
|
+
# `:b_return`
|
|
51
|
+
# : event hook at block ending
|
|
52
|
+
# `:thread_begin`
|
|
53
|
+
# : event hook at thread beginning
|
|
54
|
+
# `:thread_end`
|
|
55
|
+
# : event hook at thread ending
|
|
56
|
+
# `:fiber_switch`
|
|
57
|
+
# : event hook at fiber switch
|
|
58
|
+
# `:script_compiled`
|
|
59
|
+
# : new Ruby code compiled (with `eval`, `load` or `require`)
|
|
60
|
+
#
|
|
61
|
+
#
|
|
62
|
+
class TracePoint < Object
|
|
63
|
+
# Returns a new TracePoint object, not enabled by default.
|
|
64
|
+
#
|
|
65
|
+
# Next, in order to activate the trace, you must use TracePoint#enable
|
|
66
|
+
#
|
|
67
|
+
# trace = TracePoint.new(:call) do |tp|
|
|
68
|
+
# p [tp.lineno, tp.defined_class, tp.method_id, tp.event]
|
|
69
|
+
# end
|
|
70
|
+
# #=> #<TracePoint:disabled>
|
|
71
|
+
#
|
|
72
|
+
# trace.enable
|
|
73
|
+
# #=> false
|
|
74
|
+
#
|
|
75
|
+
# puts "Hello, TracePoint!"
|
|
76
|
+
# # ...
|
|
77
|
+
# # [48, IRB::Notifier::AbstractNotifier, :printf, :call]
|
|
78
|
+
# # ...
|
|
79
|
+
#
|
|
80
|
+
# When you want to deactivate the trace, you must use TracePoint#disable
|
|
81
|
+
#
|
|
82
|
+
# trace.disable
|
|
83
|
+
#
|
|
84
|
+
# See TracePoint@Events for possible events and more information.
|
|
85
|
+
#
|
|
86
|
+
# A block must be given, otherwise an ArgumentError is raised.
|
|
87
|
+
#
|
|
88
|
+
# If the trace method isn't included in the given events filter, a RuntimeError
|
|
89
|
+
# is raised.
|
|
90
|
+
#
|
|
91
|
+
# TracePoint.trace(:line) do |tp|
|
|
92
|
+
# p tp.raised_exception
|
|
93
|
+
# end
|
|
94
|
+
# #=> RuntimeError: 'raised_exception' not supported by this event
|
|
95
|
+
#
|
|
96
|
+
# If the trace method is called outside block, a RuntimeError is raised.
|
|
97
|
+
#
|
|
98
|
+
# TracePoint.trace(:line) do |tp|
|
|
99
|
+
# $tp = tp
|
|
100
|
+
# end
|
|
101
|
+
# $tp.lineno #=> access from outside (RuntimeError)
|
|
102
|
+
#
|
|
103
|
+
# Access from other threads is also forbidden.
|
|
104
|
+
#
|
|
105
|
+
def initialize: (*Symbol events) { (TracePoint tp) -> void } -> void
|
|
106
|
+
|
|
107
|
+
# Returns internal information of TracePoint.
|
|
108
|
+
#
|
|
109
|
+
# The contents of the returned value are implementation specific. It may be
|
|
110
|
+
# changed in future.
|
|
111
|
+
#
|
|
112
|
+
# This method is only for debugging TracePoint itself.
|
|
113
|
+
#
|
|
114
|
+
def self.stat: () -> untyped
|
|
115
|
+
|
|
116
|
+
# Document-method: trace
|
|
117
|
+
#
|
|
118
|
+
# A convenience method for TracePoint.new, that activates the trace
|
|
119
|
+
# automatically.
|
|
120
|
+
#
|
|
121
|
+
# trace = TracePoint.trace(:call) { |tp| [tp.lineno, tp.event] }
|
|
122
|
+
# #=> #<TracePoint:enabled>
|
|
123
|
+
#
|
|
124
|
+
# trace.enabled? #=> true
|
|
125
|
+
#
|
|
126
|
+
def self.trace: (*Symbol events) { (TracePoint tp) -> void } -> TracePoint
|
|
127
|
+
|
|
128
|
+
# Return the generated binding object from event
|
|
129
|
+
#
|
|
130
|
+
def binding: () -> Binding
|
|
131
|
+
|
|
132
|
+
# Return the called name of the method being called
|
|
133
|
+
#
|
|
134
|
+
def callee_id: () -> Symbol
|
|
135
|
+
|
|
136
|
+
# Return class or module of the method being called.
|
|
137
|
+
#
|
|
138
|
+
# class C; def foo; end; end
|
|
139
|
+
# trace = TracePoint.new(:call) do |tp|
|
|
140
|
+
# p tp.defined_class #=> C
|
|
141
|
+
# end.enable do
|
|
142
|
+
# C.new.foo
|
|
143
|
+
# end
|
|
144
|
+
#
|
|
145
|
+
# If method is defined by a module, then that module is returned.
|
|
146
|
+
#
|
|
147
|
+
# module M; def foo; end; end
|
|
148
|
+
# class C; include M; end;
|
|
149
|
+
# trace = TracePoint.new(:call) do |tp|
|
|
150
|
+
# p tp.defined_class #=> M
|
|
151
|
+
# end.enable do
|
|
152
|
+
# C.new.foo
|
|
153
|
+
# end
|
|
154
|
+
#
|
|
155
|
+
# **Note:** #defined_class returns singleton class.
|
|
156
|
+
#
|
|
157
|
+
# 6th block parameter of Kernel#set_trace_func passes original class of attached
|
|
158
|
+
# by singleton class.
|
|
159
|
+
#
|
|
160
|
+
# **This is a difference between Kernel#set_trace_func and TracePoint.**
|
|
161
|
+
#
|
|
162
|
+
# class C; def self.foo; end; end
|
|
163
|
+
# trace = TracePoint.new(:call) do |tp|
|
|
164
|
+
# p tp.defined_class #=> #<Class:C>
|
|
165
|
+
# end.enable do
|
|
166
|
+
# C.foo
|
|
167
|
+
# end
|
|
168
|
+
#
|
|
169
|
+
def defined_class: () -> Module
|
|
170
|
+
|
|
171
|
+
# Deactivates the trace
|
|
172
|
+
#
|
|
173
|
+
# Return true if trace was enabled. Return false if trace was disabled.
|
|
174
|
+
#
|
|
175
|
+
# trace.enabled? #=> true
|
|
176
|
+
# trace.disable #=> true (previous status)
|
|
177
|
+
# trace.enabled? #=> false
|
|
178
|
+
# trace.disable #=> false
|
|
179
|
+
#
|
|
180
|
+
# If a block is given, the trace will only be disable within the scope of the
|
|
181
|
+
# block.
|
|
182
|
+
#
|
|
183
|
+
# trace.enabled?
|
|
184
|
+
# #=> true
|
|
185
|
+
#
|
|
186
|
+
# trace.disable do
|
|
187
|
+
# trace.enabled?
|
|
188
|
+
# # only disabled for this block
|
|
189
|
+
# end
|
|
190
|
+
#
|
|
191
|
+
# trace.enabled?
|
|
192
|
+
# #=> true
|
|
193
|
+
#
|
|
194
|
+
# Note: You cannot access event hooks within the block.
|
|
195
|
+
#
|
|
196
|
+
# trace.disable { p tp.lineno }
|
|
197
|
+
# #=> RuntimeError: access from outside
|
|
198
|
+
#
|
|
199
|
+
def disable: () -> bool
|
|
200
|
+
| () { () -> void } -> void
|
|
201
|
+
|
|
202
|
+
# Activates the trace.
|
|
203
|
+
#
|
|
204
|
+
# Returns `true` if trace was enabled. Returns `false` if trace was disabled.
|
|
205
|
+
#
|
|
206
|
+
# trace.enabled? #=> false
|
|
207
|
+
# trace.enable #=> false (previous state)
|
|
208
|
+
# # trace is enabled
|
|
209
|
+
# trace.enabled? #=> true
|
|
210
|
+
# trace.enable #=> true (previous state)
|
|
211
|
+
# # trace is still enabled
|
|
212
|
+
#
|
|
213
|
+
# If a block is given, the trace will only be enabled within the scope of the
|
|
214
|
+
# block.
|
|
215
|
+
#
|
|
216
|
+
# trace.enabled?
|
|
217
|
+
# #=> false
|
|
218
|
+
#
|
|
219
|
+
# trace.enable do
|
|
220
|
+
# trace.enabled?
|
|
221
|
+
# # only enabled for this block
|
|
222
|
+
# end
|
|
223
|
+
#
|
|
224
|
+
# trace.enabled?
|
|
225
|
+
# #=> false
|
|
226
|
+
#
|
|
227
|
+
# `target`, `target_line` and `target_thread` parameters are used to limit
|
|
228
|
+
# tracing only to specified code objects. `target` should be a code object for
|
|
229
|
+
# which RubyVM::InstructionSequence.of will return an instruction sequence.
|
|
230
|
+
#
|
|
231
|
+
# t = TracePoint.new(:line) { |tp| p tp }
|
|
232
|
+
#
|
|
233
|
+
# def m1
|
|
234
|
+
# p 1
|
|
235
|
+
# end
|
|
236
|
+
#
|
|
237
|
+
# def m2
|
|
238
|
+
# p 2
|
|
239
|
+
# end
|
|
240
|
+
#
|
|
241
|
+
# t.enable(target: method(:m1))
|
|
242
|
+
#
|
|
243
|
+
# m1
|
|
244
|
+
# # prints #<TracePoint:line@test.rb:5 in `m1'>
|
|
245
|
+
# m2
|
|
246
|
+
# # prints nothing
|
|
247
|
+
#
|
|
248
|
+
# Note: You cannot access event hooks within the `enable` block.
|
|
249
|
+
#
|
|
250
|
+
# trace.enable { p tp.lineno }
|
|
251
|
+
# #=> RuntimeError: access from outside
|
|
252
|
+
#
|
|
253
|
+
def enable: () -> bool
|
|
254
|
+
| () { () -> void } -> void
|
|
255
|
+
|
|
256
|
+
# The current status of the trace
|
|
257
|
+
#
|
|
258
|
+
def enabled?: () -> bool
|
|
259
|
+
|
|
260
|
+
# Return a string containing a human-readable TracePoint status.
|
|
261
|
+
#
|
|
262
|
+
def inspect: () -> String
|
|
263
|
+
|
|
264
|
+
# Line number of the event
|
|
265
|
+
#
|
|
266
|
+
def lineno: () -> Integer
|
|
267
|
+
|
|
268
|
+
# Return the name at the definition of the method being called
|
|
269
|
+
#
|
|
270
|
+
def method_id: () -> Symbol
|
|
271
|
+
|
|
272
|
+
# Path of the file being run
|
|
273
|
+
#
|
|
274
|
+
def path: () -> String
|
|
275
|
+
|
|
276
|
+
# Value from exception raised on the `:raise` event
|
|
277
|
+
#
|
|
278
|
+
def raised_exception: () -> untyped
|
|
279
|
+
|
|
280
|
+
# Return value from `:return`, `c_return`, and `b_return` event
|
|
281
|
+
#
|
|
282
|
+
def return_value: () -> untyped
|
|
283
|
+
|
|
284
|
+
# Return the trace object during event
|
|
285
|
+
#
|
|
286
|
+
# Same as TracePoint#binding:
|
|
287
|
+
# trace.binding.eval('self')
|
|
288
|
+
#
|
|
289
|
+
def `self`: () -> Binding
|
|
290
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# The global value `true` is the only instance of class TrueClass and represents
|
|
2
|
+
# a logically true value in boolean expressions. The class provides operators
|
|
3
|
+
# allowing `true` to be used in logical expressions.
|
|
4
|
+
#
|
|
5
|
+
class TrueClass
|
|
6
|
+
public
|
|
7
|
+
|
|
8
|
+
def !: () -> bool
|
|
9
|
+
|
|
10
|
+
# And---Returns `false` if *obj* is `nil` or `false`, `true` otherwise.
|
|
11
|
+
#
|
|
12
|
+
def &: (nil) -> false
|
|
13
|
+
| (false) -> false
|
|
14
|
+
| (untyped obj) -> bool
|
|
15
|
+
|
|
16
|
+
# Case Equality -- For class Object, effectively the same as calling `#==`, but
|
|
17
|
+
# typically overridden by descendants to provide meaningful semantics in `case`
|
|
18
|
+
# statements.
|
|
19
|
+
#
|
|
20
|
+
def ===: (true) -> true
|
|
21
|
+
| (untyped obj) -> bool
|
|
22
|
+
|
|
23
|
+
# Exclusive Or---Returns `true` if *obj* is `nil` or `false`, `false` otherwise.
|
|
24
|
+
#
|
|
25
|
+
def ^: (nil) -> true
|
|
26
|
+
| (false) -> true
|
|
27
|
+
| (untyped obj) -> bool
|
|
28
|
+
|
|
29
|
+
alias inspect to_s
|
|
30
|
+
|
|
31
|
+
# The string representation of `true` is "true".
|
|
32
|
+
#
|
|
33
|
+
def to_s: () -> "true"
|
|
34
|
+
|
|
35
|
+
# Or---Returns `true`. As *obj* is an argument to a method call, it is always
|
|
36
|
+
# evaluated; there is no short-circuit evaluation in this case.
|
|
37
|
+
#
|
|
38
|
+
# true | puts("or")
|
|
39
|
+
# true || puts("logical or")
|
|
40
|
+
#
|
|
41
|
+
# *produces:*
|
|
42
|
+
#
|
|
43
|
+
# or
|
|
44
|
+
#
|
|
45
|
+
def |: (bool obj) -> bool
|
|
46
|
+
end
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
# Ruby supports two forms of objectified methods. Class Method is used to
|
|
2
|
+
# represent methods that are associated with a particular object: these method
|
|
3
|
+
# objects are bound to that object. Bound method objects for an object can be
|
|
4
|
+
# created using Object#method.
|
|
5
|
+
#
|
|
6
|
+
# Ruby also supports unbound methods; methods objects that are not associated
|
|
7
|
+
# with a particular object. These can be created either by calling
|
|
8
|
+
# Module#instance_method or by calling #unbind on a bound method object. The
|
|
9
|
+
# result of both of these is an UnboundMethod object.
|
|
10
|
+
#
|
|
11
|
+
# Unbound methods can only be called after they are bound to an object. That
|
|
12
|
+
# object must be a kind_of? the method's original class.
|
|
13
|
+
#
|
|
14
|
+
# class Square
|
|
15
|
+
# def area
|
|
16
|
+
# @side * @side
|
|
17
|
+
# end
|
|
18
|
+
# def initialize(side)
|
|
19
|
+
# @side = side
|
|
20
|
+
# end
|
|
21
|
+
# end
|
|
22
|
+
#
|
|
23
|
+
# area_un = Square.instance_method(:area)
|
|
24
|
+
#
|
|
25
|
+
# s = Square.new(12)
|
|
26
|
+
# area = area_un.bind(s)
|
|
27
|
+
# area.call #=> 144
|
|
28
|
+
#
|
|
29
|
+
# Unbound methods are a reference to the method at the time it was objectified:
|
|
30
|
+
# subsequent changes to the underlying class will not affect the unbound method.
|
|
31
|
+
#
|
|
32
|
+
# class Test
|
|
33
|
+
# def test
|
|
34
|
+
# :original
|
|
35
|
+
# end
|
|
36
|
+
# end
|
|
37
|
+
# um = Test.instance_method(:test)
|
|
38
|
+
# class Test
|
|
39
|
+
# def test
|
|
40
|
+
# :modified
|
|
41
|
+
# end
|
|
42
|
+
# end
|
|
43
|
+
# t = Test.new
|
|
44
|
+
# t.test #=> :modified
|
|
45
|
+
# um.bind(t).call #=> :original
|
|
46
|
+
#
|
|
47
|
+
class UnboundMethod
|
|
48
|
+
# Returns an indication of the number of arguments accepted by a method. Returns
|
|
49
|
+
# a nonnegative integer for methods that take a fixed number of arguments. For
|
|
50
|
+
# Ruby methods that take a variable number of arguments, returns -n-1, where n
|
|
51
|
+
# is the number of required arguments. Keyword arguments will be considered as a
|
|
52
|
+
# single additional argument, that argument being mandatory if any keyword
|
|
53
|
+
# argument is mandatory. For methods written in C, returns -1 if the call takes
|
|
54
|
+
# a variable number of arguments.
|
|
55
|
+
#
|
|
56
|
+
# class C
|
|
57
|
+
# def one; end
|
|
58
|
+
# def two(a); end
|
|
59
|
+
# def three(*a); end
|
|
60
|
+
# def four(a, b); end
|
|
61
|
+
# def five(a, b, *c); end
|
|
62
|
+
# def six(a, b, *c, &d); end
|
|
63
|
+
# def seven(a, b, x:0); end
|
|
64
|
+
# def eight(x:, y:); end
|
|
65
|
+
# def nine(x:, y:, **z); end
|
|
66
|
+
# def ten(*a, x:, y:); end
|
|
67
|
+
# end
|
|
68
|
+
# c = C.new
|
|
69
|
+
# c.method(:one).arity #=> 0
|
|
70
|
+
# c.method(:two).arity #=> 1
|
|
71
|
+
# c.method(:three).arity #=> -1
|
|
72
|
+
# c.method(:four).arity #=> 2
|
|
73
|
+
# c.method(:five).arity #=> -3
|
|
74
|
+
# c.method(:six).arity #=> -3
|
|
75
|
+
# c.method(:seven).arity #=> -3
|
|
76
|
+
# c.method(:eight).arity #=> 1
|
|
77
|
+
# c.method(:nine).arity #=> 1
|
|
78
|
+
# c.method(:ten).arity #=> -2
|
|
79
|
+
#
|
|
80
|
+
# "cat".method(:size).arity #=> 0
|
|
81
|
+
# "cat".method(:replace).arity #=> 1
|
|
82
|
+
# "cat".method(:squeeze).arity #=> -1
|
|
83
|
+
# "cat".method(:count).arity #=> -1
|
|
84
|
+
#
|
|
85
|
+
def arity: () -> Integer
|
|
86
|
+
|
|
87
|
+
# Bind *umeth* to *obj*. If Klass was the class from which *umeth* was obtained,
|
|
88
|
+
# `obj.kind_of?(Klass)` must be true.
|
|
89
|
+
#
|
|
90
|
+
# class A
|
|
91
|
+
# def test
|
|
92
|
+
# puts "In test, class = #{self.class}"
|
|
93
|
+
# end
|
|
94
|
+
# end
|
|
95
|
+
# class B < A
|
|
96
|
+
# end
|
|
97
|
+
# class C < B
|
|
98
|
+
# end
|
|
99
|
+
#
|
|
100
|
+
# um = B.instance_method(:test)
|
|
101
|
+
# bm = um.bind(C.new)
|
|
102
|
+
# bm.call
|
|
103
|
+
# bm = um.bind(B.new)
|
|
104
|
+
# bm.call
|
|
105
|
+
# bm = um.bind(A.new)
|
|
106
|
+
# bm.call
|
|
107
|
+
#
|
|
108
|
+
# *produces:*
|
|
109
|
+
#
|
|
110
|
+
# In test, class = C
|
|
111
|
+
# In test, class = B
|
|
112
|
+
# prog.rb:16:in `bind': bind argument must be an instance of B (TypeError)
|
|
113
|
+
# from prog.rb:16
|
|
114
|
+
#
|
|
115
|
+
def bind: (untyped obj) -> Method
|
|
116
|
+
|
|
117
|
+
# Returns the name of the method.
|
|
118
|
+
#
|
|
119
|
+
def name: () -> Symbol
|
|
120
|
+
|
|
121
|
+
# Returns the class or module that defines the method. See also Method#receiver.
|
|
122
|
+
#
|
|
123
|
+
# (1..3).method(:map).owner #=> Enumerable
|
|
124
|
+
#
|
|
125
|
+
def owner: () -> Module
|
|
126
|
+
|
|
127
|
+
# Returns the parameter information of this method.
|
|
128
|
+
#
|
|
129
|
+
# def foo(bar); end
|
|
130
|
+
# method(:foo).parameters #=> [[:req, :bar]]
|
|
131
|
+
#
|
|
132
|
+
# def foo(bar, baz, bat, &blk); end
|
|
133
|
+
# method(:foo).parameters #=> [[:req, :bar], [:req, :baz], [:req, :bat], [:block, :blk]]
|
|
134
|
+
#
|
|
135
|
+
# def foo(bar, *args); end
|
|
136
|
+
# method(:foo).parameters #=> [[:req, :bar], [:rest, :args]]
|
|
137
|
+
#
|
|
138
|
+
# def foo(bar, baz, *args, &blk); end
|
|
139
|
+
# method(:foo).parameters #=> [[:req, :bar], [:req, :baz], [:rest, :args], [:block, :blk]]
|
|
140
|
+
#
|
|
141
|
+
def parameters: () -> ::Array[[ Symbol, Symbol ]]
|
|
142
|
+
| () -> ::Array[[ Symbol ]]
|
|
143
|
+
|
|
144
|
+
# Returns the Ruby source filename and line number containing this method or nil
|
|
145
|
+
# if this method was not defined in Ruby (i.e. native).
|
|
146
|
+
#
|
|
147
|
+
def source_location: () -> [ String, Integer ]?
|
|
148
|
+
|
|
149
|
+
# Returns a Method of superclass which would be called when super is used or nil
|
|
150
|
+
# if there is no method on superclass.
|
|
151
|
+
#
|
|
152
|
+
def super_method: () -> UnboundMethod?
|
|
153
|
+
end
|