rbs 0.13.1 → 0.14.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +1 -1
- data/.gitignore +0 -1
- data/CHANGELOG.md +7 -2
- data/Gemfile +3 -0
- data/README.md +8 -2
- data/Steepfile +1 -0
- data/bin/annotate-with-rdoc +1 -1
- data/bin/setup +0 -2
- data/docs/CONTRIBUTING.md +1 -0
- data/goodcheck.yml +22 -5
- data/lib/rbs/ast/comment.rb +1 -1
- data/lib/rbs/definition_builder.rb +4 -5
- data/lib/rbs/environment.rb +1 -1
- data/lib/rbs/namespace.rb +1 -1
- data/lib/rbs/parser.rb +3146 -0
- data/lib/rbs/parser.y +7 -2
- data/lib/rbs/test/setup_helper.rb +4 -4
- data/lib/rbs/test/type_check.rb +2 -2
- data/lib/rbs/type_name.rb +1 -1
- data/lib/rbs/variance_calculator.rb +1 -1
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs/writer.rb +1 -1
- data/sig/constant.rbs +2 -2
- data/sig/constant_table.rbs +10 -10
- data/sig/declarations.rbs +1 -1
- data/sig/definition.rbs +1 -1
- data/sig/namespace.rbs +3 -3
- data/sig/parser.rbs +25 -0
- data/sig/substitution.rbs +3 -3
- data/sig/typename.rbs +1 -1
- data/sig/types.rbs +1 -1
- data/sig/writer.rbs +15 -15
- data/stdlib/benchmark/benchmark.rbs +2 -2
- data/stdlib/builtin/basic_object.rbs +54 -54
- data/stdlib/builtin/binding.rbs +42 -42
- data/stdlib/builtin/class.rbs +33 -33
- data/stdlib/builtin/complex.rbs +90 -90
- data/stdlib/builtin/encoding.rbs +33 -33
- data/stdlib/builtin/enumerable.rbs +32 -32
- data/stdlib/builtin/enumerator.rbs +35 -35
- data/stdlib/builtin/errors.rbs +1 -1
- data/stdlib/builtin/exception.rbs +50 -50
- data/stdlib/builtin/false_class.rbs +6 -6
- data/stdlib/builtin/fiber.rbs +14 -14
- data/stdlib/builtin/fiber_error.rbs +1 -1
- data/stdlib/builtin/float.rbs +161 -161
- data/stdlib/builtin/gc.rbs +1 -1
- data/stdlib/builtin/io.rbs +83 -83
- data/stdlib/builtin/kernel.rbs +69 -69
- data/stdlib/builtin/match_data.rbs +1 -1
- data/stdlib/builtin/method.rbs +19 -19
- data/stdlib/builtin/nil_class.rbs +20 -20
- data/stdlib/builtin/numeric.rbs +101 -101
- data/stdlib/builtin/object.rbs +172 -172
- data/stdlib/builtin/proc.rbs +91 -91
- data/stdlib/builtin/range.rbs +2 -4
- data/stdlib/builtin/rational.rbs +83 -83
- data/stdlib/builtin/signal.rbs +7 -7
- data/stdlib/builtin/string.rbs +4 -4
- data/stdlib/builtin/string_io.rbs +1 -1
- data/stdlib/builtin/thread.rbs +185 -185
- data/stdlib/builtin/thread_group.rbs +2 -2
- data/stdlib/builtin/true_class.rbs +9 -9
- data/stdlib/builtin/warning.rbs +1 -1
- data/stdlib/date/date.rbs +2 -2
- data/stdlib/find/find.rbs +10 -10
- data/stdlib/pathname/pathname.rbs +1 -1
- data/stdlib/tmpdir/tmpdir.rbs +12 -12
- metadata +3 -2
data/stdlib/builtin/object.rbs
CHANGED
@@ -1,68 +1,68 @@
|
|
1
1
|
# Object is the default root of all Ruby objects. Object inherits from
|
2
2
|
# BasicObject which allows creating alternate object hierarchies. Methods on
|
3
3
|
# Object are available to all classes unless explicitly overridden.
|
4
|
-
#
|
4
|
+
#
|
5
5
|
# Object mixes in the Kernel module, making the built-in kernel functions
|
6
6
|
# globally accessible. Although the instance methods of Object are defined by
|
7
7
|
# the Kernel module, we have chosen to document them here for clarity.
|
8
|
-
#
|
8
|
+
#
|
9
9
|
# When referencing constants in classes inheriting from Object you do not need
|
10
10
|
# to use the full namespace. For example, referencing `File` inside `YourClass`
|
11
11
|
# will find the top-level File class.
|
12
|
-
#
|
12
|
+
#
|
13
13
|
# In the descriptions of Object's methods, the parameter *symbol* refers to a
|
14
14
|
# symbol, which is either a quoted string or a Symbol (such as `:name`).
|
15
|
-
#
|
15
|
+
#
|
16
16
|
class Object < BasicObject
|
17
17
|
include Kernel
|
18
18
|
|
19
19
|
# Returns true if two objects do not match (using the *=~* method), otherwise
|
20
20
|
# false.
|
21
|
-
#
|
21
|
+
#
|
22
22
|
def !~: (untyped) -> bool
|
23
23
|
|
24
24
|
# Returns 0 if `obj` and `other` are the same object or `obj == other`,
|
25
25
|
# otherwise nil.
|
26
|
-
#
|
26
|
+
#
|
27
27
|
# The `<=>` is used by various methods to compare objects, for example
|
28
28
|
# Enumerable#sort, Enumerable#max etc.
|
29
|
-
#
|
29
|
+
#
|
30
30
|
# Your implementation of `<=>` should return one of the following values: -1, 0,
|
31
31
|
# 1 or nil. -1 means self is smaller than other. 0 means self is equal to other.
|
32
32
|
# 1 means self is bigger than other. Nil means the two values could not be
|
33
33
|
# compared.
|
34
|
-
#
|
34
|
+
#
|
35
35
|
# When you define `<=>`, you can include Comparable to gain the methods `<=`,
|
36
36
|
# `<`, `==`, `>=`, `>` and `between?`.
|
37
|
-
#
|
37
|
+
#
|
38
38
|
def <=>: (untyped) -> Integer?
|
39
39
|
|
40
40
|
# Case Equality -- For class Object, effectively the same as calling `#==`, but
|
41
41
|
# typically overridden by descendants to provide meaningful semantics in `case`
|
42
42
|
# statements.
|
43
|
-
#
|
43
|
+
#
|
44
44
|
def ===: (untyped) -> bool
|
45
45
|
|
46
46
|
# This method is deprecated.
|
47
|
-
#
|
47
|
+
#
|
48
48
|
# This is not only unuseful but also troublesome because it may hide a type
|
49
49
|
# error.
|
50
|
-
#
|
50
|
+
#
|
51
51
|
def =~: (untyped) -> bool
|
52
52
|
|
53
53
|
# Returns the class of *obj*. This method must always be called with an explicit
|
54
54
|
# receiver, as `class` is also a reserved word in Ruby.
|
55
|
-
#
|
55
|
+
#
|
56
56
|
# 1.class #=> Integer
|
57
57
|
# self.class #=> Object
|
58
|
-
#
|
58
|
+
#
|
59
59
|
def `class`: () -> untyped
|
60
60
|
|
61
61
|
# Produces a shallow copy of *obj*---the instance variables of *obj* are copied,
|
62
62
|
# but not the objects they reference. `clone` copies the frozen (unless :freeze
|
63
63
|
# keyword argument is given with a false value) and tainted state of *obj*. See
|
64
64
|
# also the discussion under `Object#dup`.
|
65
|
-
#
|
65
|
+
#
|
66
66
|
# class Klass
|
67
67
|
# attr_accessor :str
|
68
68
|
# end
|
@@ -72,16 +72,16 @@ class Object < BasicObject
|
|
72
72
|
# s2.str[1,4] = "i" #=> "i"
|
73
73
|
# s1.inspect #=> "#<Klass:0x401b3a38 @str=\"Hi\">"
|
74
74
|
# s2.inspect #=> "#<Klass:0x401b3998 @str=\"Hi\">"
|
75
|
-
#
|
75
|
+
#
|
76
76
|
# This method may have class-specific behavior. If so, that behavior will be
|
77
77
|
# documented under the #`initialize_copy` method of the class.
|
78
|
-
#
|
78
|
+
#
|
79
79
|
def clone: (?freeze: bool) -> self
|
80
80
|
|
81
81
|
# Defines a singleton method in the receiver. The *method* parameter can be a
|
82
82
|
# `Proc`, a `Method` or an `UnboundMethod` object. If a block is specified, it
|
83
83
|
# is used as the method body.
|
84
|
-
#
|
84
|
+
#
|
85
85
|
# class A
|
86
86
|
# class << self
|
87
87
|
# def class_name
|
@@ -93,95 +93,95 @@ class Object < BasicObject
|
|
93
93
|
# "I am: #{class_name}"
|
94
94
|
# end
|
95
95
|
# A.who_am_i # ==> "I am: A"
|
96
|
-
#
|
96
|
+
#
|
97
97
|
# guy = "Bob"
|
98
98
|
# guy.define_singleton_method(:hello) { "#{self}: Hello there!" }
|
99
99
|
# guy.hello #=> "Bob: Hello there!"
|
100
|
-
#
|
100
|
+
#
|
101
101
|
def define_singleton_method: (Symbol, Method | UnboundMethod) -> Symbol
|
102
102
|
| (Symbol) { (*untyped) -> untyped } -> Symbol
|
103
103
|
|
104
104
|
# Prints *obj* on the given port (default `$>`). Equivalent to:
|
105
|
-
#
|
105
|
+
#
|
106
106
|
# def display(port=$>)
|
107
107
|
# port.write self
|
108
108
|
# nil
|
109
109
|
# end
|
110
|
-
#
|
110
|
+
#
|
111
111
|
# For example:
|
112
|
-
#
|
112
|
+
#
|
113
113
|
# 1.display
|
114
114
|
# "cat".display
|
115
115
|
# [ 4, 5, 6 ].display
|
116
116
|
# puts
|
117
|
-
#
|
117
|
+
#
|
118
118
|
# *produces:*
|
119
|
-
#
|
119
|
+
#
|
120
120
|
# 1cat[4, 5, 6]
|
121
|
-
#
|
121
|
+
#
|
122
122
|
def display: (?_Writeable port) -> void
|
123
123
|
|
124
124
|
# Produces a shallow copy of *obj*---the instance variables of *obj* are copied,
|
125
125
|
# but not the objects they reference. `dup` copies the tainted state of *obj*.
|
126
|
-
#
|
126
|
+
#
|
127
127
|
# This method may have class-specific behavior. If so, that behavior will be
|
128
128
|
# documented under the #`initialize_copy` method of the class.
|
129
|
-
#
|
129
|
+
#
|
130
130
|
# ### on dup vs clone
|
131
|
-
#
|
131
|
+
#
|
132
132
|
# In general, `clone` and `dup` may have different semantics in descendant
|
133
133
|
# classes. While `clone` is used to duplicate an object, including its internal
|
134
134
|
# state, `dup` typically uses the class of the descendant object to create the
|
135
135
|
# new instance.
|
136
|
-
#
|
136
|
+
#
|
137
137
|
# When using #dup, any modules that the object has been extended with will not
|
138
138
|
# be copied.
|
139
|
-
#
|
139
|
+
#
|
140
140
|
# class Klass
|
141
141
|
# attr_accessor :str
|
142
142
|
# end
|
143
|
-
#
|
143
|
+
#
|
144
144
|
# module Foo
|
145
145
|
# def foo; 'foo'; end
|
146
146
|
# end
|
147
|
-
#
|
147
|
+
#
|
148
148
|
# s1 = Klass.new #=> #<Klass:0x401b3a38>
|
149
149
|
# s1.extend(Foo) #=> #<Klass:0x401b3a38>
|
150
150
|
# s1.foo #=> "foo"
|
151
|
-
#
|
151
|
+
#
|
152
152
|
# s2 = s1.clone #=> #<Klass:0x401b3a38>
|
153
153
|
# s2.foo #=> "foo"
|
154
|
-
#
|
154
|
+
#
|
155
155
|
# s3 = s1.dup #=> #<Klass:0x401b3a38>
|
156
156
|
# s3.foo #=> NoMethodError: undefined method `foo' for #<Klass:0x401b3a38>
|
157
|
-
#
|
157
|
+
#
|
158
158
|
def dup: () -> self
|
159
159
|
|
160
160
|
# Creates a new Enumerator which will enumerate by calling `method` on `obj`,
|
161
161
|
# passing `args` if any.
|
162
|
-
#
|
162
|
+
#
|
163
163
|
# If a block is given, it will be used to calculate the size of the enumerator
|
164
164
|
# without the need to iterate it (see Enumerator#size).
|
165
|
-
#
|
165
|
+
#
|
166
166
|
# ### Examples
|
167
|
-
#
|
167
|
+
#
|
168
168
|
# str = "xyz"
|
169
|
-
#
|
169
|
+
#
|
170
170
|
# enum = str.enum_for(:each_byte)
|
171
171
|
# enum.each { |b| puts b }
|
172
172
|
# # => 120
|
173
173
|
# # => 121
|
174
174
|
# # => 122
|
175
|
-
#
|
175
|
+
#
|
176
176
|
# # protect an array from being modified by some_method
|
177
177
|
# a = [1, 2, 3]
|
178
178
|
# some_method(a.to_enum)
|
179
|
-
#
|
179
|
+
#
|
180
180
|
# It is typical to call to_enum when defining methods for a generic Enumerable,
|
181
181
|
# in case no block is passed.
|
182
|
-
#
|
182
|
+
#
|
183
183
|
# Here is such an example, with parameter passing and a sizing block:
|
184
|
-
#
|
184
|
+
#
|
185
185
|
# module Enumerable
|
186
186
|
# # a generic method to repeat the values of any enumerable
|
187
187
|
# def repeat(n)
|
@@ -197,42 +197,42 @@ class Object < BasicObject
|
|
197
197
|
# end
|
198
198
|
# end
|
199
199
|
# end
|
200
|
-
#
|
200
|
+
#
|
201
201
|
# %i[hello world].repeat(2) { |w| puts w }
|
202
202
|
# # => Prints 'hello', 'hello', 'world', 'world'
|
203
203
|
# enum = (1..14).repeat(3)
|
204
204
|
# # => returns an Enumerator when called without a block
|
205
205
|
# enum.first(4) # => [1, 1, 1, 2]
|
206
206
|
# enum.size # => 42
|
207
|
-
#
|
207
|
+
#
|
208
208
|
def enum_for: (Symbol method, *untyped args) ?{ (*untyped args) -> Integer } -> Enumerator[untyped, untyped]
|
209
209
|
| (*untyped args) ?{ (*untyped args) -> Integer } -> Enumerator[untyped, untyped]
|
210
210
|
|
211
211
|
# Creates a new Enumerator which will enumerate by calling `method` on `obj`,
|
212
212
|
# passing `args` if any.
|
213
|
-
#
|
213
|
+
#
|
214
214
|
# If a block is given, it will be used to calculate the size of the enumerator
|
215
215
|
# without the need to iterate it (see Enumerator#size).
|
216
|
-
#
|
216
|
+
#
|
217
217
|
# ### Examples
|
218
|
-
#
|
218
|
+
#
|
219
219
|
# str = "xyz"
|
220
|
-
#
|
220
|
+
#
|
221
221
|
# enum = str.enum_for(:each_byte)
|
222
222
|
# enum.each { |b| puts b }
|
223
223
|
# # => 120
|
224
224
|
# # => 121
|
225
225
|
# # => 122
|
226
|
-
#
|
226
|
+
#
|
227
227
|
# # protect an array from being modified by some_method
|
228
228
|
# a = [1, 2, 3]
|
229
229
|
# some_method(a.to_enum)
|
230
|
-
#
|
230
|
+
#
|
231
231
|
# It is typical to call to_enum when defining methods for a generic Enumerable,
|
232
232
|
# in case no block is passed.
|
233
|
-
#
|
233
|
+
#
|
234
234
|
# Here is such an example, with parameter passing and a sizing block:
|
235
|
-
#
|
235
|
+
#
|
236
236
|
# module Enumerable
|
237
237
|
# # a generic method to repeat the values of any enumerable
|
238
238
|
# def repeat(n)
|
@@ -248,89 +248,89 @@ class Object < BasicObject
|
|
248
248
|
# end
|
249
249
|
# end
|
250
250
|
# end
|
251
|
-
#
|
251
|
+
#
|
252
252
|
# %i[hello world].repeat(2) { |w| puts w }
|
253
253
|
# # => Prints 'hello', 'hello', 'world', 'world'
|
254
254
|
# enum = (1..14).repeat(3)
|
255
255
|
# # => returns an Enumerator when called without a block
|
256
256
|
# enum.first(4) # => [1, 1, 1, 2]
|
257
257
|
# enum.size # => 42
|
258
|
-
#
|
258
|
+
#
|
259
259
|
alias to_enum enum_for
|
260
260
|
|
261
261
|
# Equality --- At the `Object` level, `==` returns `true` only if `obj` and
|
262
262
|
# `other` are the same object. Typically, this method is overridden in
|
263
263
|
# descendant classes to provide class-specific meaning.
|
264
|
-
#
|
264
|
+
#
|
265
265
|
# Unlike `==`, the `equal?` method should never be overridden by subclasses as
|
266
266
|
# it is used to determine object identity (that is, `a.equal?(b)` if and only if
|
267
267
|
# `a` is the same object as `b`):
|
268
|
-
#
|
268
|
+
#
|
269
269
|
# obj = "a"
|
270
270
|
# other = obj.dup
|
271
|
-
#
|
271
|
+
#
|
272
272
|
# obj == other #=> true
|
273
273
|
# obj.equal? other #=> false
|
274
274
|
# obj.equal? obj #=> true
|
275
|
-
#
|
275
|
+
#
|
276
276
|
# The `eql?` method returns `true` if `obj` and `other` refer to the same hash
|
277
277
|
# key. This is used by Hash to test members for equality. For objects of class
|
278
278
|
# `Object`, `eql?` is synonymous with `==`. Subclasses normally continue this
|
279
279
|
# tradition by aliasing `eql?` to their overridden `==` method, but there are
|
280
280
|
# exceptions. `Numeric` types, for example, perform type conversion across
|
281
281
|
# `==`, but not across `eql?`, so:
|
282
|
-
#
|
282
|
+
#
|
283
283
|
# 1 == 1.0 #=> true
|
284
284
|
# 1.eql? 1.0 #=> false
|
285
|
-
#
|
285
|
+
#
|
286
286
|
def eql?: (untyped) -> bool
|
287
287
|
|
288
288
|
# Adds to *obj* the instance methods from each module given as a parameter.
|
289
|
-
#
|
289
|
+
#
|
290
290
|
# module Mod
|
291
291
|
# def hello
|
292
292
|
# "Hello from Mod.\n"
|
293
293
|
# end
|
294
294
|
# end
|
295
|
-
#
|
295
|
+
#
|
296
296
|
# class Klass
|
297
297
|
# def hello
|
298
298
|
# "Hello from Klass.\n"
|
299
299
|
# end
|
300
300
|
# end
|
301
|
-
#
|
301
|
+
#
|
302
302
|
# k = Klass.new
|
303
303
|
# k.hello #=> "Hello from Klass.\n"
|
304
304
|
# k.extend(Mod) #=> #<Klass:0x401b3bc8>
|
305
305
|
# k.hello #=> "Hello from Mod.\n"
|
306
|
-
#
|
306
|
+
#
|
307
307
|
def `extend`: (*Module) -> self
|
308
308
|
|
309
309
|
# Prevents further modifications to *obj*. A `RuntimeError` will be raised if
|
310
310
|
# modification is attempted. There is no way to unfreeze a frozen object. See
|
311
311
|
# also `Object#frozen?`.
|
312
|
-
#
|
312
|
+
#
|
313
313
|
# This method returns self.
|
314
|
-
#
|
314
|
+
#
|
315
315
|
# a = [ "a", "b", "c" ]
|
316
316
|
# a.freeze
|
317
317
|
# a << "z"
|
318
|
-
#
|
318
|
+
#
|
319
319
|
# *produces:*
|
320
|
-
#
|
320
|
+
#
|
321
321
|
# prog.rb:3:in `<<': can't modify frozen Array (FrozenError)
|
322
322
|
# from prog.rb:3
|
323
|
-
#
|
323
|
+
#
|
324
324
|
# Objects of the following classes are always frozen: Integer, Float, Symbol.
|
325
|
-
#
|
325
|
+
#
|
326
326
|
def freeze: () -> self
|
327
327
|
|
328
328
|
# Returns the freeze status of *obj*.
|
329
|
-
#
|
329
|
+
#
|
330
330
|
# a = [ "a", "b", "c" ]
|
331
331
|
# a.freeze #=> ["a", "b", "c"]
|
332
332
|
# a.frozen? #=> true
|
333
|
-
#
|
333
|
+
#
|
334
334
|
def frozen?: () -> bool
|
335
335
|
|
336
336
|
def hash: () -> Integer
|
@@ -341,40 +341,40 @@ class Object < BasicObject
|
|
341
341
|
# each of them). User defined classes should override this method to provide a
|
342
342
|
# better representation of *obj*. When overriding this method, it should return
|
343
343
|
# a string whose encoding is compatible with the default external encoding.
|
344
|
-
#
|
344
|
+
#
|
345
345
|
# [ 1, 2, 3..4, 'five' ].inspect #=> "[1, 2, 3..4, \"five\"]"
|
346
346
|
# Time.new.inspect #=> "2008-03-08 19:43:39 +0900"
|
347
|
-
#
|
347
|
+
#
|
348
348
|
# class Foo
|
349
349
|
# end
|
350
350
|
# Foo.new.inspect #=> "#<Foo:0x0300c868>"
|
351
|
-
#
|
351
|
+
#
|
352
352
|
# class Bar
|
353
353
|
# def initialize
|
354
354
|
# @bar = 1
|
355
355
|
# end
|
356
356
|
# end
|
357
357
|
# Bar.new.inspect #=> "#<Bar:0x0300c868 @bar=1>"
|
358
|
-
#
|
358
|
+
#
|
359
359
|
def inspect: () -> String
|
360
360
|
|
361
361
|
# Returns `true` if *obj* is an instance of the given class. See also
|
362
362
|
# `Object#kind_of?`.
|
363
|
-
#
|
363
|
+
#
|
364
364
|
# class A; end
|
365
365
|
# class B < A; end
|
366
366
|
# class C < B; end
|
367
|
-
#
|
367
|
+
#
|
368
368
|
# b = B.new
|
369
369
|
# b.instance_of? A #=> false
|
370
370
|
# b.instance_of? B #=> true
|
371
371
|
# b.instance_of? C #=> false
|
372
|
-
#
|
372
|
+
#
|
373
373
|
def instance_of?: (Module) -> bool
|
374
374
|
|
375
375
|
# Returns `true` if the given instance variable is defined in *obj*. String
|
376
376
|
# arguments are converted to symbols.
|
377
|
-
#
|
377
|
+
#
|
378
378
|
# class Fred
|
379
379
|
# def initialize(p1, p2)
|
380
380
|
# @a, @b = p1, p2
|
@@ -384,7 +384,7 @@ class Object < BasicObject
|
|
384
384
|
# fred.instance_variable_defined?(:@a) #=> true
|
385
385
|
# fred.instance_variable_defined?("@b") #=> true
|
386
386
|
# fred.instance_variable_defined?("@c") #=> false
|
387
|
-
#
|
387
|
+
#
|
388
388
|
def instance_variable_defined?: (String | Symbol var) -> bool
|
389
389
|
|
390
390
|
# Returns the value of the given instance variable, or nil if the instance
|
@@ -392,7 +392,7 @@ class Object < BasicObject
|
|
392
392
|
# regular instance variables. Throws a `NameError` exception if the supplied
|
393
393
|
# symbol is not valid as an instance variable name. String arguments are
|
394
394
|
# converted to symbols.
|
395
|
-
#
|
395
|
+
#
|
396
396
|
# class Fred
|
397
397
|
# def initialize(p1, p2)
|
398
398
|
# @a, @b = p1, p2
|
@@ -401,7 +401,7 @@ class Object < BasicObject
|
|
401
401
|
# fred = Fred.new('cat', 99)
|
402
402
|
# fred.instance_variable_get(:@a) #=> "cat"
|
403
403
|
# fred.instance_variable_get("@b") #=> 99
|
404
|
-
#
|
404
|
+
#
|
405
405
|
def instance_variable_get: (String | Symbol var) -> untyped
|
406
406
|
|
407
407
|
# Sets the instance variable named by *symbol* to the given object, thereby
|
@@ -409,7 +409,7 @@ class Object < BasicObject
|
|
409
409
|
# encapsulation. The variable does not have to exist prior to this call. If the
|
410
410
|
# instance variable name is passed as a string, that string is converted to a
|
411
411
|
# symbol.
|
412
|
-
#
|
412
|
+
#
|
413
413
|
# class Fred
|
414
414
|
# def initialize(p1, p2)
|
415
415
|
# @a, @b = p1, p2
|
@@ -419,12 +419,12 @@ class Object < BasicObject
|
|
419
419
|
# fred.instance_variable_set(:@a, 'dog') #=> "dog"
|
420
420
|
# fred.instance_variable_set(:@c, 'cat') #=> "cat"
|
421
421
|
# fred.inspect #=> "#<Fred:0x401b3da8 @a=\"dog\", @b=99, @c=\"cat\">"
|
422
|
-
#
|
422
|
+
#
|
423
423
|
def instance_variable_set: [X] (String | Symbol var, X value) -> X
|
424
424
|
|
425
425
|
# Returns an array of instance variable names for the receiver. Note that simply
|
426
426
|
# defining an accessor does not create the corresponding instance variable.
|
427
|
-
#
|
427
|
+
#
|
428
428
|
# class Fred
|
429
429
|
# attr_accessor :a1
|
430
430
|
# def initialize
|
@@ -432,67 +432,67 @@ class Object < BasicObject
|
|
432
432
|
# end
|
433
433
|
# end
|
434
434
|
# Fred.new.instance_variables #=> [:@iv]
|
435
|
-
#
|
435
|
+
#
|
436
436
|
def instance_variables: () -> Array[Symbol]
|
437
437
|
|
438
438
|
# Returns `true` if *class* is the class of *obj*, or if *class* is one of the
|
439
439
|
# superclasses of *obj* or modules included in *obj*.
|
440
|
-
#
|
440
|
+
#
|
441
441
|
# module M; end
|
442
442
|
# class A
|
443
443
|
# include M
|
444
444
|
# end
|
445
445
|
# class B < A; end
|
446
446
|
# class C < B; end
|
447
|
-
#
|
447
|
+
#
|
448
448
|
# b = B.new
|
449
449
|
# b.is_a? A #=> true
|
450
450
|
# b.is_a? B #=> true
|
451
451
|
# b.is_a? C #=> false
|
452
452
|
# b.is_a? M #=> true
|
453
|
-
#
|
453
|
+
#
|
454
454
|
# b.kind_of? A #=> true
|
455
455
|
# b.kind_of? B #=> true
|
456
456
|
# b.kind_of? C #=> false
|
457
457
|
# b.kind_of? M #=> true
|
458
|
-
#
|
458
|
+
#
|
459
459
|
def is_a?: (Module) -> bool
|
460
460
|
|
461
461
|
# Returns `true` if *class* is the class of *obj*, or if *class* is one of the
|
462
462
|
# superclasses of *obj* or modules included in *obj*.
|
463
|
-
#
|
463
|
+
#
|
464
464
|
# module M; end
|
465
465
|
# class A
|
466
466
|
# include M
|
467
467
|
# end
|
468
468
|
# class B < A; end
|
469
469
|
# class C < B; end
|
470
|
-
#
|
470
|
+
#
|
471
471
|
# b = B.new
|
472
472
|
# b.is_a? A #=> true
|
473
473
|
# b.is_a? B #=> true
|
474
474
|
# b.is_a? C #=> false
|
475
475
|
# b.is_a? M #=> true
|
476
|
-
#
|
476
|
+
#
|
477
477
|
# b.kind_of? A #=> true
|
478
478
|
# b.kind_of? B #=> true
|
479
479
|
# b.kind_of? C #=> false
|
480
480
|
# b.kind_of? M #=> true
|
481
|
-
#
|
481
|
+
#
|
482
482
|
alias kind_of? is_a?
|
483
483
|
|
484
484
|
# Returns the receiver.
|
485
|
-
#
|
485
|
+
#
|
486
486
|
# string = "my string"
|
487
487
|
# string.itself.object_id == string.object_id #=> true
|
488
|
-
#
|
488
|
+
#
|
489
489
|
def `itself`: () -> self
|
490
490
|
|
491
491
|
# Looks up the named method as a receiver in *obj*, returning a `Method` object
|
492
492
|
# (or raising `NameError`). The `Method` object acts as a closure in *obj*'s
|
493
493
|
# object instance, so instance variables and the value of `self` remain
|
494
494
|
# available.
|
495
|
-
#
|
495
|
+
#
|
496
496
|
# class Demo
|
497
497
|
# def initialize(n)
|
498
498
|
# @iv = n
|
@@ -501,27 +501,27 @@ class Object < BasicObject
|
|
501
501
|
# "Hello, @iv = #{@iv}"
|
502
502
|
# end
|
503
503
|
# end
|
504
|
-
#
|
504
|
+
#
|
505
505
|
# k = Demo.new(99)
|
506
506
|
# m = k.method(:hello)
|
507
507
|
# m.call #=> "Hello, @iv = 99"
|
508
|
-
#
|
508
|
+
#
|
509
509
|
# l = Demo.new('Fred')
|
510
510
|
# m = l.method("hello")
|
511
511
|
# m.call #=> "Hello, @iv = Fred"
|
512
|
-
#
|
512
|
+
#
|
513
513
|
# Note that `Method` implements `to_proc` method, which means it can be used
|
514
514
|
# with iterators.
|
515
|
-
#
|
515
|
+
#
|
516
516
|
# [ 1, 2, 3 ].each(&method(:puts)) # => prints 3 lines to stdout
|
517
|
-
#
|
517
|
+
#
|
518
518
|
# out = File.open('test.txt', 'w')
|
519
519
|
# [ 1, 2, 3 ].each(&out.method(:puts)) # => prints 3 lines to file
|
520
|
-
#
|
520
|
+
#
|
521
521
|
# require 'date'
|
522
522
|
# %w[2017-03-01 2017-03-02].collect(&Date.method(:parse))
|
523
523
|
# #=> [#<Date: 2017-03-01 ((2457814j,0s,0n),+0s,2299161j)>, #<Date: 2017-03-02 ((2457815j,0s,0n),+0s,2299161j)>]
|
524
|
-
#
|
524
|
+
#
|
525
525
|
def method: (String | Symbol name) -> Method
|
526
526
|
|
527
527
|
# Returns a list of the names of public and protected methods of *obj*. This
|
@@ -529,7 +529,7 @@ class Object < BasicObject
|
|
529
529
|
# parameter is `false`, it returns an array of *obj<i>'s public and protected
|
530
530
|
# singleton methods, the array will not include methods in modules included in
|
531
531
|
# <i>obj*.
|
532
|
-
#
|
532
|
+
#
|
533
533
|
# class Klass
|
534
534
|
# def klass_method()
|
535
535
|
# end
|
@@ -539,69 +539,69 @@ class Object < BasicObject
|
|
539
539
|
# # :==~, :!, :eql?
|
540
540
|
# # :hash, :<=>, :class, :singleton_class]
|
541
541
|
# k.methods.length #=> 56
|
542
|
-
#
|
542
|
+
#
|
543
543
|
# k.methods(false) #=> []
|
544
544
|
# def k.singleton_method; end
|
545
545
|
# k.methods(false) #=> [:singleton_method]
|
546
|
-
#
|
546
|
+
#
|
547
547
|
# module M123; def m123; end end
|
548
548
|
# k.extend M123
|
549
549
|
# k.methods(false) #=> [:singleton_method]
|
550
|
-
#
|
550
|
+
#
|
551
551
|
def methods: () -> Array[Symbol]
|
552
552
|
|
553
553
|
# Only the object *nil* responds `true` to `nil?`.
|
554
|
-
#
|
554
|
+
#
|
555
555
|
# Object.new.nil? #=> false
|
556
556
|
# nil.nil? #=> true
|
557
|
-
#
|
557
|
+
#
|
558
558
|
def `nil?`: () -> bool
|
559
559
|
|
560
560
|
# Returns an integer identifier for `obj`.
|
561
|
-
#
|
561
|
+
#
|
562
562
|
# The same number will be returned on all calls to `object_id` for a given
|
563
563
|
# object, and no two active objects will share an id.
|
564
|
-
#
|
564
|
+
#
|
565
565
|
# Note: that some objects of builtin classes are reused for optimization. This
|
566
566
|
# is the case for immediate values and frozen string literals.
|
567
|
-
#
|
567
|
+
#
|
568
568
|
# Immediate values are not passed by reference but are passed by value: `nil`,
|
569
569
|
# `true`, `false`, Fixnums, Symbols, and some Floats.
|
570
|
-
#
|
570
|
+
#
|
571
571
|
# Object.new.object_id == Object.new.object_id # => false
|
572
572
|
# (21 * 2).object_id == (21 * 2).object_id # => true
|
573
573
|
# "hello".object_id == "hello".object_id # => false
|
574
574
|
# "hi".freeze.object_id == "hi".freeze.object_id # => true
|
575
|
-
#
|
575
|
+
#
|
576
576
|
def object_id: () -> Integer
|
577
577
|
|
578
578
|
# Returns the list of private methods accessible to *obj*. If the *all*
|
579
579
|
# parameter is set to `false`, only those methods in the receiver will be
|
580
580
|
# listed.
|
581
|
-
#
|
581
|
+
#
|
582
582
|
def private_methods: () -> Array[Symbol]
|
583
583
|
|
584
584
|
# Returns the list of protected methods accessible to *obj*. If the *all*
|
585
585
|
# parameter is set to `false`, only those methods in the receiver will be
|
586
586
|
# listed.
|
587
|
-
#
|
587
|
+
#
|
588
588
|
def protected_methods: () -> Array[Symbol]
|
589
589
|
|
590
590
|
# Similar to *method*, searches public method only.
|
591
|
-
#
|
591
|
+
#
|
592
592
|
def public_method: (name name) -> Method
|
593
593
|
|
594
594
|
# Invokes the method identified by *symbol*, passing it any arguments specified.
|
595
595
|
# Unlike send, public_send calls public methods only. When the method is
|
596
596
|
# identified by a string, the string is converted to a symbol.
|
597
|
-
#
|
597
|
+
#
|
598
598
|
# 1.public_send(:puts, "hello") # causes NoMethodError
|
599
|
-
#
|
599
|
+
#
|
600
600
|
def `public_send`: (name name, *untyped args) ?{ (*untyped) -> untyped } -> untyped
|
601
601
|
|
602
602
|
# Removes the named instance variable from *obj*, returning that variable's
|
603
603
|
# value. String arguments are converted to symbols.
|
604
|
-
#
|
604
|
+
#
|
605
605
|
# class Dummy
|
606
606
|
# attr_reader :var
|
607
607
|
# def initialize
|
@@ -615,29 +615,29 @@ class Object < BasicObject
|
|
615
615
|
# d.var #=> 99
|
616
616
|
# d.remove #=> 99
|
617
617
|
# d.var #=> nil
|
618
|
-
#
|
618
|
+
#
|
619
619
|
def remove_instance_variable: (name name) -> untyped
|
620
620
|
|
621
621
|
# Returns `true` if *obj* responds to the given method. Private and protected
|
622
622
|
# methods are included in the search only if the optional second parameter
|
623
623
|
# evaluates to `true`.
|
624
|
-
#
|
624
|
+
#
|
625
625
|
# If the method is not implemented, as Process.fork on Windows, File.lchmod on
|
626
626
|
# GNU/Linux, etc., false is returned.
|
627
|
-
#
|
627
|
+
#
|
628
628
|
# If the method is not defined, `respond_to_missing?` method is called and the
|
629
629
|
# result is returned.
|
630
|
-
#
|
630
|
+
#
|
631
631
|
# When the method name parameter is given as a string, the string is converted
|
632
632
|
# to a symbol.
|
633
|
-
#
|
633
|
+
#
|
634
634
|
def respond_to?: (name name, ?bool include_all) -> bool
|
635
635
|
|
636
636
|
# Invokes the method identified by *symbol*, passing it any arguments specified.
|
637
637
|
# You can use `__send__` if the name `send` clashes with an existing method in
|
638
638
|
# *obj*. When the method is identified by a string, the string is converted to a
|
639
639
|
# symbol.
|
640
|
-
#
|
640
|
+
#
|
641
641
|
# class Klass
|
642
642
|
# def hello(*args)
|
643
643
|
# "Hello " + args.join(' ')
|
@@ -645,24 +645,24 @@ class Object < BasicObject
|
|
645
645
|
# end
|
646
646
|
# k = Klass.new
|
647
647
|
# k.send :hello, "gentle", "readers" #=> "Hello gentle readers"
|
648
|
-
#
|
648
|
+
#
|
649
649
|
def `send`: (name name, *untyped args) ?{ (*untyped) -> untyped } -> untyped
|
650
650
|
|
651
651
|
# Returns the singleton class of *obj*. This method creates a new singleton
|
652
652
|
# class if *obj* does not have one.
|
653
|
-
#
|
653
|
+
#
|
654
654
|
# If *obj* is `nil`, `true`, or `false`, it returns NilClass, TrueClass, or
|
655
655
|
# FalseClass, respectively. If *obj* is an Integer, a Float or a Symbol, it
|
656
656
|
# raises a TypeError.
|
657
|
-
#
|
657
|
+
#
|
658
658
|
# Object.new.singleton_class #=> #<Class:#<Object:0xb7ce1e24>>
|
659
659
|
# String.singleton_class #=> #<Class:String>
|
660
660
|
# nil.singleton_class #=> NilClass
|
661
|
-
#
|
661
|
+
#
|
662
662
|
def `singleton_class`: () -> Class
|
663
663
|
|
664
664
|
# Similar to *method*, searches singleton method only.
|
665
|
-
#
|
665
|
+
#
|
666
666
|
# class Demo
|
667
667
|
# def initialize(n)
|
668
668
|
# @iv = n
|
@@ -671,7 +671,7 @@ class Object < BasicObject
|
|
671
671
|
# "Hello, @iv = #{@iv}"
|
672
672
|
# end
|
673
673
|
# end
|
674
|
-
#
|
674
|
+
#
|
675
675
|
# k = Demo.new(99)
|
676
676
|
# def k.hi
|
677
677
|
# "Hi, @iv = #{@iv}"
|
@@ -679,99 +679,99 @@ class Object < BasicObject
|
|
679
679
|
# m = k.singleton_method(:hi)
|
680
680
|
# m.call #=> "Hi, @iv = 99"
|
681
681
|
# m = k.singleton_method(:hello) #=> NameError
|
682
|
-
#
|
682
|
+
#
|
683
683
|
def singleton_method: (name name) -> Method
|
684
684
|
|
685
685
|
# Returns an array of the names of singleton methods for *obj*. If the optional
|
686
686
|
# *all* parameter is true, the list will include methods in modules included in
|
687
687
|
# *obj*. Only public and protected singleton methods are returned.
|
688
|
-
#
|
688
|
+
#
|
689
689
|
# module Other
|
690
690
|
# def three() end
|
691
691
|
# end
|
692
|
-
#
|
692
|
+
#
|
693
693
|
# class Single
|
694
694
|
# def Single.four() end
|
695
695
|
# end
|
696
|
-
#
|
696
|
+
#
|
697
697
|
# a = Single.new
|
698
|
-
#
|
698
|
+
#
|
699
699
|
# def a.one()
|
700
700
|
# end
|
701
|
-
#
|
701
|
+
#
|
702
702
|
# class << a
|
703
703
|
# include Other
|
704
704
|
# def two()
|
705
705
|
# end
|
706
706
|
# end
|
707
|
-
#
|
707
|
+
#
|
708
708
|
# Single.singleton_methods #=> [:four]
|
709
709
|
# a.singleton_methods(false) #=> [:two, :one]
|
710
710
|
# a.singleton_methods #=> [:two, :one, :three]
|
711
|
-
#
|
711
|
+
#
|
712
712
|
def singleton_methods: () -> Array[Symbol]
|
713
713
|
|
714
714
|
# Mark the object as tainted.
|
715
|
-
#
|
715
|
+
#
|
716
716
|
# Objects that are marked as tainted will be restricted from various built-in
|
717
717
|
# methods. This is to prevent insecure data, such as command-line arguments or
|
718
718
|
# strings read from Kernel#gets, from inadvertently compromising the user's
|
719
719
|
# system.
|
720
|
-
#
|
720
|
+
#
|
721
721
|
# To check whether an object is tainted, use #tainted?.
|
722
|
-
#
|
722
|
+
#
|
723
723
|
# You should only untaint a tainted object if your code has inspected it and
|
724
724
|
# determined that it is safe. To do so use #untaint.
|
725
|
-
#
|
725
|
+
#
|
726
726
|
def taint: () -> self
|
727
727
|
|
728
728
|
# Deprecated method that is equivalent to #taint.
|
729
|
-
#
|
729
|
+
#
|
730
730
|
alias untrust taint
|
731
731
|
|
732
732
|
# Returns true if the object is tainted.
|
733
|
-
#
|
733
|
+
#
|
734
734
|
# See #taint for more information.
|
735
|
-
#
|
735
|
+
#
|
736
736
|
def tainted?: () -> bool
|
737
737
|
|
738
738
|
# Deprecated method that is equivalent to #tainted?.
|
739
|
-
#
|
739
|
+
#
|
740
740
|
alias untrusted? tainted?
|
741
741
|
|
742
742
|
# Yields self to the block, and then returns self. The primary purpose of this
|
743
743
|
# method is to "tap into" a method chain, in order to perform operations on
|
744
744
|
# intermediate results within the chain.
|
745
|
-
#
|
745
|
+
#
|
746
746
|
# (1..10) .tap {|x| puts "original: #{x}" }
|
747
747
|
# .to_a .tap {|x| puts "array: #{x}" }
|
748
748
|
# .select {|x| x.even? } .tap {|x| puts "evens: #{x}" }
|
749
749
|
# .map {|x| x*x } .tap {|x| puts "squares: #{x}" }
|
750
|
-
#
|
750
|
+
#
|
751
751
|
def tap: () { (self) -> void } -> self
|
752
752
|
|
753
753
|
# Yields self to the block and returns the result of the block.
|
754
|
-
#
|
754
|
+
#
|
755
755
|
# 3.next.then {|x| x**x }.to_s #=> "256"
|
756
756
|
# "my string".yield_self {|s| s.upcase } #=> "MY STRING"
|
757
|
-
#
|
757
|
+
#
|
758
758
|
# Good usage for `yield_self` is value piping in method chains:
|
759
|
-
#
|
759
|
+
#
|
760
760
|
# require 'open-uri'
|
761
761
|
# require 'json'
|
762
|
-
#
|
762
|
+
#
|
763
763
|
# construct_url(arguments).
|
764
764
|
# yield_self {|url| open(url).read }.
|
765
765
|
# yield_self {|response| JSON.parse(response) }
|
766
|
-
#
|
766
|
+
#
|
767
767
|
# When called without block, the method returns `Enumerator`, which can be used,
|
768
768
|
# for example, for conditional circuit-breaking:
|
769
|
-
#
|
769
|
+
#
|
770
770
|
# # meets condition, no-op
|
771
771
|
# 1.yield_self.detect(&:odd?) # => 1
|
772
772
|
# # does not meet condition, drop value
|
773
773
|
# 2.yield_self.detect(&:odd?) # => nil
|
774
|
-
#
|
774
|
+
#
|
775
775
|
def `yield_self`: [X] () { (self) -> X } -> X
|
776
776
|
| () -> Enumerator[self, untyped]
|
777
777
|
|
@@ -779,41 +779,41 @@ class Object < BasicObject
|
|
779
779
|
# class and an encoding of the object id. As a special case, the top-level
|
780
780
|
# object that is the initial execution context of Ruby programs returns
|
781
781
|
# ``main''.
|
782
|
-
#
|
782
|
+
#
|
783
783
|
def to_s: () -> String
|
784
784
|
|
785
785
|
# Removes the tainted mark from the object.
|
786
|
-
#
|
786
|
+
#
|
787
787
|
# See #taint for more information.
|
788
|
-
#
|
788
|
+
#
|
789
789
|
def untaint: () -> self
|
790
790
|
|
791
791
|
# Deprecated method that is equivalent to #untaint.
|
792
|
-
#
|
792
|
+
#
|
793
793
|
alias trust untaint
|
794
794
|
|
795
795
|
# Yields self to the block and returns the result of the block.
|
796
|
-
#
|
796
|
+
#
|
797
797
|
# 3.next.then {|x| x**x }.to_s #=> "256"
|
798
798
|
# "my string".yield_self {|s| s.upcase } #=> "MY STRING"
|
799
|
-
#
|
799
|
+
#
|
800
800
|
# Good usage for `yield_self` is value piping in method chains:
|
801
|
-
#
|
801
|
+
#
|
802
802
|
# require 'open-uri'
|
803
803
|
# require 'json'
|
804
|
-
#
|
804
|
+
#
|
805
805
|
# construct_url(arguments).
|
806
806
|
# yield_self {|url| open(url).read }.
|
807
807
|
# yield_self {|response| JSON.parse(response) }
|
808
|
-
#
|
808
|
+
#
|
809
809
|
# When called without block, the method returns `Enumerator`, which can be used,
|
810
810
|
# for example, for conditional circuit-breaking:
|
811
|
-
#
|
811
|
+
#
|
812
812
|
# # meets condition, no-op
|
813
813
|
# 1.yield_self.detect(&:odd?) # => 1
|
814
814
|
# # does not meet condition, drop value
|
815
815
|
# 2.yield_self.detect(&:odd?) # => nil
|
816
|
-
#
|
816
|
+
#
|
817
817
|
alias then yield_self
|
818
818
|
end
|
819
819
|
|