-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathbasic_object.rbs
More file actions
374 lines (360 loc) · 12.5 KB
/
basic_object.rbs
File metadata and controls
374 lines (360 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# <!-- rdoc-file=object.c -->
# BasicObject is the parent class of all classes in Ruby. It's an explicit
# blank class.
#
# BasicObject can be used for creating object hierarchies independent of Ruby's
# object hierarchy, proxy objects like the Delegator class, or other uses where
# namespace pollution from Ruby's methods and classes must be avoided.
#
# To avoid polluting BasicObject for other users an appropriately named subclass
# of BasicObject should be created instead of directly modifying BasicObject:
#
# class MyObjectSystem < BasicObject
# end
#
# BasicObject does not include Kernel (for methods like `puts`) and BasicObject
# is outside of the namespace of the standard library so common classes will not
# be found without using a full class path.
#
# A variety of strategies can be used to provide useful portions of the standard
# library to subclasses of BasicObject. A subclass could `include Kernel` to
# obtain `puts`, `exit`, etc. A custom Kernel-like module could be created and
# included or delegation can be used via #method_missing:
#
# class MyObjectSystem < BasicObject
# DELEGATE = [:puts, :p]
#
# def method_missing(name, *args, &block)
# return super unless DELEGATE.include? name
# ::Kernel.send(name, *args, &block)
# end
#
# def respond_to_missing?(name, include_private = false)
# DELEGATE.include?(name) or super
# end
# end
#
# Access to classes and modules from the Ruby standard library can be obtained
# in a BasicObject subclass by referencing the desired constant from the root
# like `::File` or `::Enumerator`. Like #method_missing, #const_missing can be
# used to delegate constant lookup to `Object`:
#
# class MyObjectSystem < BasicObject
# def self.const_missing(name)
# ::Object.const_get(name)
# end
# end
#
# ### What's Here
#
# These are the methods defined for BasicObject:
#
# * ::new: Returns a new BasicObject instance.
# * #!: Returns the boolean negation of `self`: `true` or `false`.
# * #!=: Returns whether `self` and the given object are *not* equal.
# * #==: Returns whether `self` and the given object are equivalent.
# * #__id__: Returns the integer object identifier for `self`.
# * #__send__: Calls the method identified by the given symbol.
# * #equal?: Returns whether `self` and the given object are the same object.
# * #instance_eval: Evaluates the given string or block in the context of
# `self`.
# * #instance_exec: Executes the given block in the context of `self`, passing
# the given arguments.
#
class BasicObject
# <!--
# rdoc-file=object.c
# - !obj -> true or false
# -->
# Boolean negate.
#
def !: () -> bool
# <!--
# rdoc-file=object.c
# - obj != other -> true or false
# -->
# Returns true if two objects are not-equal, otherwise false.
#
def !=: (untyped other) -> bool
# <!--
# rdoc-file=object.c
# - obj == other -> true or false
# - obj.equal?(other) -> true or false
# - obj.eql?(other) -> true or false
# -->
# Equality --- At the Object level, #== returns `true` only if `obj` and `other`
# are the same object. Typically, this method is overridden in descendant
# classes to provide class-specific meaning.
#
# Unlike #==, the #equal? method should never be overridden by subclasses as it
# is used to determine object identity (that is, `a.equal?(b)` if and only if
# `a` is the same object as `b`):
#
# obj = "a"
# other = obj.dup
#
# obj == other #=> true
# obj.equal? other #=> false
# obj.equal? obj #=> true
#
# The #eql? method returns `true` if `obj` and `other` refer to the same hash
# key. This is used by Hash to test members for equality. For any pair of
# objects where #eql? returns `true`, the #hash value of both objects must be
# equal. So any subclass that overrides #eql? should also override #hash
# appropriately.
#
# For objects of class Object, #eql? is synonymous with #==. Subclasses
# normally continue this tradition by aliasing #eql? to their overridden #==
# method, but there are exceptions. Numeric types, for example, perform type
# conversion across #==, but not across #eql?, so:
#
# 1 == 1.0 #=> true
# 1.eql? 1.0 #=> false
#
def ==: (untyped other) -> bool
# <!--
# rdoc-file=gc.c
# - obj.__id__ -> integer
# - obj.object_id -> integer
# -->
# Returns an integer identifier for `obj`.
#
# The same number will be returned on all calls to `object_id` for a given
# object, and no two active objects will share an id.
#
# Note: that some objects of builtin classes are reused for optimization. This
# is the case for immediate values and frozen string literals.
#
# BasicObject implements `__id__`, Kernel implements `object_id`.
#
# Immediate values are not passed by reference but are passed by value: `nil`,
# `true`, `false`, Fixnums, Symbols, and some Floats.
#
# Object.new.object_id == Object.new.object_id # => false
# (21 * 2).object_id == (21 * 2).object_id # => true
# "hello".object_id == "hello".object_id # => false
# "hi".freeze.object_id == "hi".freeze.object_id # => true
#
def __id__: () -> Integer
# <!--
# rdoc-file=vm_eval.c
# - foo.send(symbol [, args...]) -> obj
# - foo.__send__(symbol [, args...]) -> obj
# - foo.send(string [, args...]) -> obj
# - foo.__send__(string [, args...]) -> obj
# -->
# Invokes the method identified by *symbol*, passing it any arguments specified.
# When the method is identified by a string, the string is converted to a
# symbol.
#
# BasicObject implements `__send__`, Kernel implements `send`. `__send__` is
# safer than `send` when *obj* has the same method name like `Socket`. See also
# `public_send`.
#
# class Klass
# def hello(*args)
# "Hello " + args.join(' ')
# end
# end
# k = Klass.new
# k.send :hello, "gentle", "readers" #=> "Hello gentle readers"
#
def __send__: (interned name, *untyped, **untyped) ?{ (*untyped, **untyped) -> untyped } -> untyped
# <!-- rdoc-file=object.c -->
# Equality --- At the Object level, #== returns `true` only if `obj` and `other`
# are the same object. Typically, this method is overridden in descendant
# classes to provide class-specific meaning.
#
# Unlike #==, the #equal? method should never be overridden by subclasses as it
# is used to determine object identity (that is, `a.equal?(b)` if and only if
# `a` is the same object as `b`):
#
# obj = "a"
# other = obj.dup
#
# obj == other #=> true
# obj.equal? other #=> false
# obj.equal? obj #=> true
#
# The #eql? method returns `true` if `obj` and `other` refer to the same hash
# key. This is used by Hash to test members for equality. For any pair of
# objects where #eql? returns `true`, the #hash value of both objects must be
# equal. So any subclass that overrides #eql? should also override #hash
# appropriately.
#
# For objects of class Object, #eql? is synonymous with #==. Subclasses
# normally continue this tradition by aliasing #eql? to their overridden #==
# method, but there are exceptions. Numeric types, for example, perform type
# conversion across #==, but not across #eql?, so:
#
# 1 == 1.0 #=> true
# 1.eql? 1.0 #=> false
#
alias equal? ==
# <!--
# rdoc-file=vm_eval.c
# - obj.instance_eval(string [, filename [, lineno]] ) -> obj
# - obj.instance_eval {|obj| block } -> obj
# -->
# Evaluates a string containing Ruby source code, or the given block, within the
# context of the receiver (*obj*). In order to set the context, the variable
# `self` is set to *obj* while the code is executing, giving the code access to
# *obj*'s instance variables and private methods.
#
# When `instance_eval` is given a block, *obj* is also passed in as the block's
# only argument.
#
# When `instance_eval` is given a `String`, the optional second and third
# parameters supply a filename and starting line number that are used when
# reporting compilation errors.
#
# class KlassWithSecret
# def initialize
# @secret = 99
# end
# private
# def the_secret
# "Ssssh! The secret is #{@secret}."
# end
# end
# k = KlassWithSecret.new
# k.instance_eval { @secret } #=> 99
# k.instance_eval { the_secret } #=> "Ssssh! The secret is 99."
# k.instance_eval {|obj| obj == self } #=> true
#
def instance_eval: (string code, ?string filename, ?int lineno) -> untyped
| [U] () { (self) [self: self] -> U } -> U
# <!--
# rdoc-file=vm_eval.c
# - obj.instance_exec(arg...) {|var...| block } -> obj
# -->
# Executes the given block within the context of the receiver (*obj*). In order
# to set the context, the variable `self` is set to *obj* while the code is
# executing, giving the code access to *obj*'s instance variables. Arguments
# are passed as block parameters.
#
# class KlassWithSecret
# def initialize
# @secret = 99
# end
# end
# k = KlassWithSecret.new
# k.instance_exec(5) {|x| @secret+x } #=> 104
#
def instance_exec: [U, V] (*V args) { (*V args) [self: self] -> U } -> U
# <!--
# rdoc-file=object.c
# - BasicObject.new
# -->
# Returns a new BasicObject.
#
def initialize: () -> nil
private
# <!--
# rdoc-file=vm_eval.c
# - obj.method_missing(symbol [, *args] ) -> result
# -->
# Invoked by Ruby when *obj* is sent a message it cannot handle. *symbol* is the
# symbol for the method called, and *args* are any arguments that were passed to
# it. By default, the interpreter raises an error when this method is called.
# However, it is possible to override the method to provide more dynamic
# behavior. If it is decided that a particular method should not be handled,
# then *super* should be called, so that ancestors can pick up the missing
# method. The example below creates a class `Roman`, which responds to methods
# with names consisting of roman numerals, returning the corresponding integer
# values.
#
# class Roman
# def roman_to_int(str)
# # ...
# end
#
# def method_missing(symbol, *args)
# str = symbol.id2name
# begin
# roman_to_int(str)
# rescue
# super(symbol, *args)
# end
# end
# end
#
# r = Roman.new
# r.iv #=> 4
# r.xxiii #=> 23
# r.mm #=> 2000
# r.foo #=> NoMethodError
#
def method_missing: (Symbol, *untyped, **untyped) ?{ (*untyped, **untyped) -> untyped } -> untyped
# <!--
# rdoc-file=object.c
# - singleton_method_added(symbol)
# -->
# Invoked as a callback whenever a singleton method is added to the receiver.
#
# module Chatty
# def Chatty.singleton_method_added(id)
# puts "Adding #{id.id2name}"
# end
# def self.one() end
# def two() end
# def Chatty.three() end
# end
#
# *produces:*
#
# Adding singleton_method_added
# Adding one
# Adding three
#
def singleton_method_added: (Symbol name) -> nil
# <!--
# rdoc-file=object.c
# - singleton_method_removed(symbol)
# -->
# Invoked as a callback whenever a singleton method is removed from the
# receiver.
#
# module Chatty
# def Chatty.singleton_method_removed(id)
# puts "Removing #{id.id2name}"
# end
# def self.one() end
# def two() end
# def Chatty.three() end
# class << self
# remove_method :three
# remove_method :one
# end
# end
#
# *produces:*
#
# Removing three
# Removing one
#
def singleton_method_removed: (Symbol name) -> nil
# <!--
# rdoc-file=object.c
# - singleton_method_undefined(symbol)
# -->
# Invoked as a callback whenever a singleton method is undefined in the
# receiver.
#
# module Chatty
# def Chatty.singleton_method_undefined(id)
# puts "Undefining #{id.id2name}"
# end
# def Chatty.one() end
# class << self
# undef_method(:one)
# end
# end
#
# *produces:*
#
# Undefining one
#
def singleton_method_undefined: (Symbol name) -> nil
def self.superclass -> nil
end