rbs 0.14.0 → 0.18.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +8 -4
- data/CHANGELOG.md +35 -0
- data/Gemfile +1 -0
- data/README.md +1 -1
- data/Rakefile +19 -1
- data/Steepfile +0 -1
- data/bin/test_runner.rb +15 -1
- data/{stdlib/builtin → core}/array.rbs +124 -120
- data/{stdlib/builtin → core}/basic_object.rbs +0 -0
- data/{stdlib/builtin → core}/binding.rbs +0 -0
- data/core/builtin.rbs +70 -0
- data/{stdlib/builtin → core}/class.rbs +0 -0
- data/{stdlib/builtin → core}/comparable.rbs +0 -0
- data/{stdlib/builtin → core}/complex.rbs +0 -0
- data/{stdlib/builtin → core}/constants.rbs +0 -0
- data/{stdlib/builtin → core}/data.rbs +0 -0
- data/{stdlib/builtin → core}/deprecated.rbs +0 -0
- data/{stdlib/builtin → core}/dir.rbs +1 -1
- data/{stdlib/builtin → core}/encoding.rbs +0 -0
- data/{stdlib/builtin → core}/enumerable.rbs +67 -60
- data/{stdlib/builtin → core}/enumerator.rbs +5 -5
- data/{stdlib/builtin → core}/errno.rbs +0 -0
- data/{stdlib/builtin → core}/errors.rbs +1 -1
- data/{stdlib/builtin → core}/exception.rbs +0 -0
- data/{stdlib/builtin → core}/false_class.rbs +0 -0
- data/{stdlib/builtin → core}/fiber.rbs +0 -0
- data/{stdlib/builtin → core}/fiber_error.rbs +0 -0
- data/{stdlib/builtin → core}/file.rbs +0 -0
- data/{stdlib/builtin → core}/file_test.rbs +0 -0
- data/{stdlib/builtin → core}/float.rbs +0 -0
- data/{stdlib/builtin → core}/gc.rbs +2 -2
- data/{stdlib/builtin → core}/hash.rbs +15 -15
- data/{stdlib/builtin → core}/integer.rbs +0 -0
- data/{stdlib/builtin → core}/io.rbs +6 -6
- data/{stdlib/builtin → core}/kernel.rbs +1 -85
- data/{stdlib/builtin → core}/marshal.rbs +0 -0
- data/{stdlib/builtin → core}/match_data.rbs +0 -0
- data/{stdlib/builtin → core}/math.rbs +0 -0
- data/{stdlib/builtin → core}/method.rbs +0 -0
- data/{stdlib/builtin → core}/module.rbs +13 -13
- data/{stdlib/builtin → core}/nil_class.rbs +0 -0
- data/{stdlib/builtin → core}/numeric.rbs +0 -0
- data/{stdlib/builtin → core}/object.rbs +1 -1
- data/core/object_space.rbs +98 -0
- data/{stdlib/builtin → core}/proc.rbs +0 -0
- data/{stdlib/builtin → core}/process.rbs +0 -0
- data/{stdlib/builtin → core}/random.rbs +1 -1
- data/{stdlib/builtin → core}/range.rbs +3 -3
- data/{stdlib/builtin → core}/rational.rbs +0 -0
- data/{stdlib/builtin → core}/rb_config.rbs +0 -0
- data/{stdlib/builtin → core}/regexp.rbs +0 -0
- data/{stdlib/builtin → core}/ruby_vm.rbs +0 -0
- data/{stdlib/builtin → core}/signal.rbs +0 -0
- data/{stdlib/builtin → core}/string.rbs +6 -6
- data/{stdlib/builtin → core}/string_io.rbs +7 -7
- data/{stdlib/builtin → core}/struct.rbs +2 -2
- data/{stdlib/builtin → core}/symbol.rbs +1 -1
- data/{stdlib/builtin → core}/thread.rbs +4 -4
- data/{stdlib/builtin → core}/thread_group.rbs +0 -0
- data/{stdlib/builtin → core}/time.rbs +0 -0
- data/{stdlib/builtin → core}/trace_point.rbs +0 -0
- data/{stdlib/builtin → core}/true_class.rbs +1 -1
- data/{stdlib/builtin → core}/unbound_method.rbs +0 -0
- data/{stdlib/builtin → core}/warning.rbs +0 -0
- data/docs/repo.md +125 -0
- data/docs/syntax.md +50 -6
- data/lib/rbs.rb +1 -0
- data/lib/rbs/cli.rb +105 -103
- data/lib/rbs/definition.rb +9 -4
- data/lib/rbs/definition_builder.rb +50 -17
- data/lib/rbs/environment_loader.rb +79 -105
- data/lib/rbs/environment_walker.rb +70 -35
- data/lib/rbs/parser.rb +404 -397
- data/lib/rbs/parser.y +18 -9
- data/lib/rbs/prototype/rb.rb +186 -25
- data/lib/rbs/prototype/runtime.rb +18 -7
- data/lib/rbs/repository.rb +121 -0
- data/lib/rbs/test/hook.rb +27 -15
- data/lib/rbs/test/setup.rb +5 -3
- data/lib/rbs/test/tester.rb +4 -1
- data/lib/rbs/test/type_check.rb +16 -5
- data/lib/rbs/type_name.rb +2 -1
- data/lib/rbs/vendorer.rb +38 -16
- data/lib/rbs/version.rb +1 -1
- data/sig/cli.rbs +58 -0
- data/sig/constant_table.rbs +1 -1
- data/sig/declarations.rbs +1 -1
- data/sig/definition.rbs +9 -4
- data/sig/definition_builder.rbs +4 -1
- data/sig/environment_loader.rbs +92 -46
- data/sig/members.rbs +2 -2
- data/sig/method_types.rbs +1 -1
- data/sig/namespace.rbs +1 -1
- data/sig/polyfill.rbs +42 -0
- data/sig/rbs.rbs +8 -0
- data/sig/repository.rbs +79 -0
- data/sig/vendorer.rbs +44 -0
- data/stdlib/abbrev/{abbrev.rbs → 0/abbrev.rbs} +0 -0
- data/stdlib/base64/{base64.rbs → 0/base64.rbs} +1 -1
- data/stdlib/benchmark/{benchmark.rbs → 0/benchmark.rbs} +0 -0
- data/stdlib/{bigdecimal/math → bigdecimal-math/0}/big_math.rbs +0 -0
- data/stdlib/bigdecimal/{big_decimal.rbs → 0/big_decimal.rbs} +0 -0
- data/stdlib/coverage/{coverage.rbs → 0/coverage.rbs} +2 -2
- data/stdlib/csv/{csv.rbs → 0/csv.rbs} +4 -4
- data/stdlib/date/{date.rbs → 0/date.rbs} +2 -2
- data/stdlib/date/{date_time.rbs → 0/date_time.rbs} +1 -1
- data/stdlib/dbm/0/dbm.rbs +277 -0
- data/stdlib/erb/{erb.rbs → 0/erb.rbs} +0 -0
- data/stdlib/fiber/{fiber.rbs → 0/fiber.rbs} +0 -0
- data/stdlib/find/{find.rbs → 0/find.rbs} +2 -2
- data/stdlib/forwardable/{forwardable.rbs → 0/forwardable.rbs} +0 -0
- data/stdlib/ipaddr/{ipaddr.rbs → 0/ipaddr.rbs} +0 -0
- data/stdlib/json/{json.rbs → 0/json.rbs} +0 -0
- data/stdlib/logger/{formatter.rbs → 0/formatter.rbs} +0 -0
- data/stdlib/logger/{log_device.rbs → 0/log_device.rbs} +1 -1
- data/stdlib/logger/{logger.rbs → 0/logger.rbs} +1 -1
- data/stdlib/logger/{period.rbs → 0/period.rbs} +0 -0
- data/stdlib/logger/{severity.rbs → 0/severity.rbs} +0 -0
- data/stdlib/mutex_m/{mutex_m.rbs → 0/mutex_m.rbs} +0 -0
- data/stdlib/pathname/{pathname.rbs → 0/pathname.rbs} +39 -39
- data/stdlib/prime/{integer-extension.rbs → 0/integer-extension.rbs} +0 -0
- data/stdlib/prime/{prime.rbs → 0/prime.rbs} +1 -1
- data/stdlib/pstore/0/pstore.rbs +287 -0
- data/stdlib/pty/{pty.rbs → 0/pty.rbs} +1 -1
- data/stdlib/securerandom/{securerandom.rbs → 0/securerandom.rbs} +0 -0
- data/stdlib/set/{set.rbs → 0/set.rbs} +10 -10
- data/stdlib/singleton/0/singleton.rbs +111 -0
- data/stdlib/tmpdir/{tmpdir.rbs → 0/tmpdir.rbs} +0 -0
- data/stdlib/tsort/0/cyclic.rbs +4 -0
- data/stdlib/tsort/0/interfaces.rbs +19 -0
- data/stdlib/tsort/0/tsort.rbs +363 -0
- data/stdlib/uri/{file.rbs → 0/file.rbs} +0 -0
- data/stdlib/uri/{generic.rbs → 0/generic.rbs} +1 -1
- data/stdlib/uri/{http.rbs → 0/http.rbs} +0 -0
- data/stdlib/uri/{https.rbs → 0/https.rbs} +0 -0
- data/stdlib/uri/{ldap.rbs → 0/ldap.rbs} +0 -0
- data/stdlib/uri/{ldaps.rbs → 0/ldaps.rbs} +0 -0
- data/stdlib/yaml/0/dbm.rbs +221 -0
- data/stdlib/yaml/0/store.rbs +53 -0
- data/stdlib/zlib/{zlib.rbs → 0/zlib.rbs} +0 -0
- data/steep/Gemfile.lock +9 -9
- metadata +108 -94
- data/stdlib/builtin/builtin.rbs +0 -42
File without changes
|
@@ -0,0 +1,287 @@
|
|
1
|
+
# PStore implements a file based persistence mechanism based on a Hash. User
|
2
|
+
# code can store hierarchies of Ruby objects (values) into the data store file
|
3
|
+
# by name (keys). An object hierarchy may be just a single object. User code
|
4
|
+
# may later read values back from the data store or even update data, as needed.
|
5
|
+
#
|
6
|
+
# The transactional behavior ensures that any changes succeed or fail together.
|
7
|
+
# This can be used to ensure that the data store is not left in a transitory
|
8
|
+
# state, where some values were updated but others were not.
|
9
|
+
#
|
10
|
+
# Behind the scenes, Ruby objects are stored to the data store file with
|
11
|
+
# Marshal. That carries the usual limitations. Proc objects cannot be
|
12
|
+
# marshalled, for example.
|
13
|
+
#
|
14
|
+
# ## Usage example:
|
15
|
+
#
|
16
|
+
# require "pstore"
|
17
|
+
#
|
18
|
+
# # a mock wiki object...
|
19
|
+
# class WikiPage
|
20
|
+
# def initialize( page_name, author, contents )
|
21
|
+
# @page_name = page_name
|
22
|
+
# @revisions = Array.new
|
23
|
+
#
|
24
|
+
# add_revision(author, contents)
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# attr_reader :page_name
|
28
|
+
#
|
29
|
+
# def add_revision( author, contents )
|
30
|
+
# @revisions << { :created => Time.now,
|
31
|
+
# :author => author,
|
32
|
+
# :contents => contents }
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
# def wiki_page_references
|
36
|
+
# [@page_name] + @revisions.last[:contents].scan(/\b(?:[A-Z]+[a-z]+){2,}/)
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# # ...
|
40
|
+
# end
|
41
|
+
#
|
42
|
+
# # create a new page...
|
43
|
+
# home_page = WikiPage.new( "HomePage", "James Edward Gray II",
|
44
|
+
# "A page about the JoysOfDocumentation..." )
|
45
|
+
#
|
46
|
+
# # then we want to update page data and the index together, or not at all...
|
47
|
+
# wiki = PStore.new("wiki_pages.pstore")
|
48
|
+
# wiki.transaction do # begin transaction; do all of this or none of it
|
49
|
+
# # store page...
|
50
|
+
# wiki[home_page.page_name] = home_page
|
51
|
+
# # ensure that an index has been created...
|
52
|
+
# wiki[:wiki_index] ||= Array.new
|
53
|
+
# # update wiki index...
|
54
|
+
# wiki[:wiki_index].push(*home_page.wiki_page_references)
|
55
|
+
# end # commit changes to wiki data store file
|
56
|
+
#
|
57
|
+
# ### Some time later... ###
|
58
|
+
#
|
59
|
+
# # read wiki data...
|
60
|
+
# wiki.transaction(true) do # begin read-only transaction, no changes allowed
|
61
|
+
# wiki.roots.each do |data_root_name|
|
62
|
+
# p data_root_name
|
63
|
+
# p wiki[data_root_name]
|
64
|
+
# end
|
65
|
+
# end
|
66
|
+
#
|
67
|
+
# ## Transaction modes
|
68
|
+
#
|
69
|
+
# By default, file integrity is only ensured as long as the operating system
|
70
|
+
# (and the underlying hardware) doesn't raise any unexpected I/O errors. If an
|
71
|
+
# I/O error occurs while PStore is writing to its file, then the file will
|
72
|
+
# become corrupted.
|
73
|
+
#
|
74
|
+
# You can prevent this by setting *pstore.ultra_safe = true*. However, this
|
75
|
+
# results in a minor performance loss, and only works on platforms that support
|
76
|
+
# atomic file renames. Please consult the documentation for `ultra_safe` for
|
77
|
+
# details.
|
78
|
+
#
|
79
|
+
# Needless to say, if you're storing valuable data with PStore, then you should
|
80
|
+
# backup the PStore files from time to time.
|
81
|
+
#
|
82
|
+
class PStore
|
83
|
+
public
|
84
|
+
|
85
|
+
# Retrieves a value from the PStore file data, by *name*. The hierarchy of Ruby
|
86
|
+
# objects stored under that root *name* will be returned.
|
87
|
+
#
|
88
|
+
# **WARNING**: This method is only valid in a PStore#transaction. It will
|
89
|
+
# raise PStore::Error if called at any other time.
|
90
|
+
#
|
91
|
+
def []: (untyped name) -> untyped
|
92
|
+
|
93
|
+
# Stores an individual Ruby object or a hierarchy of Ruby objects in the data
|
94
|
+
# store file under the root *name*. Assigning to a *name* already in the data
|
95
|
+
# store clobbers the old data.
|
96
|
+
#
|
97
|
+
# ## Example:
|
98
|
+
#
|
99
|
+
# require "pstore"
|
100
|
+
#
|
101
|
+
# store = PStore.new("data_file.pstore")
|
102
|
+
# store.transaction do # begin transaction
|
103
|
+
# # load some data into the store...
|
104
|
+
# store[:single_object] = "My data..."
|
105
|
+
# store[:obj_hierarchy] = { "Kev Jackson" => ["rational.rb", "pstore.rb"],
|
106
|
+
# "James Gray" => ["erb.rb", "pstore.rb"] }
|
107
|
+
# end # commit changes to data store file
|
108
|
+
#
|
109
|
+
# **WARNING**: This method is only valid in a PStore#transaction and it cannot
|
110
|
+
# be read-only. It will raise PStore::Error if called at any other time.
|
111
|
+
#
|
112
|
+
def []=: (untyped name, untyped value) -> untyped
|
113
|
+
|
114
|
+
# Ends the current PStore#transaction, discarding any changes to the data store.
|
115
|
+
#
|
116
|
+
# ## Example:
|
117
|
+
#
|
118
|
+
# require "pstore"
|
119
|
+
#
|
120
|
+
# store = PStore.new("data_file.pstore")
|
121
|
+
# store.transaction do # begin transaction
|
122
|
+
# store[:one] = 1 # this change is not applied, see below...
|
123
|
+
# store[:two] = 2 # this change is not applied, see below...
|
124
|
+
#
|
125
|
+
# store.abort # end transaction here, discard all changes
|
126
|
+
#
|
127
|
+
# store[:three] = 3 # this change is never reached
|
128
|
+
# end
|
129
|
+
#
|
130
|
+
# **WARNING**: This method is only valid in a PStore#transaction. It will
|
131
|
+
# raise PStore::Error if called at any other time.
|
132
|
+
#
|
133
|
+
def abort: () -> untyped
|
134
|
+
|
135
|
+
# Ends the current PStore#transaction, committing any changes to the data store
|
136
|
+
# immediately.
|
137
|
+
#
|
138
|
+
# ## Example:
|
139
|
+
#
|
140
|
+
# require "pstore"
|
141
|
+
#
|
142
|
+
# store = PStore.new("data_file.pstore")
|
143
|
+
# store.transaction do # begin transaction
|
144
|
+
# # load some data into the store...
|
145
|
+
# store[:one] = 1
|
146
|
+
# store[:two] = 2
|
147
|
+
#
|
148
|
+
# store.commit # end transaction here, committing changes
|
149
|
+
#
|
150
|
+
# store[:three] = 3 # this change is never reached
|
151
|
+
# end
|
152
|
+
#
|
153
|
+
# **WARNING**: This method is only valid in a PStore#transaction. It will
|
154
|
+
# raise PStore::Error if called at any other time.
|
155
|
+
#
|
156
|
+
def commit: () -> nil
|
157
|
+
|
158
|
+
# Removes an object hierarchy from the data store, by *name*.
|
159
|
+
#
|
160
|
+
# **WARNING**: This method is only valid in a PStore#transaction and it cannot
|
161
|
+
# be read-only. It will raise PStore::Error if called at any other time.
|
162
|
+
#
|
163
|
+
def delete: (untyped name) -> untyped
|
164
|
+
|
165
|
+
# This method is just like PStore#[], save that you may also provide a *default*
|
166
|
+
# value for the object. In the event the specified *name* is not found in the
|
167
|
+
# data store, your *default* will be returned instead. If you do not specify a
|
168
|
+
# default, PStore::Error will be raised if the object is not found.
|
169
|
+
#
|
170
|
+
# **WARNING**: This method is only valid in a PStore#transaction. It will
|
171
|
+
# raise PStore::Error if called at any other time.
|
172
|
+
#
|
173
|
+
def fetch: (untyped name, ?untyped default) -> untyped
|
174
|
+
|
175
|
+
# Returns the path to the data store file.
|
176
|
+
#
|
177
|
+
def path: () -> untyped
|
178
|
+
|
179
|
+
# Returns true if the supplied *name* is currently in the data store.
|
180
|
+
#
|
181
|
+
# **WARNING**: This method is only valid in a PStore#transaction. It will
|
182
|
+
# raise PStore::Error if called at any other time.
|
183
|
+
#
|
184
|
+
def root?: (untyped name) -> bool
|
185
|
+
|
186
|
+
# Returns the names of all object hierarchies currently in the store.
|
187
|
+
#
|
188
|
+
# **WARNING**: This method is only valid in a PStore#transaction. It will
|
189
|
+
# raise PStore::Error if called at any other time.
|
190
|
+
#
|
191
|
+
def roots: () -> Array[untyped]
|
192
|
+
|
193
|
+
# Opens a new transaction for the data store. Code executed inside a block
|
194
|
+
# passed to this method may read and write data to and from the data store file.
|
195
|
+
#
|
196
|
+
# At the end of the block, changes are committed to the data store
|
197
|
+
# automatically. You may exit the transaction early with a call to either
|
198
|
+
# PStore#commit or PStore#abort. See those methods for details about how
|
199
|
+
# changes are handled. Raising an uncaught Exception in the block is equivalent
|
200
|
+
# to calling PStore#abort.
|
201
|
+
#
|
202
|
+
# If *read_only* is set to `true`, you will only be allowed to read from the
|
203
|
+
# data store during the transaction and any attempts to change the data will
|
204
|
+
# raise a PStore::Error.
|
205
|
+
#
|
206
|
+
# Note that PStore does not support nested transactions.
|
207
|
+
#
|
208
|
+
def transaction: (?untyped read_only) -> untyped
|
209
|
+
|
210
|
+
# Whether PStore should do its best to prevent file corruptions, even when under
|
211
|
+
# unlikely-to-occur error conditions such as out-of-space conditions and other
|
212
|
+
# unusual OS filesystem errors. Setting this flag comes at the price in the form
|
213
|
+
# of a performance loss.
|
214
|
+
#
|
215
|
+
# This flag only has effect on platforms on which file renames are atomic (e.g.
|
216
|
+
# all POSIX platforms: Linux, MacOS X, FreeBSD, etc). The default value is
|
217
|
+
# false.
|
218
|
+
#
|
219
|
+
def ultra_safe: () -> untyped
|
220
|
+
|
221
|
+
def ultra_safe=: (untyped) -> untyped
|
222
|
+
|
223
|
+
private
|
224
|
+
|
225
|
+
def dump: (untyped table) -> untyped
|
226
|
+
|
227
|
+
def empty_marshal_checksum: () -> untyped
|
228
|
+
|
229
|
+
def empty_marshal_data: () -> untyped
|
230
|
+
|
231
|
+
# Raises PStore::Error if the calling code is not in a PStore#transaction.
|
232
|
+
#
|
233
|
+
def in_transaction: () -> untyped
|
234
|
+
|
235
|
+
# Raises PStore::Error if the calling code is not in a PStore#transaction or if
|
236
|
+
# the code is in a read-only PStore#transaction.
|
237
|
+
#
|
238
|
+
def in_transaction_wr: () -> untyped
|
239
|
+
|
240
|
+
# To construct a PStore object, pass in the *file* path where you would like the
|
241
|
+
# data to be stored.
|
242
|
+
#
|
243
|
+
# PStore objects are always reentrant. But if *thread_safe* is set to true, then
|
244
|
+
# it will become thread-safe at the cost of a minor performance hit.
|
245
|
+
#
|
246
|
+
def initialize: (untyped file, ?boolish thread_safe) -> untyped
|
247
|
+
|
248
|
+
def load: (untyped content) -> untyped
|
249
|
+
|
250
|
+
# Load the given PStore file. If `read_only` is true, the unmarshalled Hash will
|
251
|
+
# be returned. If `read_only` is false, a 3-tuple will be returned: the
|
252
|
+
# unmarshalled Hash, a checksum of the data, and the size of the data.
|
253
|
+
#
|
254
|
+
def load_data: (untyped file, untyped read_only) -> untyped
|
255
|
+
|
256
|
+
def on_windows?: () -> bool
|
257
|
+
|
258
|
+
# Open the specified filename (either in read-only mode or in read-write mode)
|
259
|
+
# and lock it for reading or writing.
|
260
|
+
#
|
261
|
+
# The opened File object will be returned. If *read_only* is true, and the file
|
262
|
+
# does not exist, then nil will be returned.
|
263
|
+
#
|
264
|
+
# All exceptions are propagated.
|
265
|
+
#
|
266
|
+
def open_and_lock_file: (untyped filename, untyped read_only) -> untyped
|
267
|
+
|
268
|
+
def save_data: (untyped original_checksum, untyped original_file_size, untyped file) -> untyped
|
269
|
+
|
270
|
+
def save_data_with_atomic_file_rename_strategy: (untyped data, untyped file) -> untyped
|
271
|
+
|
272
|
+
def save_data_with_fast_strategy: (untyped data, untyped file) -> untyped
|
273
|
+
end
|
274
|
+
|
275
|
+
PStore::EMPTY_MARSHAL_CHECKSUM: String
|
276
|
+
|
277
|
+
PStore::EMPTY_MARSHAL_DATA: String
|
278
|
+
|
279
|
+
PStore::EMPTY_STRING: String
|
280
|
+
|
281
|
+
PStore::RDWR_ACCESS: Hash[untyped, untyped]
|
282
|
+
|
283
|
+
PStore::RD_ACCESS: Hash[untyped, untyped]
|
284
|
+
|
285
|
+
PStore::VERSION: String
|
286
|
+
|
287
|
+
PStore::WR_ACCESS: Hash[untyped, untyped]
|
@@ -66,7 +66,7 @@ module PTY
|
|
66
66
|
# : If `true` and the process identified by `pid` is no longer alive a
|
67
67
|
# PTY::ChildExited is raised.
|
68
68
|
#
|
69
|
-
def self.check: (Integer pid, ?
|
69
|
+
def self.check: (Integer pid, ?boolish raise) -> Process::Status?
|
70
70
|
|
71
71
|
alias self.getpty self.spawn
|
72
72
|
|
File without changes
|
@@ -51,8 +51,8 @@ class Set[A]
|
|
51
51
|
# Set.new(1..5) #=> #<Set: {1, 2, 3, 4, 5}>
|
52
52
|
# Set.new([1, 2, 3]) { |x| x * x } #=> #<Set: {1, 4, 9}>
|
53
53
|
#
|
54
|
-
def initialize: (_Each[A
|
55
|
-
| [X] (_Each[X
|
54
|
+
def initialize: (_Each[A]) -> untyped
|
55
|
+
| [X] (_Each[X]) { (X) -> A } -> untyped
|
56
56
|
| (?nil) -> untyped
|
57
57
|
|
58
58
|
# Creates a new set containing the given objects.
|
@@ -69,7 +69,7 @@ class Set[A]
|
|
69
69
|
# Set[1, 3, 5] & Set[3, 2, 1] #=> #<Set: {3, 1}>
|
70
70
|
# Set['a', 'b', 'z'] & ['a', 'b', 'c'] #=> #<Set: {"a", "b"}>
|
71
71
|
#
|
72
|
-
def &: (_Each[A
|
72
|
+
def &: (_Each[A]) -> self
|
73
73
|
|
74
74
|
alias intersection &
|
75
75
|
|
@@ -79,7 +79,7 @@ class Set[A]
|
|
79
79
|
# Set[1, 2, 3] | Set[2, 4, 5] #=> #<Set: {1, 2, 3, 4, 5}>
|
80
80
|
# Set[1, 5, 'z'] | (1..6) #=> #<Set: {1, 5, "z", 2, 3, 4, 6}>
|
81
81
|
#
|
82
|
-
def |: (_Each[A
|
82
|
+
def |: (_Each[A]) -> self
|
83
83
|
|
84
84
|
alias union |
|
85
85
|
|
@@ -91,7 +91,7 @@ class Set[A]
|
|
91
91
|
# Set[1, 3, 5] - Set[1, 5] #=> #<Set: {3}>
|
92
92
|
# Set['a', 'b', 'z'] - ['a', 'c'] #=> #<Set: {"b", "z"}>
|
93
93
|
#
|
94
|
-
def -: (_Each[A
|
94
|
+
def -: (_Each[A]) -> self
|
95
95
|
|
96
96
|
alias difference -
|
97
97
|
|
@@ -133,7 +133,7 @@ class Set[A]
|
|
133
133
|
# Set[1, 2] ^ Set[2, 3] #=> #<Set: {3, 1}>
|
134
134
|
# Set[1, 'b', 'c'] ^ ['b', 'd'] #=> #<Set: {"d", 1, "c"}>
|
135
135
|
#
|
136
|
-
def ^: (_Each[A
|
136
|
+
def ^: (_Each[A]) -> self
|
137
137
|
|
138
138
|
# Classifies the set by the return value of the given block and returns a hash
|
139
139
|
# of {value => set of elements} pairs. The block is called once for each
|
@@ -248,7 +248,7 @@ class Set[A]
|
|
248
248
|
# Merges the elements of the given enumerable object to the set and returns
|
249
249
|
# self.
|
250
250
|
#
|
251
|
-
def merge: (_Each[A
|
251
|
+
def merge: (_Each[A]) -> self
|
252
252
|
|
253
253
|
# Returns true if the set is a subset of the given set.
|
254
254
|
#
|
@@ -271,7 +271,7 @@ class Set[A]
|
|
271
271
|
# set.replace([1, 2]) #=> #<Set: {1, 2}>
|
272
272
|
# set #=> #<Set: {1, 2}>
|
273
273
|
#
|
274
|
-
def replace: (_Each[A
|
274
|
+
def replace: (_Each[A]) -> self
|
275
275
|
|
276
276
|
# Resets the internal state after modification to existing elements and returns
|
277
277
|
# self.
|
@@ -288,7 +288,7 @@ class Set[A]
|
|
288
288
|
# Deletes every element that appears in the given enumerable object and returns
|
289
289
|
# self.
|
290
290
|
#
|
291
|
-
def subtract: (_Each[A
|
291
|
+
def subtract: (_Each[A]) -> self
|
292
292
|
|
293
293
|
# Converts the set to an array. The order of elements is uncertain.
|
294
294
|
#
|
@@ -297,5 +297,5 @@ class Set[A]
|
|
297
297
|
#
|
298
298
|
def to_a: () -> Array[A]
|
299
299
|
|
300
|
-
include Enumerable[A
|
300
|
+
include Enumerable[A]
|
301
301
|
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
# The Singleton module implements the Singleton pattern.
|
2
|
+
#
|
3
|
+
# ## Usage
|
4
|
+
#
|
5
|
+
# To use Singleton, include the module in your class.
|
6
|
+
#
|
7
|
+
# class Klass
|
8
|
+
# include Singleton
|
9
|
+
# # ...
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# This ensures that only one instance of Klass can be created.
|
13
|
+
#
|
14
|
+
# a,b = Klass.instance, Klass.instance
|
15
|
+
#
|
16
|
+
# a == b
|
17
|
+
# # => true
|
18
|
+
#
|
19
|
+
# Klass.new
|
20
|
+
# # => NoMethodError - new is private ...
|
21
|
+
#
|
22
|
+
# The instance is created at upon the first call of Klass.instance().
|
23
|
+
#
|
24
|
+
# class OtherKlass
|
25
|
+
# include Singleton
|
26
|
+
# # ...
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# ObjectSpace.each_object(OtherKlass){}
|
30
|
+
# # => 0
|
31
|
+
#
|
32
|
+
# OtherKlass.instance
|
33
|
+
# ObjectSpace.each_object(OtherKlass){}
|
34
|
+
# # => 1
|
35
|
+
#
|
36
|
+
# This behavior is preserved under inheritance and cloning.
|
37
|
+
#
|
38
|
+
# ## Implementation
|
39
|
+
#
|
40
|
+
# This above is achieved by:
|
41
|
+
#
|
42
|
+
# * Making Klass.new and Klass.allocate private.
|
43
|
+
#
|
44
|
+
# * Overriding Klass.inherited(sub_klass) and Klass.clone() to ensure that the
|
45
|
+
# Singleton properties are kept when inherited and cloned.
|
46
|
+
#
|
47
|
+
# * Providing the Klass.instance() method that returns the same object each
|
48
|
+
# time it is called.
|
49
|
+
#
|
50
|
+
# * Overriding Klass._load(str) to call Klass.instance().
|
51
|
+
#
|
52
|
+
# * Overriding Klass#clone and Klass#dup to raise TypeErrors to prevent
|
53
|
+
# cloning or duping.
|
54
|
+
#
|
55
|
+
#
|
56
|
+
# ## Singleton and Marshal
|
57
|
+
#
|
58
|
+
# By default Singleton's #_dump(depth) returns the empty string. Marshalling by
|
59
|
+
# default will strip state information, e.g. instance variables from the
|
60
|
+
# instance. Classes using Singleton can provide custom _load(str) and
|
61
|
+
# _dump(depth) methods to retain some of the previous state of the instance.
|
62
|
+
#
|
63
|
+
# require 'singleton'
|
64
|
+
#
|
65
|
+
# class Example
|
66
|
+
# include Singleton
|
67
|
+
# attr_accessor :keep, :strip
|
68
|
+
# def _dump(depth)
|
69
|
+
# # this strips the @strip information from the instance
|
70
|
+
# Marshal.dump(@keep, depth)
|
71
|
+
# end
|
72
|
+
#
|
73
|
+
# def self._load(str)
|
74
|
+
# instance.keep = Marshal.load(str)
|
75
|
+
# instance
|
76
|
+
# end
|
77
|
+
# end
|
78
|
+
#
|
79
|
+
# a = Example.instance
|
80
|
+
# a.keep = "keep this"
|
81
|
+
# a.strip = "get rid of this"
|
82
|
+
#
|
83
|
+
# stored_state = Marshal.dump(a)
|
84
|
+
#
|
85
|
+
# a.keep = nil
|
86
|
+
# a.strip = nil
|
87
|
+
# b = Marshal.load(stored_state)
|
88
|
+
# p a == b # => true
|
89
|
+
# p a.keep # => "keep this"
|
90
|
+
# p a.strip # => nil
|
91
|
+
module Singleton
|
92
|
+
def self.__init__: (Class klass) -> Class
|
93
|
+
|
94
|
+
def self.instance: () -> instance
|
95
|
+
|
96
|
+
public
|
97
|
+
|
98
|
+
# By default, do not retain any state when marshalling.
|
99
|
+
#
|
100
|
+
def _dump: (?Integer depth) -> String
|
101
|
+
|
102
|
+
# Raises a TypeError to prevent cloning.
|
103
|
+
#
|
104
|
+
def clone: () -> bot
|
105
|
+
|
106
|
+
# Raises a TypeError to prevent duping.
|
107
|
+
#
|
108
|
+
def dup: () -> bot
|
109
|
+
end
|
110
|
+
|
111
|
+
Singleton::VERSION: String
|