rbs 3.2.2 → 3.3.0.pre.1
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 +1 -1
- data/.github/workflows/ruby.yml +7 -2
- data/CHANGELOG.md +85 -0
- data/Gemfile.lock +14 -14
- data/README.md +11 -2
- data/Rakefile +10 -7
- data/Steepfile +7 -7
- data/core/basic_object.rbs +7 -7
- data/core/binding.rbs +3 -3
- data/core/builtin.rbs +171 -5
- data/core/constants.rbs +17 -17
- data/core/dir.rbs +3 -3
- data/core/encoding.rbs +434 -628
- data/core/enumerator.rbs +37 -0
- data/core/exception.rbs +11 -11
- data/core/false_class.rbs +5 -11
- data/core/fiber.rbs +3 -3
- data/core/file_test.rbs +28 -26
- data/core/kernel.rbs +900 -21
- data/core/marshal.rbs +24 -14
- data/core/match_data.rbs +8 -8
- data/core/math.rbs +57 -53
- data/core/method.rbs +3 -1
- data/core/module.rbs +38 -36
- data/core/nil_class.rbs +7 -13
- data/core/object.rbs +3 -966
- data/core/process.rbs +3 -3
- data/core/ractor.rbs +2 -2
- data/core/rb_config.rbs +64 -43
- data/core/regexp.rbs +3 -3
- data/core/signal.rbs +10 -4
- data/core/struct.rbs +1 -1
- data/core/thread.rbs +7 -7
- data/core/thread_group.rbs +9 -9
- data/core/true_class.rbs +5 -11
- data/core/unbound_method.rbs +56 -7
- data/core/warning.rbs +33 -0
- data/docs/collection.md +56 -6
- data/docs/data_and_struct.md +57 -0
- data/docs/stdlib.md +61 -2
- data/docs/syntax.md +123 -2
- data/ext/rbs_extension/lexer.c +624 -569
- data/ext/rbs_extension/lexer.h +1 -0
- data/ext/rbs_extension/lexer.re +1 -0
- data/ext/rbs_extension/lexstate.c +1 -0
- data/ext/rbs_extension/parser.c +6 -0
- data/goodcheck.yml +2 -2
- data/lib/rbs/annotate/formatter.rb +13 -3
- data/lib/rbs/annotate/rdoc_source.rb +10 -1
- data/lib/rbs/cli/colored_io.rb +48 -0
- data/lib/rbs/cli/diff.rb +80 -0
- data/lib/rbs/cli.rb +151 -16
- data/lib/rbs/collection/config/lockfile.rb +0 -25
- data/lib/rbs/collection/config/lockfile_generator.rb +0 -6
- data/lib/rbs/collection/installer.rb +1 -1
- data/lib/rbs/collection/sources/git.rb +6 -4
- data/lib/rbs/collection/sources/local.rb +7 -5
- data/lib/rbs/diff.rb +104 -0
- data/lib/rbs/environment.rb +1 -1
- data/lib/rbs/method_type.rb +23 -0
- data/lib/rbs/prototype/rb.rb +2 -9
- data/lib/rbs/prototype/runtime/helpers.rb +59 -0
- data/lib/rbs/prototype/runtime/value_object_generator.rb +236 -0
- data/lib/rbs/prototype/runtime.rb +234 -150
- data/lib/rbs/sorter.rb +144 -117
- data/lib/rbs/test/guaranteed.rb +31 -0
- data/lib/rbs/test/type_check.rb +4 -4
- data/lib/rbs/test.rb +3 -0
- data/lib/rbs/types.rb +184 -3
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs/writer.rb +4 -4
- data/lib/rbs.rb +1 -0
- data/rbs.gemspec +1 -0
- data/sig/annotate/formatter.rbs +2 -2
- data/sig/annotate/rdoc_annotater.rbs +1 -1
- data/sig/cli/colored_io.rbs +15 -0
- data/sig/cli/diff.rbs +21 -0
- data/sig/cli.rbs +2 -0
- data/sig/collection/config/lockfile.rbs +0 -6
- data/sig/diff.rbs +23 -0
- data/sig/errors.rbs +1 -5
- data/sig/method_types.rbs +6 -0
- data/sig/prototype/runtime.rbs +108 -0
- data/sig/rdoc/rbs.rbs +4 -0
- data/sig/shims/bundler.rbs +5 -0
- data/sig/sorter.rbs +23 -5
- data/sig/types.rbs +29 -0
- data/stdlib/benchmark/0/benchmark.rbs +1 -1
- data/stdlib/cgi/0/core.rbs +2 -2
- data/stdlib/did_you_mean/0/did_you_mean.rbs +2 -2
- data/stdlib/digest/0/digest.rbs +1 -1
- data/stdlib/fileutils/0/fileutils.rbs +1 -1
- data/stdlib/forwardable/0/forwardable.rbs +4 -4
- data/stdlib/io-console/0/io-console.rbs +1 -1
- data/stdlib/json/0/json.rbs +37 -0
- data/stdlib/logger/0/logger.rbs +2 -2
- data/stdlib/net-http/0/manifest.yaml +1 -1
- data/stdlib/net-http/0/net-http.rbs +16 -63
- data/stdlib/net-protocol/0/manifest.yaml +2 -0
- data/stdlib/net-protocol/0/net-protocol.rbs +56 -0
- data/stdlib/openssl/0/openssl.rbs +1 -1
- data/stdlib/pp/0/manifest.yaml +2 -0
- data/stdlib/pp/0/pp.rbs +301 -0
- data/stdlib/{yaml → psych}/0/dbm.rbs +3 -3
- data/stdlib/psych/0/manifest.yaml +3 -0
- data/stdlib/psych/0/psych.rbs +391 -0
- data/stdlib/{yaml → psych}/0/store.rbs +2 -2
- data/stdlib/rdoc/0/code_object.rbs +55 -0
- data/stdlib/rdoc/0/comment.rbs +60 -0
- data/stdlib/rdoc/0/context.rbs +153 -0
- data/stdlib/rdoc/0/markup.rbs +119 -0
- data/stdlib/rdoc/0/parser.rbs +56 -0
- data/stdlib/rdoc/0/rdoc.rbs +0 -372
- data/stdlib/rdoc/0/ri.rbs +17 -0
- data/stdlib/rdoc/0/store.rbs +48 -0
- data/stdlib/rdoc/0/top_level.rbs +97 -0
- data/stdlib/socket/0/basic_socket.rbs +1 -1
- data/stdlib/socket/0/socket.rbs +1 -1
- data/stdlib/uri/0/common.rbs +1 -1
- data/stdlib/yaml/0/manifest.yaml +1 -2
- data/stdlib/yaml/0/yaml.rbs +1 -199
- metadata +46 -9
- data/sig/shims/pp.rbs +0 -3
- data/sig/shims.rbs +0 -47
data/core/object.rbs
CHANGED
|
@@ -107,971 +107,8 @@
|
|
|
107
107
|
#
|
|
108
108
|
class Object < BasicObject
|
|
109
109
|
include Kernel
|
|
110
|
-
|
|
111
|
-
# <!--
|
|
112
|
-
# rdoc-file=object.c
|
|
113
|
-
# - obj !~ other -> true or false
|
|
114
|
-
# -->
|
|
115
|
-
# Returns true if two objects do not match (using the *=~* method), otherwise
|
|
116
|
-
# false.
|
|
117
|
-
#
|
|
118
|
-
def !~: (untyped) -> bool
|
|
119
|
-
|
|
120
|
-
# <!--
|
|
121
|
-
# rdoc-file=object.c
|
|
122
|
-
# - obj <=> other -> 0 or nil
|
|
123
|
-
# -->
|
|
124
|
-
# Returns 0 if `obj` and `other` are the same object or `obj == other`,
|
|
125
|
-
# otherwise nil.
|
|
126
|
-
#
|
|
127
|
-
# The #<=> is used by various methods to compare objects, for example
|
|
128
|
-
# Enumerable#sort, Enumerable#max etc.
|
|
129
|
-
#
|
|
130
|
-
# Your implementation of #<=> should return one of the following values: -1, 0,
|
|
131
|
-
# 1 or nil. -1 means self is smaller than other. 0 means self is equal to other.
|
|
132
|
-
# 1 means self is bigger than other. Nil means the two values could not be
|
|
133
|
-
# compared.
|
|
134
|
-
#
|
|
135
|
-
# When you define #<=>, you can include Comparable to gain the methods #<=, #<,
|
|
136
|
-
# #==, #>=, #> and #between?.
|
|
137
|
-
#
|
|
138
|
-
def <=>: (untyped) -> Integer?
|
|
139
|
-
|
|
140
|
-
# <!--
|
|
141
|
-
# rdoc-file=object.c
|
|
142
|
-
# - obj === other -> true or false
|
|
143
|
-
# -->
|
|
144
|
-
# Case Equality -- For class Object, effectively the same as calling `#==`, but
|
|
145
|
-
# typically overridden by descendants to provide meaningful semantics in `case`
|
|
146
|
-
# statements.
|
|
147
|
-
#
|
|
148
|
-
def ===: (untyped) -> bool
|
|
149
|
-
|
|
150
|
-
# Returns the class of *obj*. This method must always be called with an explicit
|
|
151
|
-
# receiver, as `class` is also a reserved word in Ruby.
|
|
152
|
-
#
|
|
153
|
-
# 1.class #=> Integer
|
|
154
|
-
# self.class #=> Object
|
|
155
|
-
#
|
|
156
|
-
def class: () -> untyped
|
|
157
|
-
|
|
158
|
-
# Produces a shallow copy of *obj*---the instance variables of *obj* are copied,
|
|
159
|
-
# but not the objects they reference. `clone` copies the frozen (unless :freeze
|
|
160
|
-
# keyword argument is given with a false value) and tainted state of *obj*. See
|
|
161
|
-
# also the discussion under `Object#dup`.
|
|
162
|
-
#
|
|
163
|
-
# class Klass
|
|
164
|
-
# attr_accessor :str
|
|
165
|
-
# end
|
|
166
|
-
# s1 = Klass.new #=> #<Klass:0x401b3a38>
|
|
167
|
-
# s1.str = "Hello" #=> "Hello"
|
|
168
|
-
# s2 = s1.clone #=> #<Klass:0x401b3998 @str="Hello">
|
|
169
|
-
# s2.str[1,4] = "i" #=> "i"
|
|
170
|
-
# s1.inspect #=> "#<Klass:0x401b3a38 @str=\"Hi\">"
|
|
171
|
-
# s2.inspect #=> "#<Klass:0x401b3998 @str=\"Hi\">"
|
|
172
|
-
#
|
|
173
|
-
# This method may have class-specific behavior. If so, that behavior will be
|
|
174
|
-
# documented under the #`initialize_copy` method of the class.
|
|
175
|
-
#
|
|
176
|
-
def clone: (?freeze: bool?) -> self
|
|
177
|
-
|
|
178
|
-
# <!--
|
|
179
|
-
# rdoc-file=proc.c
|
|
180
|
-
# - define_singleton_method(symbol, method) -> symbol
|
|
181
|
-
# - define_singleton_method(symbol) { block } -> symbol
|
|
182
|
-
# -->
|
|
183
|
-
# Defines a public singleton method in the receiver. The *method* parameter can
|
|
184
|
-
# be a `Proc`, a `Method` or an `UnboundMethod` object. If a block is specified,
|
|
185
|
-
# it is used as the method body. If a block or a method has parameters, they're
|
|
186
|
-
# used as method parameters.
|
|
187
|
-
#
|
|
188
|
-
# class A
|
|
189
|
-
# class << self
|
|
190
|
-
# def class_name
|
|
191
|
-
# to_s
|
|
192
|
-
# end
|
|
193
|
-
# end
|
|
194
|
-
# end
|
|
195
|
-
# A.define_singleton_method(:who_am_i) do
|
|
196
|
-
# "I am: #{class_name}"
|
|
197
|
-
# end
|
|
198
|
-
# A.who_am_i # ==> "I am: A"
|
|
199
|
-
#
|
|
200
|
-
# guy = "Bob"
|
|
201
|
-
# guy.define_singleton_method(:hello) { "#{self}: Hello there!" }
|
|
202
|
-
# guy.hello #=> "Bob: Hello there!"
|
|
203
|
-
#
|
|
204
|
-
# chris = "Chris"
|
|
205
|
-
# chris.define_singleton_method(:greet) {|greeting| "#{greeting}, I'm Chris!" }
|
|
206
|
-
# chris.greet("Hi") #=> "Hi, I'm Chris!"
|
|
207
|
-
#
|
|
208
|
-
def define_singleton_method: (name, Method | UnboundMethod | Proc method) -> Symbol
|
|
209
|
-
| (name) { (*untyped) -> untyped } -> Symbol
|
|
210
|
-
|
|
211
|
-
# <!--
|
|
212
|
-
# rdoc-file=io.c
|
|
213
|
-
# - display(port = $>) -> nil
|
|
214
|
-
# -->
|
|
215
|
-
# Writes `self` on the given port:
|
|
216
|
-
#
|
|
217
|
-
# 1.display
|
|
218
|
-
# "cat".display
|
|
219
|
-
# [ 4, 5, 6 ].display
|
|
220
|
-
# puts
|
|
221
|
-
#
|
|
222
|
-
# Output:
|
|
223
|
-
#
|
|
224
|
-
# 1cat[4, 5, 6]
|
|
225
|
-
#
|
|
226
|
-
def display: (?_Writer port) -> void
|
|
227
|
-
|
|
228
|
-
# <!--
|
|
229
|
-
# rdoc-file=object.c
|
|
230
|
-
# - obj.dup -> an_object
|
|
231
|
-
# -->
|
|
232
|
-
# Produces a shallow copy of *obj*---the instance variables of *obj* are copied,
|
|
233
|
-
# but not the objects they reference.
|
|
234
|
-
#
|
|
235
|
-
# This method may have class-specific behavior. If so, that behavior will be
|
|
236
|
-
# documented under the #`initialize_copy` method of the class.
|
|
237
|
-
#
|
|
238
|
-
# ### on dup vs clone
|
|
239
|
-
#
|
|
240
|
-
# In general, #clone and #dup may have different semantics in descendant
|
|
241
|
-
# classes. While #clone is used to duplicate an object, including its internal
|
|
242
|
-
# state, #dup typically uses the class of the descendant object to create the
|
|
243
|
-
# new instance.
|
|
244
|
-
#
|
|
245
|
-
# When using #dup, any modules that the object has been extended with will not
|
|
246
|
-
# be copied.
|
|
247
|
-
#
|
|
248
|
-
# class Klass
|
|
249
|
-
# attr_accessor :str
|
|
250
|
-
# end
|
|
251
|
-
#
|
|
252
|
-
# module Foo
|
|
253
|
-
# def foo; 'foo'; end
|
|
254
|
-
# end
|
|
255
|
-
#
|
|
256
|
-
# s1 = Klass.new #=> #<Klass:0x401b3a38>
|
|
257
|
-
# s1.extend(Foo) #=> #<Klass:0x401b3a38>
|
|
258
|
-
# s1.foo #=> "foo"
|
|
259
|
-
#
|
|
260
|
-
# s2 = s1.clone #=> #<Klass:0x401be280>
|
|
261
|
-
# s2.foo #=> "foo"
|
|
262
|
-
#
|
|
263
|
-
# s3 = s1.dup #=> #<Klass:0x401c1084>
|
|
264
|
-
# s3.foo #=> NoMethodError: undefined method `foo' for #<Klass:0x401c1084>
|
|
265
|
-
#
|
|
266
|
-
def dup: () -> self
|
|
267
|
-
|
|
268
|
-
# <!-- rdoc-file=enumerator.c -->
|
|
269
|
-
# Creates a new Enumerator which will enumerate by calling `method` on `obj`,
|
|
270
|
-
# passing `args` if any. What was *yielded* by method becomes values of
|
|
271
|
-
# enumerator.
|
|
272
|
-
#
|
|
273
|
-
# If a block is given, it will be used to calculate the size of the enumerator
|
|
274
|
-
# without the need to iterate it (see Enumerator#size).
|
|
275
|
-
#
|
|
276
|
-
# ### Examples
|
|
277
|
-
#
|
|
278
|
-
# str = "xyz"
|
|
279
|
-
#
|
|
280
|
-
# enum = str.enum_for(:each_byte)
|
|
281
|
-
# enum.each { |b| puts b }
|
|
282
|
-
# # => 120
|
|
283
|
-
# # => 121
|
|
284
|
-
# # => 122
|
|
285
|
-
#
|
|
286
|
-
# # protect an array from being modified by some_method
|
|
287
|
-
# a = [1, 2, 3]
|
|
288
|
-
# some_method(a.to_enum)
|
|
289
|
-
#
|
|
290
|
-
# # String#split in block form is more memory-effective:
|
|
291
|
-
# very_large_string.split("|") { |chunk| return chunk if chunk.include?('DATE') }
|
|
292
|
-
# # This could be rewritten more idiomatically with to_enum:
|
|
293
|
-
# very_large_string.to_enum(:split, "|").lazy.grep(/DATE/).first
|
|
294
|
-
#
|
|
295
|
-
# It is typical to call to_enum when defining methods for a generic Enumerable,
|
|
296
|
-
# in case no block is passed.
|
|
297
|
-
#
|
|
298
|
-
# Here is such an example, with parameter passing and a sizing block:
|
|
299
|
-
#
|
|
300
|
-
# module Enumerable
|
|
301
|
-
# # a generic method to repeat the values of any enumerable
|
|
302
|
-
# def repeat(n)
|
|
303
|
-
# raise ArgumentError, "#{n} is negative!" if n < 0
|
|
304
|
-
# unless block_given?
|
|
305
|
-
# return to_enum(__method__, n) do # __method__ is :repeat here
|
|
306
|
-
# sz = size # Call size and multiply by n...
|
|
307
|
-
# sz * n if sz # but return nil if size itself is nil
|
|
308
|
-
# end
|
|
309
|
-
# end
|
|
310
|
-
# each do |*val|
|
|
311
|
-
# n.times { yield *val }
|
|
312
|
-
# end
|
|
313
|
-
# end
|
|
314
|
-
# end
|
|
315
|
-
#
|
|
316
|
-
# %i[hello world].repeat(2) { |w| puts w }
|
|
317
|
-
# # => Prints 'hello', 'hello', 'world', 'world'
|
|
318
|
-
# enum = (1..14).repeat(3)
|
|
319
|
-
# # => returns an Enumerator when called without a block
|
|
320
|
-
# enum.first(4) # => [1, 1, 1, 2]
|
|
321
|
-
# enum.size # => 42
|
|
322
|
-
#
|
|
323
|
-
def enum_for: (Symbol method, *untyped, **untyped) ?{ (*untyped, **untyped) -> Integer } -> Enumerator[untyped, untyped]
|
|
324
|
-
| () ?{ () -> Integer } -> Enumerator[untyped, self]
|
|
325
|
-
|
|
326
|
-
# <!--
|
|
327
|
-
# rdoc-file=enumerator.c
|
|
328
|
-
# - obj.to_enum(method = :each, *args) -> enum
|
|
329
|
-
# - obj.enum_for(method = :each, *args) -> enum
|
|
330
|
-
# - obj.to_enum(method = :each, *args) {|*args| block} -> enum
|
|
331
|
-
# - obj.enum_for(method = :each, *args){|*args| block} -> enum
|
|
332
|
-
# -->
|
|
333
|
-
# Creates a new Enumerator which will enumerate by calling `method` on `obj`,
|
|
334
|
-
# passing `args` if any. What was *yielded* by method becomes values of
|
|
335
|
-
# enumerator.
|
|
336
|
-
#
|
|
337
|
-
# If a block is given, it will be used to calculate the size of the enumerator
|
|
338
|
-
# without the need to iterate it (see Enumerator#size).
|
|
339
|
-
#
|
|
340
|
-
# ### Examples
|
|
341
|
-
#
|
|
342
|
-
# str = "xyz"
|
|
343
|
-
#
|
|
344
|
-
# enum = str.enum_for(:each_byte)
|
|
345
|
-
# enum.each { |b| puts b }
|
|
346
|
-
# # => 120
|
|
347
|
-
# # => 121
|
|
348
|
-
# # => 122
|
|
349
|
-
#
|
|
350
|
-
# # protect an array from being modified by some_method
|
|
351
|
-
# a = [1, 2, 3]
|
|
352
|
-
# some_method(a.to_enum)
|
|
353
|
-
#
|
|
354
|
-
# # String#split in block form is more memory-effective:
|
|
355
|
-
# very_large_string.split("|") { |chunk| return chunk if chunk.include?('DATE') }
|
|
356
|
-
# # This could be rewritten more idiomatically with to_enum:
|
|
357
|
-
# very_large_string.to_enum(:split, "|").lazy.grep(/DATE/).first
|
|
358
|
-
#
|
|
359
|
-
# It is typical to call to_enum when defining methods for a generic Enumerable,
|
|
360
|
-
# in case no block is passed.
|
|
361
|
-
#
|
|
362
|
-
# Here is such an example, with parameter passing and a sizing block:
|
|
363
|
-
#
|
|
364
|
-
# module Enumerable
|
|
365
|
-
# # a generic method to repeat the values of any enumerable
|
|
366
|
-
# def repeat(n)
|
|
367
|
-
# raise ArgumentError, "#{n} is negative!" if n < 0
|
|
368
|
-
# unless block_given?
|
|
369
|
-
# return to_enum(__method__, n) do # __method__ is :repeat here
|
|
370
|
-
# sz = size # Call size and multiply by n...
|
|
371
|
-
# sz * n if sz # but return nil if size itself is nil
|
|
372
|
-
# end
|
|
373
|
-
# end
|
|
374
|
-
# each do |*val|
|
|
375
|
-
# n.times { yield *val }
|
|
376
|
-
# end
|
|
377
|
-
# end
|
|
378
|
-
# end
|
|
379
|
-
#
|
|
380
|
-
# %i[hello world].repeat(2) { |w| puts w }
|
|
381
|
-
# # => Prints 'hello', 'hello', 'world', 'world'
|
|
382
|
-
# enum = (1..14).repeat(3)
|
|
383
|
-
# # => returns an Enumerator when called without a block
|
|
384
|
-
# enum.first(4) # => [1, 1, 1, 2]
|
|
385
|
-
# enum.size # => 42
|
|
386
|
-
#
|
|
387
|
-
alias to_enum enum_for
|
|
388
|
-
|
|
389
|
-
# <!--
|
|
390
|
-
# rdoc-file=object.c
|
|
391
|
-
# - obj == other -> true or false
|
|
392
|
-
# - obj.equal?(other) -> true or false
|
|
393
|
-
# - obj.eql?(other) -> true or false
|
|
394
|
-
# -->
|
|
395
|
-
# Equality --- At the Object level, #== returns `true` only if `obj` and `other`
|
|
396
|
-
# are the same object. Typically, this method is overridden in descendant
|
|
397
|
-
# classes to provide class-specific meaning.
|
|
398
|
-
#
|
|
399
|
-
# Unlike #==, the #equal? method should never be overridden by subclasses as it
|
|
400
|
-
# is used to determine object identity (that is, `a.equal?(b)` if and only if
|
|
401
|
-
# `a` is the same object as `b`):
|
|
402
|
-
#
|
|
403
|
-
# obj = "a"
|
|
404
|
-
# other = obj.dup
|
|
405
|
-
#
|
|
406
|
-
# obj == other #=> true
|
|
407
|
-
# obj.equal? other #=> false
|
|
408
|
-
# obj.equal? obj #=> true
|
|
409
|
-
#
|
|
410
|
-
# The #eql? method returns `true` if `obj` and `other` refer to the same hash
|
|
411
|
-
# key. This is used by Hash to test members for equality. For any pair of
|
|
412
|
-
# objects where #eql? returns `true`, the #hash value of both objects must be
|
|
413
|
-
# equal. So any subclass that overrides #eql? should also override #hash
|
|
414
|
-
# appropriately.
|
|
415
|
-
#
|
|
416
|
-
# For objects of class Object, #eql? is synonymous with #==. Subclasses
|
|
417
|
-
# normally continue this tradition by aliasing #eql? to their overridden #==
|
|
418
|
-
# method, but there are exceptions. Numeric types, for example, perform type
|
|
419
|
-
# conversion across #==, but not across #eql?, so:
|
|
420
|
-
#
|
|
421
|
-
# 1 == 1.0 #=> true
|
|
422
|
-
# 1.eql? 1.0 #=> false
|
|
423
|
-
#
|
|
424
|
-
def eql?: (untyped) -> bool
|
|
425
|
-
|
|
426
|
-
# <!--
|
|
427
|
-
# rdoc-file=eval.c
|
|
428
|
-
# - obj.extend(module, ...) -> obj
|
|
429
|
-
# -->
|
|
430
|
-
# Adds to *obj* the instance methods from each module given as a parameter.
|
|
431
|
-
#
|
|
432
|
-
# module Mod
|
|
433
|
-
# def hello
|
|
434
|
-
# "Hello from Mod.\n"
|
|
435
|
-
# end
|
|
436
|
-
# end
|
|
437
|
-
#
|
|
438
|
-
# class Klass
|
|
439
|
-
# def hello
|
|
440
|
-
# "Hello from Klass.\n"
|
|
441
|
-
# end
|
|
442
|
-
# end
|
|
443
|
-
#
|
|
444
|
-
# k = Klass.new
|
|
445
|
-
# k.hello #=> "Hello from Klass.\n"
|
|
446
|
-
# k.extend(Mod) #=> #<Klass:0x401b3bc8>
|
|
447
|
-
# k.hello #=> "Hello from Mod.\n"
|
|
448
|
-
#
|
|
449
|
-
def extend: (*Module) -> self
|
|
450
|
-
|
|
451
|
-
# <!--
|
|
452
|
-
# rdoc-file=object.c
|
|
453
|
-
# - obj.freeze -> obj
|
|
454
|
-
# -->
|
|
455
|
-
# Prevents further modifications to *obj*. A FrozenError will be raised if
|
|
456
|
-
# modification is attempted. There is no way to unfreeze a frozen object. See
|
|
457
|
-
# also Object#frozen?.
|
|
458
|
-
#
|
|
459
|
-
# This method returns self.
|
|
460
|
-
#
|
|
461
|
-
# a = [ "a", "b", "c" ]
|
|
462
|
-
# a.freeze
|
|
463
|
-
# a << "z"
|
|
464
|
-
#
|
|
465
|
-
# *produces:*
|
|
466
|
-
#
|
|
467
|
-
# prog.rb:3:in `<<': can't modify frozen Array (FrozenError)
|
|
468
|
-
# from prog.rb:3
|
|
469
|
-
#
|
|
470
|
-
# Objects of the following classes are always frozen: Integer, Float, Symbol.
|
|
471
|
-
#
|
|
472
|
-
def freeze: () -> self
|
|
473
|
-
|
|
474
|
-
# Returns the freeze status of *obj*.
|
|
475
|
-
#
|
|
476
|
-
# a = [ "a", "b", "c" ]
|
|
477
|
-
# a.freeze #=> ["a", "b", "c"]
|
|
478
|
-
# a.frozen? #=> true
|
|
479
|
-
#
|
|
480
|
-
def frozen?: () -> bool
|
|
481
|
-
|
|
482
|
-
# <!--
|
|
483
|
-
# rdoc-file=object.c
|
|
484
|
-
# - obj.hash -> integer
|
|
485
|
-
# -->
|
|
486
|
-
# Generates an Integer hash value for this object. This function must have the
|
|
487
|
-
# property that `a.eql?(b)` implies `a.hash == b.hash`.
|
|
488
|
-
#
|
|
489
|
-
# The hash value is used along with #eql? by the Hash class to determine if two
|
|
490
|
-
# objects reference the same hash key. Any hash value that exceeds the capacity
|
|
491
|
-
# of an Integer will be truncated before being used.
|
|
492
|
-
#
|
|
493
|
-
# The hash value for an object may not be identical across invocations or
|
|
494
|
-
# implementations of Ruby. If you need a stable identifier across Ruby
|
|
495
|
-
# invocations and implementations you will need to generate one with a custom
|
|
496
|
-
# method.
|
|
497
|
-
#
|
|
498
|
-
# Certain core classes such as Integer use built-in hash calculations and do not
|
|
499
|
-
# call the #hash method when used as a hash key.
|
|
500
|
-
#
|
|
501
|
-
# When implementing your own #hash based on multiple values, the best practice
|
|
502
|
-
# is to combine the class and any values using the hash code of an array:
|
|
503
|
-
#
|
|
504
|
-
# For example:
|
|
505
|
-
#
|
|
506
|
-
# def hash
|
|
507
|
-
# [self.class, a, b, c].hash
|
|
508
|
-
# end
|
|
509
|
-
#
|
|
510
|
-
# The reason for this is that the Array#hash method already has logic for safely
|
|
511
|
-
# and efficiently combining multiple hash values.
|
|
512
|
-
#
|
|
513
|
-
def hash: () -> Integer
|
|
514
|
-
|
|
515
|
-
# <!--
|
|
516
|
-
# rdoc-file=object.c
|
|
517
|
-
# - obj.inspect -> string
|
|
518
|
-
# -->
|
|
519
|
-
# Returns a string containing a human-readable representation of *obj*. The
|
|
520
|
-
# default #inspect shows the object's class name, an encoding of its memory
|
|
521
|
-
# address, and a list of the instance variables and their values (by calling
|
|
522
|
-
# #inspect on each of them). User defined classes should override this method
|
|
523
|
-
# to provide a better representation of *obj*. When overriding this method, it
|
|
524
|
-
# should return a string whose encoding is compatible with the default external
|
|
525
|
-
# encoding.
|
|
526
|
-
#
|
|
527
|
-
# [ 1, 2, 3..4, 'five' ].inspect #=> "[1, 2, 3..4, \"five\"]"
|
|
528
|
-
# Time.new.inspect #=> "2008-03-08 19:43:39 +0900"
|
|
529
|
-
#
|
|
530
|
-
# class Foo
|
|
531
|
-
# end
|
|
532
|
-
# Foo.new.inspect #=> "#<Foo:0x0300c868>"
|
|
533
|
-
#
|
|
534
|
-
# class Bar
|
|
535
|
-
# def initialize
|
|
536
|
-
# @bar = 1
|
|
537
|
-
# end
|
|
538
|
-
# end
|
|
539
|
-
# Bar.new.inspect #=> "#<Bar:0x0300c868 @bar=1>"
|
|
540
|
-
#
|
|
541
|
-
def inspect: () -> String
|
|
542
|
-
|
|
543
|
-
# <!--
|
|
544
|
-
# rdoc-file=object.c
|
|
545
|
-
# - obj.instance_of?(class) -> true or false
|
|
546
|
-
# -->
|
|
547
|
-
# Returns `true` if *obj* is an instance of the given class. See also
|
|
548
|
-
# Object#kind_of?.
|
|
549
|
-
#
|
|
550
|
-
# class A; end
|
|
551
|
-
# class B < A; end
|
|
552
|
-
# class C < B; end
|
|
553
|
-
#
|
|
554
|
-
# b = B.new
|
|
555
|
-
# b.instance_of? A #=> false
|
|
556
|
-
# b.instance_of? B #=> true
|
|
557
|
-
# b.instance_of? C #=> false
|
|
558
|
-
#
|
|
559
|
-
def instance_of?: (Module) -> bool
|
|
560
|
-
|
|
561
|
-
# <!--
|
|
562
|
-
# rdoc-file=object.c
|
|
563
|
-
# - obj.instance_variable_defined?(symbol) -> true or false
|
|
564
|
-
# - obj.instance_variable_defined?(string) -> true or false
|
|
565
|
-
# -->
|
|
566
|
-
# Returns `true` if the given instance variable is defined in *obj*. String
|
|
567
|
-
# arguments are converted to symbols.
|
|
568
|
-
#
|
|
569
|
-
# class Fred
|
|
570
|
-
# def initialize(p1, p2)
|
|
571
|
-
# @a, @b = p1, p2
|
|
572
|
-
# end
|
|
573
|
-
# end
|
|
574
|
-
# fred = Fred.new('cat', 99)
|
|
575
|
-
# fred.instance_variable_defined?(:@a) #=> true
|
|
576
|
-
# fred.instance_variable_defined?("@b") #=> true
|
|
577
|
-
# fred.instance_variable_defined?("@c") #=> false
|
|
578
|
-
#
|
|
579
|
-
def instance_variable_defined?: (String | Symbol var) -> bool
|
|
580
|
-
|
|
581
|
-
# <!--
|
|
582
|
-
# rdoc-file=object.c
|
|
583
|
-
# - obj.instance_variable_get(symbol) -> obj
|
|
584
|
-
# - obj.instance_variable_get(string) -> obj
|
|
585
|
-
# -->
|
|
586
|
-
# Returns the value of the given instance variable, or nil if the instance
|
|
587
|
-
# variable is not set. The `@` part of the variable name should be included for
|
|
588
|
-
# regular instance variables. Throws a NameError exception if the supplied
|
|
589
|
-
# symbol is not valid as an instance variable name. String arguments are
|
|
590
|
-
# converted to symbols.
|
|
591
|
-
#
|
|
592
|
-
# class Fred
|
|
593
|
-
# def initialize(p1, p2)
|
|
594
|
-
# @a, @b = p1, p2
|
|
595
|
-
# end
|
|
596
|
-
# end
|
|
597
|
-
# fred = Fred.new('cat', 99)
|
|
598
|
-
# fred.instance_variable_get(:@a) #=> "cat"
|
|
599
|
-
# fred.instance_variable_get("@b") #=> 99
|
|
600
|
-
#
|
|
601
|
-
def instance_variable_get: (String | Symbol var) -> untyped
|
|
602
|
-
|
|
603
|
-
# <!--
|
|
604
|
-
# rdoc-file=object.c
|
|
605
|
-
# - obj.instance_variable_set(symbol, obj) -> obj
|
|
606
|
-
# - obj.instance_variable_set(string, obj) -> obj
|
|
607
|
-
# -->
|
|
608
|
-
# Sets the instance variable named by *symbol* to the given object. This may
|
|
609
|
-
# circumvent the encapsulation intended by the author of the class, so it should
|
|
610
|
-
# be used with care. The variable does not have to exist prior to this call. If
|
|
611
|
-
# the instance variable name is passed as a string, that string is converted to
|
|
612
|
-
# a symbol.
|
|
613
|
-
#
|
|
614
|
-
# class Fred
|
|
615
|
-
# def initialize(p1, p2)
|
|
616
|
-
# @a, @b = p1, p2
|
|
617
|
-
# end
|
|
618
|
-
# end
|
|
619
|
-
# fred = Fred.new('cat', 99)
|
|
620
|
-
# fred.instance_variable_set(:@a, 'dog') #=> "dog"
|
|
621
|
-
# fred.instance_variable_set(:@c, 'cat') #=> "cat"
|
|
622
|
-
# fred.inspect #=> "#<Fred:0x401b3da8 @a=\"dog\", @b=99, @c=\"cat\">"
|
|
623
|
-
#
|
|
624
|
-
def instance_variable_set: [X] (String | Symbol var, X value) -> X
|
|
625
|
-
|
|
626
|
-
# <!--
|
|
627
|
-
# rdoc-file=object.c
|
|
628
|
-
# - obj.instance_variables -> array
|
|
629
|
-
# -->
|
|
630
|
-
# Returns an array of instance variable names for the receiver. Note that simply
|
|
631
|
-
# defining an accessor does not create the corresponding instance variable.
|
|
632
|
-
#
|
|
633
|
-
# class Fred
|
|
634
|
-
# attr_accessor :a1
|
|
635
|
-
# def initialize
|
|
636
|
-
# @iv = 3
|
|
637
|
-
# end
|
|
638
|
-
# end
|
|
639
|
-
# Fred.new.instance_variables #=> [:@iv]
|
|
640
|
-
#
|
|
641
|
-
def instance_variables: () -> Array[Symbol]
|
|
642
|
-
|
|
643
|
-
# <!-- rdoc-file=object.c -->
|
|
644
|
-
# Returns `true` if *class* is the class of *obj*, or if *class* is one of the
|
|
645
|
-
# superclasses of *obj* or modules included in *obj*.
|
|
646
|
-
#
|
|
647
|
-
# module M; end
|
|
648
|
-
# class A
|
|
649
|
-
# include M
|
|
650
|
-
# end
|
|
651
|
-
# class B < A; end
|
|
652
|
-
# class C < B; end
|
|
653
|
-
#
|
|
654
|
-
# b = B.new
|
|
655
|
-
# b.is_a? A #=> true
|
|
656
|
-
# b.is_a? B #=> true
|
|
657
|
-
# b.is_a? C #=> false
|
|
658
|
-
# b.is_a? M #=> true
|
|
659
|
-
#
|
|
660
|
-
# b.kind_of? A #=> true
|
|
661
|
-
# b.kind_of? B #=> true
|
|
662
|
-
# b.kind_of? C #=> false
|
|
663
|
-
# b.kind_of? M #=> true
|
|
664
|
-
#
|
|
665
|
-
def is_a?: (Module) -> bool
|
|
666
|
-
|
|
667
|
-
# <!--
|
|
668
|
-
# rdoc-file=object.c
|
|
669
|
-
# - obj.is_a?(class) -> true or false
|
|
670
|
-
# - obj.kind_of?(class) -> true or false
|
|
671
|
-
# -->
|
|
672
|
-
# Returns `true` if *class* is the class of *obj*, or if *class* is one of the
|
|
673
|
-
# superclasses of *obj* or modules included in *obj*.
|
|
674
|
-
#
|
|
675
|
-
# module M; end
|
|
676
|
-
# class A
|
|
677
|
-
# include M
|
|
678
|
-
# end
|
|
679
|
-
# class B < A; end
|
|
680
|
-
# class C < B; end
|
|
681
|
-
#
|
|
682
|
-
# b = B.new
|
|
683
|
-
# b.is_a? A #=> true
|
|
684
|
-
# b.is_a? B #=> true
|
|
685
|
-
# b.is_a? C #=> false
|
|
686
|
-
# b.is_a? M #=> true
|
|
687
|
-
#
|
|
688
|
-
# b.kind_of? A #=> true
|
|
689
|
-
# b.kind_of? B #=> true
|
|
690
|
-
# b.kind_of? C #=> false
|
|
691
|
-
# b.kind_of? M #=> true
|
|
692
|
-
#
|
|
693
|
-
alias kind_of? is_a?
|
|
694
|
-
|
|
695
|
-
# <!--
|
|
696
|
-
# rdoc-file=object.c
|
|
697
|
-
# - obj.itself -> obj
|
|
698
|
-
# -->
|
|
699
|
-
# Returns the receiver.
|
|
700
|
-
#
|
|
701
|
-
# string = "my string"
|
|
702
|
-
# string.itself.object_id == string.object_id #=> true
|
|
703
|
-
#
|
|
704
|
-
def itself: () -> self
|
|
705
|
-
|
|
706
|
-
# <!--
|
|
707
|
-
# rdoc-file=proc.c
|
|
708
|
-
# - obj.method(sym) -> method
|
|
709
|
-
# -->
|
|
710
|
-
# Looks up the named method as a receiver in *obj*, returning a Method object
|
|
711
|
-
# (or raising NameError). The Method object acts as a closure in *obj*'s object
|
|
712
|
-
# instance, so instance variables and the value of `self` remain available.
|
|
713
|
-
#
|
|
714
|
-
# class Demo
|
|
715
|
-
# def initialize(n)
|
|
716
|
-
# @iv = n
|
|
717
|
-
# end
|
|
718
|
-
# def hello()
|
|
719
|
-
# "Hello, @iv = #{@iv}"
|
|
720
|
-
# end
|
|
721
|
-
# end
|
|
722
|
-
#
|
|
723
|
-
# k = Demo.new(99)
|
|
724
|
-
# m = k.method(:hello)
|
|
725
|
-
# m.call #=> "Hello, @iv = 99"
|
|
726
|
-
#
|
|
727
|
-
# l = Demo.new('Fred')
|
|
728
|
-
# m = l.method("hello")
|
|
729
|
-
# m.call #=> "Hello, @iv = Fred"
|
|
730
|
-
#
|
|
731
|
-
# Note that Method implements `to_proc` method, which means it can be used with
|
|
732
|
-
# iterators.
|
|
733
|
-
#
|
|
734
|
-
# [ 1, 2, 3 ].each(&method(:puts)) # => prints 3 lines to stdout
|
|
735
|
-
#
|
|
736
|
-
# out = File.open('test.txt', 'w')
|
|
737
|
-
# [ 1, 2, 3 ].each(&out.method(:puts)) # => prints 3 lines to file
|
|
738
|
-
#
|
|
739
|
-
# require 'date'
|
|
740
|
-
# %w[2017-03-01 2017-03-02].collect(&Date.method(:parse))
|
|
741
|
-
# #=> [#<Date: 2017-03-01 ((2457814j,0s,0n),+0s,2299161j)>, #<Date: 2017-03-02 ((2457815j,0s,0n),+0s,2299161j)>]
|
|
742
|
-
#
|
|
743
|
-
def method: (String | Symbol name) -> Method
|
|
744
|
-
|
|
745
|
-
# <!--
|
|
746
|
-
# rdoc-file=object.c
|
|
747
|
-
# - obj.methods(regular=true) -> array
|
|
748
|
-
# -->
|
|
749
|
-
# Returns a list of the names of public and protected methods of *obj*. This
|
|
750
|
-
# will include all the methods accessible in *obj*'s ancestors. If the optional
|
|
751
|
-
# parameter is `false`, it returns an array of *obj*'s public and protected
|
|
752
|
-
# singleton methods, the array will not include methods in modules included in
|
|
753
|
-
# *obj*.
|
|
754
|
-
#
|
|
755
|
-
# class Klass
|
|
756
|
-
# def klass_method()
|
|
757
|
-
# end
|
|
758
|
-
# end
|
|
759
|
-
# k = Klass.new
|
|
760
|
-
# k.methods[0..9] #=> [:klass_method, :nil?, :===,
|
|
761
|
-
# # :==~, :!, :eql?
|
|
762
|
-
# # :hash, :<=>, :class, :singleton_class]
|
|
763
|
-
# k.methods.length #=> 56
|
|
764
|
-
#
|
|
765
|
-
# k.methods(false) #=> []
|
|
766
|
-
# def k.singleton_method; end
|
|
767
|
-
# k.methods(false) #=> [:singleton_method]
|
|
768
|
-
#
|
|
769
|
-
# module M123; def m123; end end
|
|
770
|
-
# k.extend M123
|
|
771
|
-
# k.methods(false) #=> [:singleton_method]
|
|
772
|
-
#
|
|
773
|
-
def methods: () -> Array[Symbol]
|
|
774
|
-
|
|
775
|
-
# <!--
|
|
776
|
-
# rdoc-file=object.c
|
|
777
|
-
# - obj.nil? -> true or false
|
|
778
|
-
# -->
|
|
779
|
-
# Only the object *nil* responds `true` to `nil?`.
|
|
780
|
-
#
|
|
781
|
-
# Object.new.nil? #=> false
|
|
782
|
-
# nil.nil? #=> true
|
|
783
|
-
#
|
|
784
|
-
def nil?: () -> bool
|
|
785
|
-
|
|
786
|
-
# <!--
|
|
787
|
-
# rdoc-file=gc.c
|
|
788
|
-
# - obj.__id__ -> integer
|
|
789
|
-
# - obj.object_id -> integer
|
|
790
|
-
# -->
|
|
791
|
-
# Returns an integer identifier for `obj`.
|
|
792
|
-
#
|
|
793
|
-
# The same number will be returned on all calls to `object_id` for a given
|
|
794
|
-
# object, and no two active objects will share an id.
|
|
795
|
-
#
|
|
796
|
-
# Note: that some objects of builtin classes are reused for optimization. This
|
|
797
|
-
# is the case for immediate values and frozen string literals.
|
|
798
|
-
#
|
|
799
|
-
# BasicObject implements +__id__+, Kernel implements `object_id`.
|
|
800
|
-
#
|
|
801
|
-
# Immediate values are not passed by reference but are passed by value: `nil`,
|
|
802
|
-
# `true`, `false`, Fixnums, Symbols, and some Floats.
|
|
803
|
-
#
|
|
804
|
-
# Object.new.object_id == Object.new.object_id # => false
|
|
805
|
-
# (21 * 2).object_id == (21 * 2).object_id # => true
|
|
806
|
-
# "hello".object_id == "hello".object_id # => false
|
|
807
|
-
# "hi".freeze.object_id == "hi".freeze.object_id # => true
|
|
808
|
-
#
|
|
809
|
-
def object_id: () -> Integer
|
|
810
|
-
|
|
811
|
-
# <!--
|
|
812
|
-
# rdoc-file=object.c
|
|
813
|
-
# - obj.private_methods(all=true) -> array
|
|
814
|
-
# -->
|
|
815
|
-
# Returns the list of private methods accessible to *obj*. If the *all*
|
|
816
|
-
# parameter is set to `false`, only those methods in the receiver will be
|
|
817
|
-
# listed.
|
|
818
|
-
#
|
|
819
|
-
def private_methods: () -> Array[Symbol]
|
|
820
|
-
|
|
821
|
-
# <!--
|
|
822
|
-
# rdoc-file=object.c
|
|
823
|
-
# - obj.protected_methods(all=true) -> array
|
|
824
|
-
# -->
|
|
825
|
-
# Returns the list of protected methods accessible to *obj*. If the *all*
|
|
826
|
-
# parameter is set to `false`, only those methods in the receiver will be
|
|
827
|
-
# listed.
|
|
828
|
-
#
|
|
829
|
-
def protected_methods: () -> Array[Symbol]
|
|
830
|
-
|
|
831
|
-
# <!--
|
|
832
|
-
# rdoc-file=proc.c
|
|
833
|
-
# - obj.public_method(sym) -> method
|
|
834
|
-
# -->
|
|
835
|
-
# Similar to *method*, searches public method only.
|
|
836
|
-
#
|
|
837
|
-
def public_method: (name name) -> Method
|
|
838
|
-
|
|
839
|
-
# <!--
|
|
840
|
-
# rdoc-file=object.c
|
|
841
|
-
# - obj.public_methods(all=true) -> array
|
|
842
|
-
# -->
|
|
843
|
-
# Returns the list of public methods accessible to *obj*. If the *all* parameter
|
|
844
|
-
# is set to `false`, only those methods in the receiver will be listed.
|
|
845
|
-
#
|
|
846
|
-
def public_methods: (?boolish all) -> Array[Symbol]
|
|
847
|
-
|
|
848
|
-
# <!--
|
|
849
|
-
# rdoc-file=vm_eval.c
|
|
850
|
-
# - obj.public_send(symbol [, args...]) -> obj
|
|
851
|
-
# - obj.public_send(string [, args...]) -> obj
|
|
852
|
-
# -->
|
|
853
|
-
# Invokes the method identified by *symbol*, passing it any arguments specified.
|
|
854
|
-
# Unlike send, public_send calls public methods only. When the method is
|
|
855
|
-
# identified by a string, the string is converted to a symbol.
|
|
856
|
-
#
|
|
857
|
-
# 1.public_send(:puts, "hello") # causes NoMethodError
|
|
858
|
-
#
|
|
859
|
-
def public_send: (name name, *untyped args) ?{ (*untyped) -> untyped } -> untyped
|
|
860
|
-
|
|
861
|
-
# <!--
|
|
862
|
-
# rdoc-file=object.c
|
|
863
|
-
# - obj.remove_instance_variable(symbol) -> obj
|
|
864
|
-
# - obj.remove_instance_variable(string) -> obj
|
|
865
|
-
# -->
|
|
866
|
-
# Removes the named instance variable from *obj*, returning that variable's
|
|
867
|
-
# value. String arguments are converted to symbols.
|
|
868
|
-
#
|
|
869
|
-
# class Dummy
|
|
870
|
-
# attr_reader :var
|
|
871
|
-
# def initialize
|
|
872
|
-
# @var = 99
|
|
873
|
-
# end
|
|
874
|
-
# def remove
|
|
875
|
-
# remove_instance_variable(:@var)
|
|
876
|
-
# end
|
|
877
|
-
# end
|
|
878
|
-
# d = Dummy.new
|
|
879
|
-
# d.var #=> 99
|
|
880
|
-
# d.remove #=> 99
|
|
881
|
-
# d.var #=> nil
|
|
882
|
-
#
|
|
883
|
-
def remove_instance_variable: (name name) -> untyped
|
|
884
|
-
|
|
885
|
-
# <!--
|
|
886
|
-
# rdoc-file=vm_method.c
|
|
887
|
-
# - obj.respond_to?(symbol, include_all=false) -> true or false
|
|
888
|
-
# - obj.respond_to?(string, include_all=false) -> true or false
|
|
889
|
-
# -->
|
|
890
|
-
# Returns `true` if *obj* responds to the given method. Private and protected
|
|
891
|
-
# methods are included in the search only if the optional second parameter
|
|
892
|
-
# evaluates to `true`.
|
|
893
|
-
#
|
|
894
|
-
# If the method is not implemented, as Process.fork on Windows, File.lchmod on
|
|
895
|
-
# GNU/Linux, etc., false is returned.
|
|
896
|
-
#
|
|
897
|
-
# If the method is not defined, `respond_to_missing?` method is called and the
|
|
898
|
-
# result is returned.
|
|
899
|
-
#
|
|
900
|
-
# When the method name parameter is given as a string, the string is converted
|
|
901
|
-
# to a symbol.
|
|
902
|
-
#
|
|
903
|
-
def respond_to?: (name name, ?boolish include_all) -> bool
|
|
904
|
-
|
|
905
|
-
# <!--
|
|
906
|
-
# rdoc-file=vm_eval.c
|
|
907
|
-
# - foo.send(symbol [, args...]) -> obj
|
|
908
|
-
# - foo.__send__(symbol [, args...]) -> obj
|
|
909
|
-
# - foo.send(string [, args...]) -> obj
|
|
910
|
-
# - foo.__send__(string [, args...]) -> obj
|
|
911
|
-
# -->
|
|
912
|
-
# Invokes the method identified by *symbol*, passing it any arguments specified.
|
|
913
|
-
# When the method is identified by a string, the string is converted to a
|
|
914
|
-
# symbol.
|
|
915
|
-
#
|
|
916
|
-
# BasicObject implements +__send__+, Kernel implements `send`. `__send__` is
|
|
917
|
-
# safer than `send` when *obj* has the same method name like `Socket`. See also
|
|
918
|
-
# `public_send`.
|
|
919
|
-
#
|
|
920
|
-
# class Klass
|
|
921
|
-
# def hello(*args)
|
|
922
|
-
# "Hello " + args.join(' ')
|
|
923
|
-
# end
|
|
924
|
-
# end
|
|
925
|
-
# k = Klass.new
|
|
926
|
-
# k.send :hello, "gentle", "readers" #=> "Hello gentle readers"
|
|
927
|
-
#
|
|
928
|
-
def send: (name name, *untyped args) ?{ (*untyped) -> untyped } -> untyped
|
|
929
|
-
|
|
930
|
-
# <!--
|
|
931
|
-
# rdoc-file=object.c
|
|
932
|
-
# - obj.singleton_class -> class
|
|
933
|
-
# -->
|
|
934
|
-
# Returns the singleton class of *obj*. This method creates a new singleton
|
|
935
|
-
# class if *obj* does not have one.
|
|
936
|
-
#
|
|
937
|
-
# If *obj* is `nil`, `true`, or `false`, it returns NilClass, TrueClass, or
|
|
938
|
-
# FalseClass, respectively. If *obj* is an Integer, a Float or a Symbol, it
|
|
939
|
-
# raises a TypeError.
|
|
940
|
-
#
|
|
941
|
-
# Object.new.singleton_class #=> #<Class:#<Object:0xb7ce1e24>>
|
|
942
|
-
# String.singleton_class #=> #<Class:String>
|
|
943
|
-
# nil.singleton_class #=> NilClass
|
|
944
|
-
#
|
|
945
|
-
def singleton_class: () -> Class
|
|
946
|
-
|
|
947
|
-
# <!--
|
|
948
|
-
# rdoc-file=proc.c
|
|
949
|
-
# - obj.singleton_method(sym) -> method
|
|
950
|
-
# -->
|
|
951
|
-
# Similar to *method*, searches singleton method only.
|
|
952
|
-
#
|
|
953
|
-
# class Demo
|
|
954
|
-
# def initialize(n)
|
|
955
|
-
# @iv = n
|
|
956
|
-
# end
|
|
957
|
-
# def hello()
|
|
958
|
-
# "Hello, @iv = #{@iv}"
|
|
959
|
-
# end
|
|
960
|
-
# end
|
|
961
|
-
#
|
|
962
|
-
# k = Demo.new(99)
|
|
963
|
-
# def k.hi
|
|
964
|
-
# "Hi, @iv = #{@iv}"
|
|
965
|
-
# end
|
|
966
|
-
# m = k.singleton_method(:hi)
|
|
967
|
-
# m.call #=> "Hi, @iv = 99"
|
|
968
|
-
# m = k.singleton_method(:hello) #=> NameError
|
|
969
|
-
#
|
|
970
|
-
def singleton_method: (name name) -> Method
|
|
971
|
-
|
|
972
|
-
# <!--
|
|
973
|
-
# rdoc-file=object.c
|
|
974
|
-
# - obj.singleton_methods(all=true) -> array
|
|
975
|
-
# -->
|
|
976
|
-
# Returns an array of the names of singleton methods for *obj*. If the optional
|
|
977
|
-
# *all* parameter is true, the list will include methods in modules included in
|
|
978
|
-
# *obj*. Only public and protected singleton methods are returned.
|
|
979
|
-
#
|
|
980
|
-
# module Other
|
|
981
|
-
# def three() end
|
|
982
|
-
# end
|
|
983
|
-
#
|
|
984
|
-
# class Single
|
|
985
|
-
# def Single.four() end
|
|
986
|
-
# end
|
|
987
|
-
#
|
|
988
|
-
# a = Single.new
|
|
989
|
-
#
|
|
990
|
-
# def a.one()
|
|
991
|
-
# end
|
|
992
|
-
#
|
|
993
|
-
# class << a
|
|
994
|
-
# include Other
|
|
995
|
-
# def two()
|
|
996
|
-
# end
|
|
997
|
-
# end
|
|
998
|
-
#
|
|
999
|
-
# Single.singleton_methods #=> [:four]
|
|
1000
|
-
# a.singleton_methods(false) #=> [:two, :one]
|
|
1001
|
-
# a.singleton_methods #=> [:two, :one, :three]
|
|
1002
|
-
#
|
|
1003
|
-
def singleton_methods: () -> Array[Symbol]
|
|
1004
|
-
|
|
1005
|
-
# Yields self to the block, and then returns self. The primary purpose of this
|
|
1006
|
-
# method is to "tap into" a method chain, in order to perform operations on
|
|
1007
|
-
# intermediate results within the chain.
|
|
1008
|
-
#
|
|
1009
|
-
# (1..10) .tap {|x| puts "original: #{x}" }
|
|
1010
|
-
# .to_a .tap {|x| puts "array: #{x}" }
|
|
1011
|
-
# .select {|x| x.even? } .tap {|x| puts "evens: #{x}" }
|
|
1012
|
-
# .map {|x| x*x } .tap {|x| puts "squares: #{x}" }
|
|
1013
|
-
#
|
|
1014
|
-
def tap: () { (self) -> void } -> self
|
|
1015
|
-
|
|
1016
|
-
# Yields self to the block and returns the result of the block.
|
|
1017
|
-
#
|
|
1018
|
-
# 3.next.then {|x| x**x }.to_s #=> "256"
|
|
1019
|
-
# "my string".yield_self {|s| s.upcase } #=> "MY STRING"
|
|
1020
|
-
#
|
|
1021
|
-
# Good usage for `yield_self` is value piping in method chains:
|
|
1022
|
-
#
|
|
1023
|
-
# require 'open-uri'
|
|
1024
|
-
# require 'json'
|
|
1025
|
-
#
|
|
1026
|
-
# construct_url(arguments).
|
|
1027
|
-
# yield_self {|url| open(url).read }.
|
|
1028
|
-
# yield_self {|response| JSON.parse(response) }
|
|
1029
|
-
#
|
|
1030
|
-
# When called without block, the method returns `Enumerator`, which can be used,
|
|
1031
|
-
# for example, for conditional circuit-breaking:
|
|
1032
|
-
#
|
|
1033
|
-
# # meets condition, no-op
|
|
1034
|
-
# 1.yield_self.detect(&:odd?) # => 1
|
|
1035
|
-
# # does not meet condition, drop value
|
|
1036
|
-
# 2.yield_self.detect(&:odd?) # => nil
|
|
1037
|
-
#
|
|
1038
|
-
def yield_self: [X] () { (self) -> X } -> X
|
|
1039
|
-
| () -> Enumerator[self, untyped]
|
|
1040
|
-
|
|
1041
|
-
# <!--
|
|
1042
|
-
# rdoc-file=object.c
|
|
1043
|
-
# - obj.to_s -> string
|
|
1044
|
-
# -->
|
|
1045
|
-
# Returns a string representing *obj*. The default #to_s prints the object's
|
|
1046
|
-
# class and an encoding of the object id. As a special case, the top-level
|
|
1047
|
-
# object that is the initial execution context of Ruby programs returns
|
|
1048
|
-
# ``main''.
|
|
1049
|
-
#
|
|
1050
|
-
def to_s: () -> String
|
|
1051
|
-
|
|
1052
|
-
# Yields self to the block and returns the result of the block.
|
|
1053
|
-
#
|
|
1054
|
-
# 3.next.then {|x| x**x }.to_s #=> "256"
|
|
1055
|
-
# "my string".yield_self {|s| s.upcase } #=> "MY STRING"
|
|
1056
|
-
#
|
|
1057
|
-
# Good usage for `yield_self` is value piping in method chains:
|
|
1058
|
-
#
|
|
1059
|
-
# require 'open-uri'
|
|
1060
|
-
# require 'json'
|
|
1061
|
-
#
|
|
1062
|
-
# construct_url(arguments).
|
|
1063
|
-
# yield_self {|url| open(url).read }.
|
|
1064
|
-
# yield_self {|response| JSON.parse(response) }
|
|
1065
|
-
#
|
|
1066
|
-
# When called without block, the method returns `Enumerator`, which can be used,
|
|
1067
|
-
# for example, for conditional circuit-breaking:
|
|
1068
|
-
#
|
|
1069
|
-
# # meets condition, no-op
|
|
1070
|
-
# 1.yield_self.detect(&:odd?) # => 1
|
|
1071
|
-
# # does not meet condition, drop value
|
|
1072
|
-
# 2.yield_self.detect(&:odd?) # => nil
|
|
1073
|
-
#
|
|
1074
|
-
alias then yield_self
|
|
1075
110
|
end
|
|
1076
111
|
|
|
1077
|
-
|
|
112
|
+
# A previous incarnation of `interned` for backward-compatibility (see #1499)
|
|
113
|
+
%a{steep:deprecated}
|
|
114
|
+
type Object::name = interned
|