rbs 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.github/workflows/ruby.yml +28 -0
- data/.gitignore +12 -0
- data/.rubocop.yml +15 -0
- data/BSDL +22 -0
- data/CHANGELOG.md +9 -0
- data/COPYING +56 -0
- data/Gemfile +6 -0
- data/README.md +93 -0
- data/Rakefile +142 -0
- data/bin/annotate-with-rdoc +157 -0
- data/bin/console +14 -0
- data/bin/query-rdoc +103 -0
- data/bin/setup +10 -0
- data/bin/sort +89 -0
- data/bin/test_runner.rb +16 -0
- data/docs/CONTRIBUTING.md +97 -0
- data/docs/sigs.md +148 -0
- data/docs/stdlib.md +152 -0
- data/docs/syntax.md +528 -0
- data/exe/rbs +7 -0
- data/lib/rbs.rb +64 -0
- data/lib/rbs/ast/annotation.rb +27 -0
- data/lib/rbs/ast/comment.rb +27 -0
- data/lib/rbs/ast/declarations.rb +395 -0
- data/lib/rbs/ast/members.rb +362 -0
- data/lib/rbs/buffer.rb +50 -0
- data/lib/rbs/builtin_names.rb +55 -0
- data/lib/rbs/cli.rb +558 -0
- data/lib/rbs/constant.rb +26 -0
- data/lib/rbs/constant_table.rb +150 -0
- data/lib/rbs/definition.rb +170 -0
- data/lib/rbs/definition_builder.rb +919 -0
- data/lib/rbs/environment.rb +281 -0
- data/lib/rbs/environment_loader.rb +136 -0
- data/lib/rbs/environment_walker.rb +124 -0
- data/lib/rbs/errors.rb +187 -0
- data/lib/rbs/location.rb +102 -0
- data/lib/rbs/method_type.rb +123 -0
- data/lib/rbs/namespace.rb +91 -0
- data/lib/rbs/parser.y +1344 -0
- data/lib/rbs/prototype/rb.rb +553 -0
- data/lib/rbs/prototype/rbi.rb +587 -0
- data/lib/rbs/prototype/runtime.rb +381 -0
- data/lib/rbs/substitution.rb +46 -0
- data/lib/rbs/test.rb +26 -0
- data/lib/rbs/test/errors.rb +61 -0
- data/lib/rbs/test/hook.rb +294 -0
- data/lib/rbs/test/setup.rb +58 -0
- data/lib/rbs/test/spy.rb +325 -0
- data/lib/rbs/test/test_helper.rb +183 -0
- data/lib/rbs/test/type_check.rb +254 -0
- data/lib/rbs/type_name.rb +70 -0
- data/lib/rbs/types.rb +936 -0
- data/lib/rbs/variance_calculator.rb +138 -0
- data/lib/rbs/vendorer.rb +47 -0
- data/lib/rbs/version.rb +3 -0
- data/lib/rbs/writer.rb +269 -0
- data/lib/ruby/signature.rb +7 -0
- data/rbs.gemspec +46 -0
- data/stdlib/abbrev/abbrev.rbs +60 -0
- data/stdlib/base64/base64.rbs +71 -0
- data/stdlib/benchmark/benchmark.rbs +372 -0
- data/stdlib/builtin/array.rbs +1997 -0
- data/stdlib/builtin/basic_object.rbs +280 -0
- data/stdlib/builtin/binding.rbs +177 -0
- data/stdlib/builtin/builtin.rbs +45 -0
- data/stdlib/builtin/class.rbs +145 -0
- data/stdlib/builtin/comparable.rbs +116 -0
- data/stdlib/builtin/complex.rbs +400 -0
- data/stdlib/builtin/constants.rbs +37 -0
- data/stdlib/builtin/data.rbs +5 -0
- data/stdlib/builtin/deprecated.rbs +2 -0
- data/stdlib/builtin/dir.rbs +413 -0
- data/stdlib/builtin/encoding.rbs +607 -0
- data/stdlib/builtin/enumerable.rbs +404 -0
- data/stdlib/builtin/enumerator.rbs +260 -0
- data/stdlib/builtin/errno.rbs +781 -0
- data/stdlib/builtin/errors.rbs +582 -0
- data/stdlib/builtin/exception.rbs +194 -0
- data/stdlib/builtin/false_class.rbs +40 -0
- data/stdlib/builtin/fiber.rbs +68 -0
- data/stdlib/builtin/fiber_error.rbs +12 -0
- data/stdlib/builtin/file.rbs +1076 -0
- data/stdlib/builtin/file_test.rbs +59 -0
- data/stdlib/builtin/float.rbs +696 -0
- data/stdlib/builtin/gc.rbs +243 -0
- data/stdlib/builtin/hash.rbs +1029 -0
- data/stdlib/builtin/integer.rbs +707 -0
- data/stdlib/builtin/io.rbs +683 -0
- data/stdlib/builtin/kernel.rbs +576 -0
- data/stdlib/builtin/marshal.rbs +161 -0
- data/stdlib/builtin/match_data.rbs +271 -0
- data/stdlib/builtin/math.rbs +369 -0
- data/stdlib/builtin/method.rbs +185 -0
- data/stdlib/builtin/module.rbs +1104 -0
- data/stdlib/builtin/nil_class.rbs +82 -0
- data/stdlib/builtin/numeric.rbs +409 -0
- data/stdlib/builtin/object.rbs +824 -0
- data/stdlib/builtin/proc.rbs +429 -0
- data/stdlib/builtin/process.rbs +1227 -0
- data/stdlib/builtin/random.rbs +267 -0
- data/stdlib/builtin/range.rbs +226 -0
- data/stdlib/builtin/rational.rbs +424 -0
- data/stdlib/builtin/rb_config.rbs +57 -0
- data/stdlib/builtin/regexp.rbs +1083 -0
- data/stdlib/builtin/ruby_vm.rbs +14 -0
- data/stdlib/builtin/signal.rbs +55 -0
- data/stdlib/builtin/string.rbs +1901 -0
- data/stdlib/builtin/string_io.rbs +284 -0
- data/stdlib/builtin/struct.rbs +40 -0
- data/stdlib/builtin/symbol.rbs +228 -0
- data/stdlib/builtin/thread.rbs +1108 -0
- data/stdlib/builtin/thread_group.rbs +23 -0
- data/stdlib/builtin/time.rbs +1047 -0
- data/stdlib/builtin/trace_point.rbs +290 -0
- data/stdlib/builtin/true_class.rbs +46 -0
- data/stdlib/builtin/unbound_method.rbs +153 -0
- data/stdlib/builtin/warning.rbs +17 -0
- data/stdlib/coverage/coverage.rbs +62 -0
- data/stdlib/csv/csv.rbs +773 -0
- data/stdlib/erb/erb.rbs +392 -0
- data/stdlib/find/find.rbs +40 -0
- data/stdlib/ipaddr/ipaddr.rbs +247 -0
- data/stdlib/json/json.rbs +335 -0
- data/stdlib/pathname/pathname.rbs +1093 -0
- data/stdlib/prime/integer-extension.rbs +23 -0
- data/stdlib/prime/prime.rbs +188 -0
- data/stdlib/securerandom/securerandom.rbs +9 -0
- data/stdlib/set/set.rbs +301 -0
- data/stdlib/tmpdir/tmpdir.rbs +53 -0
- metadata +292 -0
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
class Method < Object
|
|
2
|
+
# Returns a `Proc` object corresponding to this method.
|
|
3
|
+
def to_proc: () -> Proc
|
|
4
|
+
|
|
5
|
+
# Invokes the *meth* with the specified arguments, returning the method’s
|
|
6
|
+
# return value.
|
|
7
|
+
#
|
|
8
|
+
# ```ruby
|
|
9
|
+
# m = 12.method("+")
|
|
10
|
+
# m.call(3) #=> 15
|
|
11
|
+
# m.call(20) #=> 32
|
|
12
|
+
# ```
|
|
13
|
+
def call: (*untyped args) -> untyped
|
|
14
|
+
|
|
15
|
+
# Returns a proc that is the composition of this method and the given *g*
|
|
16
|
+
# . The returned proc takes a variable number of arguments, calls *g* with
|
|
17
|
+
# them then calls this method with the result.
|
|
18
|
+
#
|
|
19
|
+
# ```ruby
|
|
20
|
+
# def f(x)
|
|
21
|
+
# x * x
|
|
22
|
+
# end
|
|
23
|
+
#
|
|
24
|
+
# f = self.method(:f)
|
|
25
|
+
# g = proc {|x| x + x }
|
|
26
|
+
# p (f << g).call(2) #=> 16
|
|
27
|
+
# ```
|
|
28
|
+
def <<: (Proc g) -> Proc
|
|
29
|
+
|
|
30
|
+
# Invokes the method with `obj` as the parameter like
|
|
31
|
+
# [call](Method.downloaded.ruby_doc#method-i-call). This allows a method
|
|
32
|
+
# object to be the target of a `when` clause in a case statement.
|
|
33
|
+
#
|
|
34
|
+
# ```ruby
|
|
35
|
+
# require 'prime'
|
|
36
|
+
#
|
|
37
|
+
# case 1373
|
|
38
|
+
# when Prime.method(:prime?)
|
|
39
|
+
# # ...
|
|
40
|
+
# end
|
|
41
|
+
# ```
|
|
42
|
+
alias === call
|
|
43
|
+
|
|
44
|
+
# Returns a proc that is the composition of this method and the given *g*
|
|
45
|
+
# . The returned proc takes a variable number of arguments, calls *g* with
|
|
46
|
+
# them then calls this method with the result.
|
|
47
|
+
#
|
|
48
|
+
# ```ruby
|
|
49
|
+
# def f(x)
|
|
50
|
+
# x * x
|
|
51
|
+
# end
|
|
52
|
+
#
|
|
53
|
+
# f = self.method(:f)
|
|
54
|
+
# g = proc {|x| x + x }
|
|
55
|
+
# p (f >> g).call(2) #=> 8
|
|
56
|
+
# ```
|
|
57
|
+
def >>: (Proc g) -> Proc
|
|
58
|
+
|
|
59
|
+
# Invokes the *meth* with the specified arguments, returning the method’s
|
|
60
|
+
# return value.
|
|
61
|
+
#
|
|
62
|
+
# ```ruby
|
|
63
|
+
# m = 12.method("+")
|
|
64
|
+
# m.call(3) #=> 15
|
|
65
|
+
# m.call(20) #=> 32
|
|
66
|
+
# ```
|
|
67
|
+
alias [] call
|
|
68
|
+
|
|
69
|
+
# Returns an indication of the number of arguments accepted by a method.
|
|
70
|
+
# Returns a nonnegative integer for methods that take a fixed number of
|
|
71
|
+
# arguments. For Ruby methods that take a variable number of arguments,
|
|
72
|
+
# returns -n-1, where n is the number of required arguments. Keyword
|
|
73
|
+
# arguments will be considered as a single additional argument, that
|
|
74
|
+
# argument being mandatory if any keyword argument is mandatory. For
|
|
75
|
+
# methods written in C, returns -1 if the call takes a variable number of
|
|
76
|
+
# arguments.
|
|
77
|
+
#
|
|
78
|
+
# class C
|
|
79
|
+
# def one; end
|
|
80
|
+
# def two(a); end
|
|
81
|
+
# def three(*a); end
|
|
82
|
+
# def four(a, b); end
|
|
83
|
+
# def five(a, b, *c); end
|
|
84
|
+
# def six(a, b, *c, &d); end
|
|
85
|
+
# def seven(a, b, x:0); end
|
|
86
|
+
# def eight(x:, y:); end
|
|
87
|
+
# def nine(x:, y:, **z); end
|
|
88
|
+
# def ten(*a, x:, y:); end
|
|
89
|
+
# end
|
|
90
|
+
# c = C.new
|
|
91
|
+
# c.method(:one).arity #=> 0
|
|
92
|
+
# c.method(:two).arity #=> 1
|
|
93
|
+
# c.method(:three).arity #=> -1
|
|
94
|
+
# c.method(:four).arity #=> 2
|
|
95
|
+
# c.method(:five).arity #=> -3
|
|
96
|
+
# c.method(:six).arity #=> -3
|
|
97
|
+
# c.method(:seven).arity #=> -3
|
|
98
|
+
# c.method(:eight).arity #=> 1
|
|
99
|
+
# c.method(:nine).arity #=> 1
|
|
100
|
+
# c.method(:ten).arity #=> -2
|
|
101
|
+
#
|
|
102
|
+
# "cat".method(:size).arity #=> 0
|
|
103
|
+
# "cat".method(:replace).arity #=> 1
|
|
104
|
+
# "cat".method(:squeeze).arity #=> -1
|
|
105
|
+
# "cat".method(:count).arity #=> -1
|
|
106
|
+
def arity: () -> Integer
|
|
107
|
+
|
|
108
|
+
# Returns a clone of this method.
|
|
109
|
+
#
|
|
110
|
+
# ```ruby
|
|
111
|
+
# class A
|
|
112
|
+
# def foo
|
|
113
|
+
# return "bar"
|
|
114
|
+
# end
|
|
115
|
+
# end
|
|
116
|
+
#
|
|
117
|
+
# m = A.new.method(:foo)
|
|
118
|
+
# m.call # => "bar"
|
|
119
|
+
# n = m.clone.call # => "bar"
|
|
120
|
+
# ```
|
|
121
|
+
def clone: () -> Method
|
|
122
|
+
|
|
123
|
+
def curry: (?Integer arity) -> Proc
|
|
124
|
+
|
|
125
|
+
# Returns the name of the method.
|
|
126
|
+
def name: () -> Symbol
|
|
127
|
+
|
|
128
|
+
# Returns the original name of the method.
|
|
129
|
+
#
|
|
130
|
+
# ```ruby
|
|
131
|
+
# class C
|
|
132
|
+
# def foo; end
|
|
133
|
+
# alias bar foo
|
|
134
|
+
# end
|
|
135
|
+
# C.instance_method(:bar).original_name # => :foo
|
|
136
|
+
# ```
|
|
137
|
+
def original_name: () -> Symbol
|
|
138
|
+
|
|
139
|
+
# Returns the class or module that defines the method. See also receiver.
|
|
140
|
+
#
|
|
141
|
+
# ```ruby
|
|
142
|
+
# (1..3).method(:map).owner #=> Enumerable
|
|
143
|
+
# ```
|
|
144
|
+
def owner: () -> (Class | Module)
|
|
145
|
+
|
|
146
|
+
# Returns the parameter information of this method.
|
|
147
|
+
#
|
|
148
|
+
# ```ruby
|
|
149
|
+
# def foo(bar); end
|
|
150
|
+
# method(:foo).parameters #=> [[:req, :bar]]
|
|
151
|
+
#
|
|
152
|
+
# def foo(bar, baz, bat, &blk); end
|
|
153
|
+
# method(:foo).parameters #=> [[:req, :bar], [:req, :baz], [:req, :bat], [:block, :blk]]
|
|
154
|
+
#
|
|
155
|
+
# def foo(bar, *args); end
|
|
156
|
+
# method(:foo).parameters #=> [[:req, :bar], [:rest, :args]]
|
|
157
|
+
#
|
|
158
|
+
# def foo(bar, baz, *args, &blk); end
|
|
159
|
+
# method(:foo).parameters #=> [[:req, :bar], [:req, :baz], [:rest, :args], [:block, :blk]]
|
|
160
|
+
# ```
|
|
161
|
+
def parameters: () -> ::Array[
|
|
162
|
+
[:req | :opt | :rest | :keyreq | :key | :keyrest | :block, Symbol] |
|
|
163
|
+
[:rest | :keyrest]
|
|
164
|
+
]
|
|
165
|
+
|
|
166
|
+
# Returns the bound receiver of the method object.
|
|
167
|
+
#
|
|
168
|
+
# ```ruby
|
|
169
|
+
# (1..3).method(:map).receiver # => 1..3
|
|
170
|
+
# ```
|
|
171
|
+
def receiver: () -> untyped
|
|
172
|
+
|
|
173
|
+
# Returns the Ruby source filename and line number containing this method
|
|
174
|
+
# or nil if this method was not defined in Ruby (i.e. native).
|
|
175
|
+
def source_location: () -> [String, Integer]?
|
|
176
|
+
|
|
177
|
+
# Returns a [Method](Method.downloaded.ruby_doc) of superclass which would
|
|
178
|
+
# be called when super is used or nil if there is no method on superclass.
|
|
179
|
+
def super_method: () -> Method?
|
|
180
|
+
|
|
181
|
+
# Dissociates *meth* from its current receiver. The resulting
|
|
182
|
+
# `UnboundMethod` can subsequently be bound to a new object of the same
|
|
183
|
+
# class (see `UnboundMethod` ).
|
|
184
|
+
def unbind: () -> UnboundMethod
|
|
185
|
+
end
|
|
@@ -0,0 +1,1104 @@
|
|
|
1
|
+
# A Module is a collection of methods and constants. The methods in a module may
|
|
2
|
+
# be instance methods or module methods. Instance methods appear as methods in a
|
|
3
|
+
# class when the module is included, module methods do not. Conversely, module
|
|
4
|
+
# methods may be called without creating an encapsulating object, while instance
|
|
5
|
+
# methods may not. (See Module#module_function.)
|
|
6
|
+
#
|
|
7
|
+
# In the descriptions that follow, the parameter *sym* refers to a symbol, which
|
|
8
|
+
# is either a quoted string or a Symbol (such as `:name`).
|
|
9
|
+
#
|
|
10
|
+
# module Mod
|
|
11
|
+
# include Math
|
|
12
|
+
# CONST = 1
|
|
13
|
+
# def meth
|
|
14
|
+
# # ...
|
|
15
|
+
# end
|
|
16
|
+
# end
|
|
17
|
+
# Mod.class #=> Module
|
|
18
|
+
# Mod.constants #=> [:CONST, :PI, :E]
|
|
19
|
+
# Mod.instance_methods #=> [:meth]
|
|
20
|
+
#
|
|
21
|
+
class Module < Object
|
|
22
|
+
# In the first form, returns an array of the names of all constants accessible
|
|
23
|
+
# from the point of call. This list includes the names of all modules and
|
|
24
|
+
# classes defined in the global scope.
|
|
25
|
+
#
|
|
26
|
+
# Module.constants.first(4)
|
|
27
|
+
# # => [:ARGF, :ARGV, :ArgumentError, :Array]
|
|
28
|
+
#
|
|
29
|
+
# Module.constants.include?(:SEEK_SET) # => false
|
|
30
|
+
#
|
|
31
|
+
# class IO
|
|
32
|
+
# Module.constants.include?(:SEEK_SET) # => true
|
|
33
|
+
# end
|
|
34
|
+
#
|
|
35
|
+
# The second form calls the instance method `constants`.
|
|
36
|
+
#
|
|
37
|
+
def self.constants: () -> ::Array[Integer]
|
|
38
|
+
|
|
39
|
+
# Returns the list of `Modules` nested at the point of call.
|
|
40
|
+
#
|
|
41
|
+
# module M1
|
|
42
|
+
# module M2
|
|
43
|
+
# $a = Module.nesting
|
|
44
|
+
# end
|
|
45
|
+
# end
|
|
46
|
+
# $a #=> [M1::M2, M1]
|
|
47
|
+
# $a[0].name #=> "M1::M2"
|
|
48
|
+
#
|
|
49
|
+
def self.nesting: () -> ::Array[Module]
|
|
50
|
+
|
|
51
|
+
# Returns an array of all modules used in the current scope. The ordering of
|
|
52
|
+
# modules in the resulting array is not defined.
|
|
53
|
+
#
|
|
54
|
+
# module A
|
|
55
|
+
# refine Object do
|
|
56
|
+
# end
|
|
57
|
+
# end
|
|
58
|
+
#
|
|
59
|
+
# module B
|
|
60
|
+
# refine Object do
|
|
61
|
+
# end
|
|
62
|
+
# end
|
|
63
|
+
#
|
|
64
|
+
# using A
|
|
65
|
+
# using B
|
|
66
|
+
# p Module.used_modules
|
|
67
|
+
#
|
|
68
|
+
# *produces:*
|
|
69
|
+
#
|
|
70
|
+
# [B, A]
|
|
71
|
+
#
|
|
72
|
+
def self.used_modules: () -> ::Array[Module]
|
|
73
|
+
|
|
74
|
+
# Returns true if *mod* is a subclass of *other*. Returns `nil` if there's no
|
|
75
|
+
# relationship between the two. (Think of the relationship in terms of the class
|
|
76
|
+
# definition: "class A < B" implies "A < B".)
|
|
77
|
+
#
|
|
78
|
+
def <: (Module other) -> bool?
|
|
79
|
+
|
|
80
|
+
# Returns true if *mod* is a subclass of *other* or is the same as *other*.
|
|
81
|
+
# Returns `nil` if there's no relationship between the two. (Think of the
|
|
82
|
+
# relationship in terms of the class definition: "class A < B" implies "A < B".)
|
|
83
|
+
#
|
|
84
|
+
def <=: (Module other) -> bool?
|
|
85
|
+
|
|
86
|
+
# Comparison---Returns -1, 0, +1 or nil depending on whether `module` includes
|
|
87
|
+
# `other_module`, they are the same, or if `module` is included by
|
|
88
|
+
# `other_module`.
|
|
89
|
+
#
|
|
90
|
+
# Returns `nil` if `module` has no relationship with `other_module`, if
|
|
91
|
+
# `other_module` is not a module, or if the two values are incomparable.
|
|
92
|
+
#
|
|
93
|
+
def <=>: (Module other) -> Integer?
|
|
94
|
+
|
|
95
|
+
# Equality --- At the Object level, #== returns `true` only if `obj` and `other`
|
|
96
|
+
# are the same object. Typically, this method is overridden in descendant
|
|
97
|
+
# classes to provide class-specific meaning.
|
|
98
|
+
#
|
|
99
|
+
# Unlike #==, the #equal? method should never be overridden by subclasses as it
|
|
100
|
+
# is used to determine object identity (that is, `a.equal?(b)` if and only if
|
|
101
|
+
# `a` is the same object as `b`):
|
|
102
|
+
#
|
|
103
|
+
# obj = "a"
|
|
104
|
+
# other = obj.dup
|
|
105
|
+
#
|
|
106
|
+
# obj == other #=> true
|
|
107
|
+
# obj.equal? other #=> false
|
|
108
|
+
# obj.equal? obj #=> true
|
|
109
|
+
#
|
|
110
|
+
# The #eql? method returns `true` if `obj` and `other` refer to the same hash
|
|
111
|
+
# key. This is used by Hash to test members for equality. For any pair of
|
|
112
|
+
# objects where #eql? returns `true`, the #hash value of both objects must be
|
|
113
|
+
# equal. So any subclass that overrides #eql? should also override #hash
|
|
114
|
+
# appropriately.
|
|
115
|
+
#
|
|
116
|
+
# For objects of class Object, #eql? is synonymous with #==. Subclasses
|
|
117
|
+
# normally continue this tradition by aliasing #eql? to their overridden #==
|
|
118
|
+
# method, but there are exceptions. Numeric types, for example, perform type
|
|
119
|
+
# conversion across #==, but not across #eql?, so:
|
|
120
|
+
#
|
|
121
|
+
# 1 == 1.0 #=> true
|
|
122
|
+
# 1.eql? 1.0 #=> false
|
|
123
|
+
#
|
|
124
|
+
def ==: (untyped other) -> bool
|
|
125
|
+
|
|
126
|
+
# Case Equality---Returns `true` if *obj* is an instance of *mod* or an instance
|
|
127
|
+
# of one of *mod*'s descendants. Of limited use for modules, but can be used in
|
|
128
|
+
# `case` statements to classify objects by class.
|
|
129
|
+
#
|
|
130
|
+
def ===: (untyped other) -> bool
|
|
131
|
+
|
|
132
|
+
# Returns true if *mod* is an ancestor of *other*. Returns `nil` if there's no
|
|
133
|
+
# relationship between the two. (Think of the relationship in terms of the class
|
|
134
|
+
# definition: "class A < B" implies "B > A".)
|
|
135
|
+
#
|
|
136
|
+
def >: (Module other) -> bool?
|
|
137
|
+
|
|
138
|
+
# Returns true if *mod* is an ancestor of *other*, or the two modules are the
|
|
139
|
+
# same. Returns `nil` if there's no relationship between the two. (Think of the
|
|
140
|
+
# relationship in terms of the class definition: "class A < B" implies "B > A".)
|
|
141
|
+
#
|
|
142
|
+
def >=: (Module other) -> bool?
|
|
143
|
+
|
|
144
|
+
# Makes *new_name* a new copy of the method *old_name*. This can be used to
|
|
145
|
+
# retain access to methods that are overridden.
|
|
146
|
+
#
|
|
147
|
+
# module Mod
|
|
148
|
+
# alias_method :orig_exit, :exit
|
|
149
|
+
# def exit(code=0)
|
|
150
|
+
# puts "Exiting with code #{code}"
|
|
151
|
+
# orig_exit(code)
|
|
152
|
+
# end
|
|
153
|
+
# end
|
|
154
|
+
# include Mod
|
|
155
|
+
# exit(99)
|
|
156
|
+
#
|
|
157
|
+
# *produces:*
|
|
158
|
+
#
|
|
159
|
+
# Exiting with code 99
|
|
160
|
+
#
|
|
161
|
+
def alias_method: (Symbol new_name, Symbol old_name) -> self
|
|
162
|
+
|
|
163
|
+
# Returns a list of modules included/prepended in *mod* (including *mod*
|
|
164
|
+
# itself).
|
|
165
|
+
#
|
|
166
|
+
# module Mod
|
|
167
|
+
# include Math
|
|
168
|
+
# include Comparable
|
|
169
|
+
# prepend Enumerable
|
|
170
|
+
# end
|
|
171
|
+
#
|
|
172
|
+
# Mod.ancestors #=> [Enumerable, Mod, Comparable, Math]
|
|
173
|
+
# Math.ancestors #=> [Math]
|
|
174
|
+
# Enumerable.ancestors #=> [Enumerable]
|
|
175
|
+
#
|
|
176
|
+
def ancestors: () -> ::Array[Module]
|
|
177
|
+
|
|
178
|
+
# When this module is included in another, Ruby calls #append_features in this
|
|
179
|
+
# module, passing it the receiving module in *mod*. Ruby's default
|
|
180
|
+
# implementation is to add the constants, methods, and module variables of this
|
|
181
|
+
# module to *mod* if this module has not already been added to *mod* or one of
|
|
182
|
+
# its ancestors. See also Module#include.
|
|
183
|
+
#
|
|
184
|
+
def append_features: (Module arg0) -> self
|
|
185
|
+
|
|
186
|
+
# Defines a named attribute for this module, where the name is
|
|
187
|
+
# *symbol.*`id2name`, creating an instance variable (`@name`) and a
|
|
188
|
+
# corresponding access method to read it. Also creates a method called `name=`
|
|
189
|
+
# to set the attribute. String arguments are converted to symbols.
|
|
190
|
+
#
|
|
191
|
+
# module Mod
|
|
192
|
+
# attr_accessor(:one, :two)
|
|
193
|
+
# end
|
|
194
|
+
# Mod.instance_methods.sort #=> [:one, :one=, :two, :two=]
|
|
195
|
+
#
|
|
196
|
+
def `attr_accessor`: (*Symbol | String arg0) -> NilClass
|
|
197
|
+
|
|
198
|
+
# Creates instance variables and corresponding methods that return the value of
|
|
199
|
+
# each instance variable. Equivalent to calling ```attr`*:name*'' on each name
|
|
200
|
+
# in turn. String arguments are converted to symbols.
|
|
201
|
+
#
|
|
202
|
+
def `attr_reader`: (*Symbol | String arg0) -> NilClass
|
|
203
|
+
|
|
204
|
+
# Creates an accessor method to allow assignment to the attribute
|
|
205
|
+
# *symbol*`.id2name`. String arguments are converted to symbols.
|
|
206
|
+
#
|
|
207
|
+
def `attr_writer`: (*Symbol | String arg0) -> NilClass
|
|
208
|
+
|
|
209
|
+
# Registers *filename* to be loaded (using Kernel::require) the first time that
|
|
210
|
+
# *module* (which may be a String or a symbol) is accessed in the namespace of
|
|
211
|
+
# *mod*.
|
|
212
|
+
#
|
|
213
|
+
# module A
|
|
214
|
+
# end
|
|
215
|
+
# A.autoload(:B, "b")
|
|
216
|
+
# A::B.doit # autoloads "b"
|
|
217
|
+
#
|
|
218
|
+
def autoload: (Symbol _module, String filename) -> NilClass
|
|
219
|
+
|
|
220
|
+
# Returns *filename* to be loaded if *name* is registered as `autoload` in the
|
|
221
|
+
# namespace of *mod* or one of its ancestors.
|
|
222
|
+
#
|
|
223
|
+
# module A
|
|
224
|
+
# end
|
|
225
|
+
# A.autoload(:B, "b")
|
|
226
|
+
# A.autoload?(:B) #=> "b"
|
|
227
|
+
#
|
|
228
|
+
# If `inherit` is false, the lookup only checks the autoloads in the receiver:
|
|
229
|
+
#
|
|
230
|
+
# class A
|
|
231
|
+
# autoload :CONST, "const.rb"
|
|
232
|
+
# end
|
|
233
|
+
#
|
|
234
|
+
# class B < A
|
|
235
|
+
# end
|
|
236
|
+
#
|
|
237
|
+
# B.autoload?(:CONST) #=> "const.rb", found in A (ancestor)
|
|
238
|
+
# B.autoload?(:CONST, false) #=> nil, not found in B itself
|
|
239
|
+
#
|
|
240
|
+
def autoload?: (Symbol name, ?bool inherit) -> String?
|
|
241
|
+
|
|
242
|
+
# Evaluates the string or block in the context of *mod*, except that when a
|
|
243
|
+
# block is given, constant/class variable lookup is not affected. This can be
|
|
244
|
+
# used to add methods to a class. `module_eval` returns the result of evaluating
|
|
245
|
+
# its argument. The optional *filename* and *lineno* parameters set the text for
|
|
246
|
+
# error messages.
|
|
247
|
+
#
|
|
248
|
+
# class Thing
|
|
249
|
+
# end
|
|
250
|
+
# a = %q{def hello() "Hello there!" end}
|
|
251
|
+
# Thing.module_eval(a)
|
|
252
|
+
# puts Thing.new.hello()
|
|
253
|
+
# Thing.module_eval("invalid code", "dummy", 123)
|
|
254
|
+
#
|
|
255
|
+
# *produces:*
|
|
256
|
+
#
|
|
257
|
+
# Hello there!
|
|
258
|
+
# dummy:123:in `module_eval': undefined local variable
|
|
259
|
+
# or method `code' for Thing:Class
|
|
260
|
+
#
|
|
261
|
+
def class_eval: (String arg0, ?String filename, ?Integer lineno) -> untyped
|
|
262
|
+
| [U] (untyped arg0) { (untyped m) -> U } -> U
|
|
263
|
+
|
|
264
|
+
# Evaluates the given block in the context of the class/module. The method
|
|
265
|
+
# defined in the block will belong to the receiver. Any arguments passed to the
|
|
266
|
+
# method will be passed to the block. This can be used if the block needs to
|
|
267
|
+
# access instance variables.
|
|
268
|
+
#
|
|
269
|
+
# class Thing
|
|
270
|
+
# end
|
|
271
|
+
# Thing.class_exec{
|
|
272
|
+
# def hello() "Hello there!" end
|
|
273
|
+
# }
|
|
274
|
+
# puts Thing.new.hello()
|
|
275
|
+
#
|
|
276
|
+
# *produces:*
|
|
277
|
+
#
|
|
278
|
+
# Hello there!
|
|
279
|
+
#
|
|
280
|
+
def class_exec: (*untyped args) { () -> untyped } -> untyped
|
|
281
|
+
|
|
282
|
+
# Returns `true` if the given class variable is defined in *obj*. String
|
|
283
|
+
# arguments are converted to symbols.
|
|
284
|
+
#
|
|
285
|
+
# class Fred
|
|
286
|
+
# @@foo = 99
|
|
287
|
+
# end
|
|
288
|
+
# Fred.class_variable_defined?(:@@foo) #=> true
|
|
289
|
+
# Fred.class_variable_defined?(:@@bar) #=> false
|
|
290
|
+
#
|
|
291
|
+
def class_variable_defined?: (Symbol | String arg0) -> bool
|
|
292
|
+
|
|
293
|
+
# Returns the value of the given class variable (or throws a NameError
|
|
294
|
+
# exception). The `@@` part of the variable name should be included for regular
|
|
295
|
+
# class variables. String arguments are converted to symbols.
|
|
296
|
+
#
|
|
297
|
+
# class Fred
|
|
298
|
+
# @@foo = 99
|
|
299
|
+
# end
|
|
300
|
+
# Fred.class_variable_get(:@@foo) #=> 99
|
|
301
|
+
#
|
|
302
|
+
def class_variable_get: (Symbol | String arg0) -> untyped
|
|
303
|
+
|
|
304
|
+
# Sets the class variable named by *symbol* to the given object. If the class
|
|
305
|
+
# variable name is passed as a string, that string is converted to a symbol.
|
|
306
|
+
#
|
|
307
|
+
# class Fred
|
|
308
|
+
# @@foo = 99
|
|
309
|
+
# def foo
|
|
310
|
+
# @@foo
|
|
311
|
+
# end
|
|
312
|
+
# end
|
|
313
|
+
# Fred.class_variable_set(:@@foo, 101) #=> 101
|
|
314
|
+
# Fred.new.foo #=> 101
|
|
315
|
+
#
|
|
316
|
+
def class_variable_set: (Symbol | String arg0, untyped arg1) -> untyped
|
|
317
|
+
|
|
318
|
+
# Returns an array of the names of class variables in *mod*. This includes the
|
|
319
|
+
# names of class variables in any included modules, unless the *inherit*
|
|
320
|
+
# parameter is set to `false`.
|
|
321
|
+
#
|
|
322
|
+
# class One
|
|
323
|
+
# @@var1 = 1
|
|
324
|
+
# end
|
|
325
|
+
# class Two < One
|
|
326
|
+
# @@var2 = 2
|
|
327
|
+
# end
|
|
328
|
+
# One.class_variables #=> [:@@var1]
|
|
329
|
+
# Two.class_variables #=> [:@@var2, :@@var1]
|
|
330
|
+
# Two.class_variables(false) #=> [:@@var2]
|
|
331
|
+
#
|
|
332
|
+
def class_variables: (?bool inherit) -> ::Array[Symbol]
|
|
333
|
+
|
|
334
|
+
# Says whether *mod* or its ancestors have a constant with the given name:
|
|
335
|
+
#
|
|
336
|
+
# Float.const_defined?(:EPSILON) #=> true, found in Float itself
|
|
337
|
+
# Float.const_defined?("String") #=> true, found in Object (ancestor)
|
|
338
|
+
# BasicObject.const_defined?(:Hash) #=> false
|
|
339
|
+
#
|
|
340
|
+
# If *mod* is a `Module`, additionally `Object` and its ancestors are checked:
|
|
341
|
+
#
|
|
342
|
+
# Math.const_defined?(:String) #=> true, found in Object
|
|
343
|
+
#
|
|
344
|
+
# In each of the checked classes or modules, if the constant is not present but
|
|
345
|
+
# there is an autoload for it, `true` is returned directly without autoloading:
|
|
346
|
+
#
|
|
347
|
+
# module Admin
|
|
348
|
+
# autoload :User, 'admin/user'
|
|
349
|
+
# end
|
|
350
|
+
# Admin.const_defined?(:User) #=> true
|
|
351
|
+
#
|
|
352
|
+
# If the constant is not found the callback `const_missing` is **not** called
|
|
353
|
+
# and the method returns `false`.
|
|
354
|
+
#
|
|
355
|
+
# If `inherit` is false, the lookup only checks the constants in the receiver:
|
|
356
|
+
#
|
|
357
|
+
# IO.const_defined?(:SYNC) #=> true, found in File::Constants (ancestor)
|
|
358
|
+
# IO.const_defined?(:SYNC, false) #=> false, not found in IO itself
|
|
359
|
+
#
|
|
360
|
+
# In this case, the same logic for autoloading applies.
|
|
361
|
+
#
|
|
362
|
+
# If the argument is not a valid constant name a `NameError` is raised with the
|
|
363
|
+
# message "wrong constant name *name*":
|
|
364
|
+
#
|
|
365
|
+
# Hash.const_defined? 'foobar' #=> NameError: wrong constant name foobar
|
|
366
|
+
#
|
|
367
|
+
def const_defined?: (Symbol | String arg0, ?bool inherit) -> bool
|
|
368
|
+
|
|
369
|
+
# Checks for a constant with the given name in *mod*. If `inherit` is set, the
|
|
370
|
+
# lookup will also search the ancestors (and `Object` if *mod* is a `Module`).
|
|
371
|
+
#
|
|
372
|
+
# The value of the constant is returned if a definition is found, otherwise a
|
|
373
|
+
# `NameError` is raised.
|
|
374
|
+
#
|
|
375
|
+
# Math.const_get(:PI) #=> 3.14159265358979
|
|
376
|
+
#
|
|
377
|
+
# This method will recursively look up constant names if a namespaced class name
|
|
378
|
+
# is provided. For example:
|
|
379
|
+
#
|
|
380
|
+
# module Foo; class Bar; end end
|
|
381
|
+
# Object.const_get 'Foo::Bar'
|
|
382
|
+
#
|
|
383
|
+
# The `inherit` flag is respected on each lookup. For example:
|
|
384
|
+
#
|
|
385
|
+
# module Foo
|
|
386
|
+
# class Bar
|
|
387
|
+
# VAL = 10
|
|
388
|
+
# end
|
|
389
|
+
#
|
|
390
|
+
# class Baz < Bar; end
|
|
391
|
+
# end
|
|
392
|
+
#
|
|
393
|
+
# Object.const_get 'Foo::Baz::VAL' # => 10
|
|
394
|
+
# Object.const_get 'Foo::Baz::VAL', false # => NameError
|
|
395
|
+
#
|
|
396
|
+
# If the argument is not a valid constant name a `NameError` will be raised with
|
|
397
|
+
# a warning "wrong constant name".
|
|
398
|
+
#
|
|
399
|
+
# Object.const_get 'foobar' #=> NameError: wrong constant name foobar
|
|
400
|
+
#
|
|
401
|
+
def const_get: (Symbol | String arg0, ?bool inherit) -> untyped
|
|
402
|
+
|
|
403
|
+
# Invoked when a reference is made to an undefined constant in *mod*. It is
|
|
404
|
+
# passed a symbol for the undefined constant, and returns a value to be used for
|
|
405
|
+
# that constant. The following code is an example of the same:
|
|
406
|
+
#
|
|
407
|
+
# def Foo.const_missing(name)
|
|
408
|
+
# name # return the constant name as Symbol
|
|
409
|
+
# end
|
|
410
|
+
#
|
|
411
|
+
# Foo::UNDEFINED_CONST #=> :UNDEFINED_CONST: symbol returned
|
|
412
|
+
#
|
|
413
|
+
# In the next example when a reference is made to an undefined constant, it
|
|
414
|
+
# attempts to load a file whose name is the lowercase version of the constant
|
|
415
|
+
# (thus class `Fred` is assumed to be in file `fred.rb`). If found, it returns
|
|
416
|
+
# the loaded class. It therefore implements an autoload feature similar to
|
|
417
|
+
# Kernel#autoload and Module#autoload.
|
|
418
|
+
#
|
|
419
|
+
# def Object.const_missing(name)
|
|
420
|
+
# @looked_for ||= {}
|
|
421
|
+
# str_name = name.to_s
|
|
422
|
+
# raise "Class not found: #{name}" if @looked_for[str_name]
|
|
423
|
+
# @looked_for[str_name] = 1
|
|
424
|
+
# file = str_name.downcase
|
|
425
|
+
# require file
|
|
426
|
+
# klass = const_get(name)
|
|
427
|
+
# return klass if klass
|
|
428
|
+
# raise "Class not found: #{name}"
|
|
429
|
+
# end
|
|
430
|
+
#
|
|
431
|
+
def const_missing: (Symbol arg0) -> untyped
|
|
432
|
+
|
|
433
|
+
# Sets the named constant to the given object, returning that object. Creates a
|
|
434
|
+
# new constant if no constant with the given name previously existed.
|
|
435
|
+
#
|
|
436
|
+
# Math.const_set("HIGH_SCHOOL_PI", 22.0/7.0) #=> 3.14285714285714
|
|
437
|
+
# Math::HIGH_SCHOOL_PI - Math::PI #=> 0.00126448926734968
|
|
438
|
+
#
|
|
439
|
+
# If `sym` or `str` is not a valid constant name a `NameError` will be raised
|
|
440
|
+
# with a warning "wrong constant name".
|
|
441
|
+
#
|
|
442
|
+
# Object.const_set('foobar', 42) #=> NameError: wrong constant name foobar
|
|
443
|
+
#
|
|
444
|
+
def const_set: (Symbol | String arg0, untyped arg1) -> untyped
|
|
445
|
+
|
|
446
|
+
# Returns an array of the names of the constants accessible in *mod*. This
|
|
447
|
+
# includes the names of constants in any included modules (example at start of
|
|
448
|
+
# section), unless the *inherit* parameter is set to `false`.
|
|
449
|
+
#
|
|
450
|
+
# The implementation makes no guarantees about the order in which the constants
|
|
451
|
+
# are yielded.
|
|
452
|
+
#
|
|
453
|
+
# IO.constants.include?(:SYNC) #=> true
|
|
454
|
+
# IO.constants(false).include?(:SYNC) #=> false
|
|
455
|
+
#
|
|
456
|
+
# Also see Module#const_defined?.
|
|
457
|
+
#
|
|
458
|
+
def constants: (?bool inherit) -> ::Array[Symbol]
|
|
459
|
+
|
|
460
|
+
# Defines an instance method in the receiver. The *method* parameter can be a
|
|
461
|
+
# `Proc`, a `Method` or an `UnboundMethod` object. If a block is specified, it
|
|
462
|
+
# is used as the method body. If a block or the *method* parameter has
|
|
463
|
+
# parameters, they're used as method parameters. This block is evaluated using
|
|
464
|
+
# #instance_eval.
|
|
465
|
+
#
|
|
466
|
+
# class A
|
|
467
|
+
# def fred
|
|
468
|
+
# puts "In Fred"
|
|
469
|
+
# end
|
|
470
|
+
# def create_method(name, &block)
|
|
471
|
+
# self.class.define_method(name, &block)
|
|
472
|
+
# end
|
|
473
|
+
# define_method(:wilma) { puts "Charge it!" }
|
|
474
|
+
# define_method(:flint) {|name| puts "I'm #{name}!"}
|
|
475
|
+
# end
|
|
476
|
+
# class B < A
|
|
477
|
+
# define_method(:barney, instance_method(:fred))
|
|
478
|
+
# end
|
|
479
|
+
# a = B.new
|
|
480
|
+
# a.barney
|
|
481
|
+
# a.wilma
|
|
482
|
+
# a.flint('Dino')
|
|
483
|
+
# a.create_method(:betty) { p self }
|
|
484
|
+
# a.betty
|
|
485
|
+
#
|
|
486
|
+
# *produces:*
|
|
487
|
+
#
|
|
488
|
+
# In Fred
|
|
489
|
+
# Charge it!
|
|
490
|
+
# I'm Dino!
|
|
491
|
+
# #<B:0x401b39e8>
|
|
492
|
+
#
|
|
493
|
+
def define_method: (Symbol | String arg0, ?Proc | Method | UnboundMethod arg1) -> Symbol
|
|
494
|
+
| (Symbol | String arg0) { () -> untyped } -> Symbol
|
|
495
|
+
|
|
496
|
+
def eql?: (untyped other) -> bool
|
|
497
|
+
|
|
498
|
+
def equal?: (untyped other) -> bool
|
|
499
|
+
|
|
500
|
+
# Extends the specified object by adding this module's constants and methods
|
|
501
|
+
# (which are added as singleton methods). This is the callback method used by
|
|
502
|
+
# Object#extend.
|
|
503
|
+
#
|
|
504
|
+
# module Picky
|
|
505
|
+
# def Picky.extend_object(o)
|
|
506
|
+
# if String === o
|
|
507
|
+
# puts "Can't add Picky to a String"
|
|
508
|
+
# else
|
|
509
|
+
# puts "Picky added to #{o.class}"
|
|
510
|
+
# super
|
|
511
|
+
# end
|
|
512
|
+
# end
|
|
513
|
+
# end
|
|
514
|
+
# (s = Array.new).extend Picky # Call Object.extend
|
|
515
|
+
# (s = "quick brown fox").extend Picky
|
|
516
|
+
#
|
|
517
|
+
# *produces:*
|
|
518
|
+
#
|
|
519
|
+
# Picky added to Array
|
|
520
|
+
# Can't add Picky to a String
|
|
521
|
+
#
|
|
522
|
+
def extend_object: (untyped arg0) -> untyped
|
|
523
|
+
|
|
524
|
+
# The equivalent of `included`, but for extended modules.
|
|
525
|
+
#
|
|
526
|
+
# module A
|
|
527
|
+
# def self.extended(mod)
|
|
528
|
+
# puts "#{self} extended in #{mod}"
|
|
529
|
+
# end
|
|
530
|
+
# end
|
|
531
|
+
# module Enumerable
|
|
532
|
+
# extend A
|
|
533
|
+
# end
|
|
534
|
+
# # => prints "A extended in Enumerable"
|
|
535
|
+
#
|
|
536
|
+
def extended: (Module othermod) -> untyped
|
|
537
|
+
|
|
538
|
+
# Prevents further modifications to *mod*.
|
|
539
|
+
#
|
|
540
|
+
# This method returns self.
|
|
541
|
+
#
|
|
542
|
+
def freeze: () -> self
|
|
543
|
+
|
|
544
|
+
# Invokes Module.append_features on each parameter in reverse order.
|
|
545
|
+
#
|
|
546
|
+
def `include`: (*Module arg0) -> self
|
|
547
|
+
|
|
548
|
+
# Returns `true` if *module* is included in *mod* or one of *mod*'s ancestors.
|
|
549
|
+
#
|
|
550
|
+
# module A
|
|
551
|
+
# end
|
|
552
|
+
# class B
|
|
553
|
+
# include A
|
|
554
|
+
# end
|
|
555
|
+
# class C < B
|
|
556
|
+
# end
|
|
557
|
+
# B.include?(A) #=> true
|
|
558
|
+
# C.include?(A) #=> true
|
|
559
|
+
# A.include?(A) #=> false
|
|
560
|
+
#
|
|
561
|
+
def include?: (Module arg0) -> bool
|
|
562
|
+
|
|
563
|
+
# Callback invoked whenever the receiver is included in another module or class.
|
|
564
|
+
# This should be used in preference to `Module.append_features` if your code
|
|
565
|
+
# wants to perform some action when a module is included in another.
|
|
566
|
+
#
|
|
567
|
+
# module A
|
|
568
|
+
# def A.included(mod)
|
|
569
|
+
# puts "#{self} included in #{mod}"
|
|
570
|
+
# end
|
|
571
|
+
# end
|
|
572
|
+
# module Enumerable
|
|
573
|
+
# include A
|
|
574
|
+
# end
|
|
575
|
+
# # => prints "A included in Enumerable"
|
|
576
|
+
#
|
|
577
|
+
def included: (Module othermod) -> untyped
|
|
578
|
+
|
|
579
|
+
# Returns the list of modules included in *mod*.
|
|
580
|
+
#
|
|
581
|
+
# module Mixin
|
|
582
|
+
# end
|
|
583
|
+
#
|
|
584
|
+
# module Outer
|
|
585
|
+
# include Mixin
|
|
586
|
+
# end
|
|
587
|
+
#
|
|
588
|
+
# Mixin.included_modules #=> []
|
|
589
|
+
# Outer.included_modules #=> [Mixin]
|
|
590
|
+
#
|
|
591
|
+
def included_modules: () -> ::Array[Module]
|
|
592
|
+
|
|
593
|
+
# Creates a new anonymous module. If a block is given, it is passed the module
|
|
594
|
+
# object, and the block is evaluated in the context of this module like
|
|
595
|
+
# #module_eval.
|
|
596
|
+
#
|
|
597
|
+
# fred = Module.new do
|
|
598
|
+
# def meth1
|
|
599
|
+
# "hello"
|
|
600
|
+
# end
|
|
601
|
+
# def meth2
|
|
602
|
+
# "bye"
|
|
603
|
+
# end
|
|
604
|
+
# end
|
|
605
|
+
# a = "my string"
|
|
606
|
+
# a.extend(fred) #=> "my string"
|
|
607
|
+
# a.meth1 #=> "hello"
|
|
608
|
+
# a.meth2 #=> "bye"
|
|
609
|
+
#
|
|
610
|
+
# Assign the module to a constant (name starting uppercase) if you want to treat
|
|
611
|
+
# it like a regular module.
|
|
612
|
+
#
|
|
613
|
+
def initialize: () -> Object
|
|
614
|
+
| () { (Module arg0) -> untyped } -> void
|
|
615
|
+
|
|
616
|
+
# Returns an `UnboundMethod` representing the given instance method in *mod*.
|
|
617
|
+
#
|
|
618
|
+
# class Interpreter
|
|
619
|
+
# def do_a() print "there, "; end
|
|
620
|
+
# def do_d() print "Hello "; end
|
|
621
|
+
# def do_e() print "!\n"; end
|
|
622
|
+
# def do_v() print "Dave"; end
|
|
623
|
+
# Dispatcher = {
|
|
624
|
+
# "a" => instance_method(:do_a),
|
|
625
|
+
# "d" => instance_method(:do_d),
|
|
626
|
+
# "e" => instance_method(:do_e),
|
|
627
|
+
# "v" => instance_method(:do_v)
|
|
628
|
+
# }
|
|
629
|
+
# def interpret(string)
|
|
630
|
+
# string.each_char {|b| Dispatcher[b].bind(self).call }
|
|
631
|
+
# end
|
|
632
|
+
# end
|
|
633
|
+
#
|
|
634
|
+
# interpreter = Interpreter.new
|
|
635
|
+
# interpreter.interpret('dave')
|
|
636
|
+
#
|
|
637
|
+
# *produces:*
|
|
638
|
+
#
|
|
639
|
+
# Hello there, Dave!
|
|
640
|
+
#
|
|
641
|
+
def instance_method: (Symbol arg0) -> UnboundMethod
|
|
642
|
+
|
|
643
|
+
# Returns an array containing the names of the public and protected instance
|
|
644
|
+
# methods in the receiver. For a module, these are the public and protected
|
|
645
|
+
# methods; for a class, they are the instance (not singleton) methods. If the
|
|
646
|
+
# optional parameter is `false`, the methods of any ancestors are not included.
|
|
647
|
+
#
|
|
648
|
+
# module A
|
|
649
|
+
# def method1() end
|
|
650
|
+
# end
|
|
651
|
+
# class B
|
|
652
|
+
# include A
|
|
653
|
+
# def method2() end
|
|
654
|
+
# end
|
|
655
|
+
# class C < B
|
|
656
|
+
# def method3() end
|
|
657
|
+
# end
|
|
658
|
+
#
|
|
659
|
+
# A.instance_methods(false) #=> [:method1]
|
|
660
|
+
# B.instance_methods(false) #=> [:method2]
|
|
661
|
+
# B.instance_methods(true).include?(:method1) #=> true
|
|
662
|
+
# C.instance_methods(false) #=> [:method3]
|
|
663
|
+
# C.instance_methods.include?(:method2) #=> true
|
|
664
|
+
#
|
|
665
|
+
def instance_methods: (?bool include_super) -> ::Array[Symbol]
|
|
666
|
+
|
|
667
|
+
# Invoked as a callback whenever an instance method is added to the receiver.
|
|
668
|
+
#
|
|
669
|
+
# module Chatty
|
|
670
|
+
# def self.method_added(method_name)
|
|
671
|
+
# puts "Adding #{method_name.inspect}"
|
|
672
|
+
# end
|
|
673
|
+
# def self.some_class_method() end
|
|
674
|
+
# def some_instance_method() end
|
|
675
|
+
# end
|
|
676
|
+
#
|
|
677
|
+
# *produces:*
|
|
678
|
+
#
|
|
679
|
+
# Adding :some_instance_method
|
|
680
|
+
#
|
|
681
|
+
def method_added: (Symbol meth) -> untyped
|
|
682
|
+
|
|
683
|
+
# Returns `true` if the named method is defined by *mod*. If *inherit* is set,
|
|
684
|
+
# the lookup will also search *mod*'s ancestors. Public and protected methods
|
|
685
|
+
# are matched. String arguments are converted to symbols.
|
|
686
|
+
#
|
|
687
|
+
# module A
|
|
688
|
+
# def method1() end
|
|
689
|
+
# def protected_method1() end
|
|
690
|
+
# protected :protected_method1
|
|
691
|
+
# end
|
|
692
|
+
# class B
|
|
693
|
+
# def method2() end
|
|
694
|
+
# def private_method2() end
|
|
695
|
+
# private :private_method2
|
|
696
|
+
# end
|
|
697
|
+
# class C < B
|
|
698
|
+
# include A
|
|
699
|
+
# def method3() end
|
|
700
|
+
# end
|
|
701
|
+
#
|
|
702
|
+
# A.method_defined? :method1 #=> true
|
|
703
|
+
# C.method_defined? "method1" #=> true
|
|
704
|
+
# C.method_defined? "method2" #=> true
|
|
705
|
+
# C.method_defined? "method2", true #=> true
|
|
706
|
+
# C.method_defined? "method2", false #=> false
|
|
707
|
+
# C.method_defined? "method3" #=> true
|
|
708
|
+
# C.method_defined? "protected_method1" #=> true
|
|
709
|
+
# C.method_defined? "method4" #=> false
|
|
710
|
+
# C.method_defined? "private_method2" #=> false
|
|
711
|
+
#
|
|
712
|
+
def method_defined?: (Symbol | String arg0, ?bool inherit) -> bool
|
|
713
|
+
|
|
714
|
+
# Invoked as a callback whenever an instance method is removed from the
|
|
715
|
+
# receiver.
|
|
716
|
+
#
|
|
717
|
+
# module Chatty
|
|
718
|
+
# def self.method_removed(method_name)
|
|
719
|
+
# puts "Removing #{method_name.inspect}"
|
|
720
|
+
# end
|
|
721
|
+
# def self.some_class_method() end
|
|
722
|
+
# def some_instance_method() end
|
|
723
|
+
# class << self
|
|
724
|
+
# remove_method :some_class_method
|
|
725
|
+
# end
|
|
726
|
+
# remove_method :some_instance_method
|
|
727
|
+
# end
|
|
728
|
+
#
|
|
729
|
+
# *produces:*
|
|
730
|
+
#
|
|
731
|
+
# Removing :some_instance_method
|
|
732
|
+
#
|
|
733
|
+
def method_removed: (Symbol method_name) -> untyped
|
|
734
|
+
|
|
735
|
+
# Evaluates the string or block in the context of *mod*, except that when a
|
|
736
|
+
# block is given, constant/class variable lookup is not affected. This can be
|
|
737
|
+
# used to add methods to a class. `module_eval` returns the result of evaluating
|
|
738
|
+
# its argument. The optional *filename* and *lineno* parameters set the text for
|
|
739
|
+
# error messages.
|
|
740
|
+
#
|
|
741
|
+
# class Thing
|
|
742
|
+
# end
|
|
743
|
+
# a = %q{def hello() "Hello there!" end}
|
|
744
|
+
# Thing.module_eval(a)
|
|
745
|
+
# puts Thing.new.hello()
|
|
746
|
+
# Thing.module_eval("invalid code", "dummy", 123)
|
|
747
|
+
#
|
|
748
|
+
# *produces:*
|
|
749
|
+
#
|
|
750
|
+
# Hello there!
|
|
751
|
+
# dummy:123:in `module_eval': undefined local variable
|
|
752
|
+
# or method `code' for Thing:Class
|
|
753
|
+
#
|
|
754
|
+
def module_eval: (String arg0, ?String filename, ?Integer lineno) -> untyped
|
|
755
|
+
| [U] (untyped arg0) { (untyped m) -> U } -> U
|
|
756
|
+
|
|
757
|
+
# Evaluates the given block in the context of the class/module. The method
|
|
758
|
+
# defined in the block will belong to the receiver. Any arguments passed to the
|
|
759
|
+
# method will be passed to the block. This can be used if the block needs to
|
|
760
|
+
# access instance variables.
|
|
761
|
+
#
|
|
762
|
+
# class Thing
|
|
763
|
+
# end
|
|
764
|
+
# Thing.class_exec{
|
|
765
|
+
# def hello() "Hello there!" end
|
|
766
|
+
# }
|
|
767
|
+
# puts Thing.new.hello()
|
|
768
|
+
#
|
|
769
|
+
# *produces:*
|
|
770
|
+
#
|
|
771
|
+
# Hello there!
|
|
772
|
+
#
|
|
773
|
+
def module_exec: (*untyped args) { () -> untyped } -> untyped
|
|
774
|
+
|
|
775
|
+
# Creates module functions for the named methods. These functions may be called
|
|
776
|
+
# with the module as a receiver, and also become available as instance methods
|
|
777
|
+
# to classes that mix in the module. Module functions are copies of the
|
|
778
|
+
# original, and so may be changed independently. The instance-method versions
|
|
779
|
+
# are made private. If used with no arguments, subsequently defined methods
|
|
780
|
+
# become module functions. String arguments are converted to symbols.
|
|
781
|
+
#
|
|
782
|
+
# module Mod
|
|
783
|
+
# def one
|
|
784
|
+
# "This is one"
|
|
785
|
+
# end
|
|
786
|
+
# module_function :one
|
|
787
|
+
# end
|
|
788
|
+
# class Cls
|
|
789
|
+
# include Mod
|
|
790
|
+
# def call_one
|
|
791
|
+
# one
|
|
792
|
+
# end
|
|
793
|
+
# end
|
|
794
|
+
# Mod.one #=> "This is one"
|
|
795
|
+
# c = Cls.new
|
|
796
|
+
# c.call_one #=> "This is one"
|
|
797
|
+
# module Mod
|
|
798
|
+
# def one
|
|
799
|
+
# "This is the new one"
|
|
800
|
+
# end
|
|
801
|
+
# end
|
|
802
|
+
# Mod.one #=> "This is one"
|
|
803
|
+
# c.call_one #=> "This is the new one"
|
|
804
|
+
#
|
|
805
|
+
def module_function: (*Symbol | String arg0) -> self
|
|
806
|
+
|
|
807
|
+
# Returns the name of the module *mod*. Returns nil for anonymous modules.
|
|
808
|
+
#
|
|
809
|
+
def name: () -> String?
|
|
810
|
+
|
|
811
|
+
# Invokes Module.prepend_features on each parameter in reverse order.
|
|
812
|
+
#
|
|
813
|
+
def `prepend`: (*Module arg0) -> self
|
|
814
|
+
|
|
815
|
+
# When this module is prepended in another, Ruby calls #prepend_features in this
|
|
816
|
+
# module, passing it the receiving module in *mod*. Ruby's default
|
|
817
|
+
# implementation is to overlay the constants, methods, and module variables of
|
|
818
|
+
# this module to *mod* if this module has not already been added to *mod* or one
|
|
819
|
+
# of its ancestors. See also Module#prepend.
|
|
820
|
+
#
|
|
821
|
+
def prepend_features: (Module arg0) -> self
|
|
822
|
+
|
|
823
|
+
# The equivalent of `included`, but for prepended modules.
|
|
824
|
+
#
|
|
825
|
+
# module A
|
|
826
|
+
# def self.prepended(mod)
|
|
827
|
+
# puts "#{self} prepended to #{mod}"
|
|
828
|
+
# end
|
|
829
|
+
# end
|
|
830
|
+
# module Enumerable
|
|
831
|
+
# prepend A
|
|
832
|
+
# end
|
|
833
|
+
# # => prints "A prepended to Enumerable"
|
|
834
|
+
#
|
|
835
|
+
def prepended: (Module othermod) -> untyped
|
|
836
|
+
|
|
837
|
+
# With no arguments, sets the default visibility for subsequently defined
|
|
838
|
+
# methods to private. With arguments, sets the named methods to have private
|
|
839
|
+
# visibility. String arguments are converted to symbols.
|
|
840
|
+
#
|
|
841
|
+
# module Mod
|
|
842
|
+
# def a() end
|
|
843
|
+
# def b() end
|
|
844
|
+
# private
|
|
845
|
+
# def c() end
|
|
846
|
+
# private :a
|
|
847
|
+
# end
|
|
848
|
+
# Mod.private_instance_methods #=> [:a, :c]
|
|
849
|
+
#
|
|
850
|
+
# Note that to show a private method on RDoc, use `:doc:`.
|
|
851
|
+
#
|
|
852
|
+
def `private`: (*Symbol | String arg0) -> self
|
|
853
|
+
|
|
854
|
+
# Makes existing class methods private. Often used to hide the default
|
|
855
|
+
# constructor `new`.
|
|
856
|
+
#
|
|
857
|
+
# String arguments are converted to symbols.
|
|
858
|
+
#
|
|
859
|
+
# class SimpleSingleton # Not thread safe
|
|
860
|
+
# private_class_method :new
|
|
861
|
+
# def SimpleSingleton.create(*args, &block)
|
|
862
|
+
# @me = new(*args, &block) if ! @me
|
|
863
|
+
# @me
|
|
864
|
+
# end
|
|
865
|
+
# end
|
|
866
|
+
#
|
|
867
|
+
def private_class_method: (*Symbol | String arg0) -> self
|
|
868
|
+
|
|
869
|
+
# Makes a list of existing constants private.
|
|
870
|
+
#
|
|
871
|
+
def private_constant: (*Symbol arg0) -> self
|
|
872
|
+
|
|
873
|
+
# Returns a list of the private instance methods defined in *mod*. If the
|
|
874
|
+
# optional parameter is `false`, the methods of any ancestors are not included.
|
|
875
|
+
#
|
|
876
|
+
# module Mod
|
|
877
|
+
# def method1() end
|
|
878
|
+
# private :method1
|
|
879
|
+
# def method2() end
|
|
880
|
+
# end
|
|
881
|
+
# Mod.instance_methods #=> [:method2]
|
|
882
|
+
# Mod.private_instance_methods #=> [:method1]
|
|
883
|
+
#
|
|
884
|
+
def private_instance_methods: (?bool include_super) -> ::Array[Symbol]
|
|
885
|
+
|
|
886
|
+
# Returns `true` if the named private method is defined by *mod*. If *inherit*
|
|
887
|
+
# is set, the lookup will also search *mod*'s ancestors. String arguments are
|
|
888
|
+
# converted to symbols.
|
|
889
|
+
#
|
|
890
|
+
# module A
|
|
891
|
+
# def method1() end
|
|
892
|
+
# end
|
|
893
|
+
# class B
|
|
894
|
+
# private
|
|
895
|
+
# def method2() end
|
|
896
|
+
# end
|
|
897
|
+
# class C < B
|
|
898
|
+
# include A
|
|
899
|
+
# def method3() end
|
|
900
|
+
# end
|
|
901
|
+
#
|
|
902
|
+
# A.method_defined? :method1 #=> true
|
|
903
|
+
# C.private_method_defined? "method1" #=> false
|
|
904
|
+
# C.private_method_defined? "method2" #=> true
|
|
905
|
+
# C.private_method_defined? "method2", true #=> true
|
|
906
|
+
# C.private_method_defined? "method2", false #=> false
|
|
907
|
+
# C.method_defined? "method2" #=> false
|
|
908
|
+
#
|
|
909
|
+
def private_method_defined?: (Symbol | String arg0, ?bool inherit) -> bool
|
|
910
|
+
|
|
911
|
+
# With no arguments, sets the default visibility for subsequently defined
|
|
912
|
+
# methods to protected. With arguments, sets the named methods to have protected
|
|
913
|
+
# visibility. String arguments are converted to symbols.
|
|
914
|
+
#
|
|
915
|
+
# If a method has protected visibility, it is callable only where `self` of the
|
|
916
|
+
# context is the same as the method. (method definition or instance_eval). This
|
|
917
|
+
# behavior is different from Java's protected method. Usually `private` should
|
|
918
|
+
# be used.
|
|
919
|
+
#
|
|
920
|
+
# Note that a protected method is slow because it can't use inline cache.
|
|
921
|
+
#
|
|
922
|
+
# To show a private method on RDoc, use `:doc:` instead of this.
|
|
923
|
+
#
|
|
924
|
+
def protected: (*Symbol | String arg0) -> self
|
|
925
|
+
|
|
926
|
+
# Returns a list of the protected instance methods defined in *mod*. If the
|
|
927
|
+
# optional parameter is `false`, the methods of any ancestors are not included.
|
|
928
|
+
#
|
|
929
|
+
def protected_instance_methods: (?bool include_super) -> ::Array[Symbol]
|
|
930
|
+
|
|
931
|
+
# Returns `true` if the named protected method is defined *mod*. If *inherit*
|
|
932
|
+
# is set, the lookup will also search *mod*'s ancestors. String arguments are
|
|
933
|
+
# converted to symbols.
|
|
934
|
+
#
|
|
935
|
+
# module A
|
|
936
|
+
# def method1() end
|
|
937
|
+
# end
|
|
938
|
+
# class B
|
|
939
|
+
# protected
|
|
940
|
+
# def method2() end
|
|
941
|
+
# end
|
|
942
|
+
# class C < B
|
|
943
|
+
# include A
|
|
944
|
+
# def method3() end
|
|
945
|
+
# end
|
|
946
|
+
#
|
|
947
|
+
# A.method_defined? :method1 #=> true
|
|
948
|
+
# C.protected_method_defined? "method1" #=> false
|
|
949
|
+
# C.protected_method_defined? "method2" #=> true
|
|
950
|
+
# C.protected_method_defined? "method2", true #=> true
|
|
951
|
+
# C.protected_method_defined? "method2", false #=> false
|
|
952
|
+
# C.method_defined? "method2" #=> true
|
|
953
|
+
#
|
|
954
|
+
def protected_method_defined?: (Symbol | String arg0, ?bool inherit) -> bool
|
|
955
|
+
|
|
956
|
+
# With no arguments, sets the default visibility for subsequently defined
|
|
957
|
+
# methods to public. With arguments, sets the named methods to have public
|
|
958
|
+
# visibility. String arguments are converted to symbols.
|
|
959
|
+
#
|
|
960
|
+
def `public`: (*Symbol | String arg0) -> self
|
|
961
|
+
|
|
962
|
+
# Makes a list of existing class methods public.
|
|
963
|
+
#
|
|
964
|
+
# String arguments are converted to symbols.
|
|
965
|
+
#
|
|
966
|
+
def public_class_method: (*Symbol | String arg0) -> self
|
|
967
|
+
|
|
968
|
+
# Makes a list of existing constants public.
|
|
969
|
+
#
|
|
970
|
+
def public_constant: (*Symbol arg0) -> self
|
|
971
|
+
|
|
972
|
+
# Similar to *instance_method*, searches public method only.
|
|
973
|
+
#
|
|
974
|
+
def public_instance_method: (Symbol arg0) -> UnboundMethod
|
|
975
|
+
|
|
976
|
+
# Returns a list of the public instance methods defined in *mod*. If the
|
|
977
|
+
# optional parameter is `false`, the methods of any ancestors are not included.
|
|
978
|
+
#
|
|
979
|
+
def public_instance_methods: (?bool include_super) -> ::Array[Symbol]
|
|
980
|
+
|
|
981
|
+
# Returns `true` if the named public method is defined by *mod*. If *inherit*
|
|
982
|
+
# is set, the lookup will also search *mod*'s ancestors. String arguments are
|
|
983
|
+
# converted to symbols.
|
|
984
|
+
#
|
|
985
|
+
# module A
|
|
986
|
+
# def method1() end
|
|
987
|
+
# end
|
|
988
|
+
# class B
|
|
989
|
+
# protected
|
|
990
|
+
# def method2() end
|
|
991
|
+
# end
|
|
992
|
+
# class C < B
|
|
993
|
+
# include A
|
|
994
|
+
# def method3() end
|
|
995
|
+
# end
|
|
996
|
+
#
|
|
997
|
+
# A.method_defined? :method1 #=> true
|
|
998
|
+
# C.public_method_defined? "method1" #=> true
|
|
999
|
+
# C.public_method_defined? "method1", true #=> true
|
|
1000
|
+
# C.public_method_defined? "method1", false #=> true
|
|
1001
|
+
# C.public_method_defined? "method2" #=> false
|
|
1002
|
+
# C.method_defined? "method2" #=> true
|
|
1003
|
+
#
|
|
1004
|
+
def public_method_defined?: (Symbol | String arg0, ?bool inherit) -> bool
|
|
1005
|
+
|
|
1006
|
+
# Refine *mod* in the receiver.
|
|
1007
|
+
#
|
|
1008
|
+
# Returns a module, where refined methods are defined.
|
|
1009
|
+
#
|
|
1010
|
+
def refine: (Class arg0) { (untyped arg0) -> untyped } -> self
|
|
1011
|
+
|
|
1012
|
+
# Removes the definition of the *sym*, returning that constant's value.
|
|
1013
|
+
#
|
|
1014
|
+
# class Dummy
|
|
1015
|
+
# @@var = 99
|
|
1016
|
+
# puts @@var
|
|
1017
|
+
# remove_class_variable(:@@var)
|
|
1018
|
+
# p(defined? @@var)
|
|
1019
|
+
# end
|
|
1020
|
+
#
|
|
1021
|
+
# *produces:*
|
|
1022
|
+
#
|
|
1023
|
+
# 99
|
|
1024
|
+
# nil
|
|
1025
|
+
#
|
|
1026
|
+
def remove_class_variable: (Symbol arg0) -> untyped
|
|
1027
|
+
|
|
1028
|
+
# Removes the definition of the given constant, returning that constant's
|
|
1029
|
+
# previous value. If that constant referred to a module, this will not change
|
|
1030
|
+
# that module's name and can lead to confusion.
|
|
1031
|
+
#
|
|
1032
|
+
def remove_const: (Symbol arg0) -> untyped
|
|
1033
|
+
|
|
1034
|
+
# Removes the method identified by *symbol* from the current class. For an
|
|
1035
|
+
# example, see Module#undef_method. String arguments are converted to symbols.
|
|
1036
|
+
#
|
|
1037
|
+
def remove_method: (*Symbol | String arg0) -> self
|
|
1038
|
+
|
|
1039
|
+
# Returns `true` if *mod* is a singleton class or `false` if it is an ordinary
|
|
1040
|
+
# class or module.
|
|
1041
|
+
#
|
|
1042
|
+
# class C
|
|
1043
|
+
# end
|
|
1044
|
+
# C.singleton_class? #=> false
|
|
1045
|
+
# C.singleton_class.singleton_class? #=> true
|
|
1046
|
+
#
|
|
1047
|
+
def singleton_class?: () -> bool
|
|
1048
|
+
|
|
1049
|
+
# Returns a string representing this module or class. For basic classes and
|
|
1050
|
+
# modules, this is the name. For singletons, we show information on the thing
|
|
1051
|
+
# we're attached to as well.
|
|
1052
|
+
#
|
|
1053
|
+
def to_s: () -> String
|
|
1054
|
+
|
|
1055
|
+
# Prevents the current class from responding to calls to the named method.
|
|
1056
|
+
# Contrast this with `remove_method`, which deletes the method from the
|
|
1057
|
+
# particular class; Ruby will still search superclasses and mixed-in modules for
|
|
1058
|
+
# a possible receiver. String arguments are converted to symbols.
|
|
1059
|
+
#
|
|
1060
|
+
# class Parent
|
|
1061
|
+
# def hello
|
|
1062
|
+
# puts "In parent"
|
|
1063
|
+
# end
|
|
1064
|
+
# end
|
|
1065
|
+
# class Child < Parent
|
|
1066
|
+
# def hello
|
|
1067
|
+
# puts "In child"
|
|
1068
|
+
# end
|
|
1069
|
+
# end
|
|
1070
|
+
#
|
|
1071
|
+
# c = Child.new
|
|
1072
|
+
# c.hello
|
|
1073
|
+
#
|
|
1074
|
+
# class Child
|
|
1075
|
+
# remove_method :hello # remove from child, still in parent
|
|
1076
|
+
# end
|
|
1077
|
+
# c.hello
|
|
1078
|
+
#
|
|
1079
|
+
# class Child
|
|
1080
|
+
# undef_method :hello # prevent any calls to 'hello'
|
|
1081
|
+
# end
|
|
1082
|
+
# c.hello
|
|
1083
|
+
#
|
|
1084
|
+
# *produces:*
|
|
1085
|
+
#
|
|
1086
|
+
# In child
|
|
1087
|
+
# In parent
|
|
1088
|
+
# prog.rb:23: undefined method `hello' for #<Child:0x401b3bb4> (NoMethodError)
|
|
1089
|
+
#
|
|
1090
|
+
def undef_method: (*Symbol | String arg0) -> self
|
|
1091
|
+
|
|
1092
|
+
# Import class refinements from *module* into the current class or module
|
|
1093
|
+
# definition.
|
|
1094
|
+
#
|
|
1095
|
+
def using: (Module arg0) -> self
|
|
1096
|
+
|
|
1097
|
+
def inspect: () -> String
|
|
1098
|
+
|
|
1099
|
+
# The first form is equivalent to #attr_reader. The second form is equivalent to
|
|
1100
|
+
# `attr_accessor(name)` but deprecated. The last form is equivalent to
|
|
1101
|
+
# `attr_reader(name)` but deprecated.
|
|
1102
|
+
#
|
|
1103
|
+
def attr: (*Symbol | String arg0) -> NilClass
|
|
1104
|
+
end
|