opal 0.3.10 → 0.3.11
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.
- data/README.md +47 -46
- data/lib/opal.rb +11 -3
- data/lib/opal/builder.rb +4 -46
- data/lib/opal/bundle.rb +11 -25
- data/lib/opal/command.rb +101 -22
- data/lib/opal/context.rb +16 -50
- data/lib/opal/lexer.rb +21 -2
- data/lib/opal/nodes.rb +134 -20
- data/lib/opal/parser.rb +7 -7
- data/lib/opal/parser.y +7 -7
- data/lib/opal/rake/bundle_task.rb +24 -23
- data/lib/opal/version.rb +3 -0
- data/opal-parser.js +8343 -0
- data/opal.js +5685 -0
- data/stdlib/dev.rb +6 -4
- data/templates/init/Rakefile +7 -0
- data/templates/init/index.html +17 -0
- data/templates/init/lib/__NAME__.rb +2 -0
- metadata +9 -32
- data/corelib/array.rb +0 -1424
- data/corelib/boolean.rb +0 -20
- data/corelib/class.rb +0 -58
- data/corelib/core.rb +0 -66
- data/corelib/dir.rb +0 -22
- data/corelib/enumerable.rb +0 -33
- data/corelib/error.rb +0 -19
- data/corelib/file.rb +0 -60
- data/corelib/hash.rb +0 -729
- data/corelib/kernel.rb +0 -251
- data/corelib/load_order +0 -21
- data/corelib/match_data.rb +0 -33
- data/corelib/module.rb +0 -101
- data/corelib/nil_class.rb +0 -54
- data/corelib/numeric.rb +0 -352
- data/corelib/object.rb +0 -37
- data/corelib/proc.rb +0 -55
- data/corelib/range.rb +0 -27
- data/corelib/regexp.rb +0 -69
- data/corelib/string.rb +0 -300
- data/corelib/top_self.rb +0 -8
- data/runtime/class.js +0 -386
- data/runtime/fs.js +0 -199
- data/runtime/init.js +0 -556
- data/runtime/loader.js +0 -330
- data/runtime/module.js +0 -103
- data/runtime/post.js +0 -10
- data/runtime/pre.js +0 -7
- data/runtime/runtime.js +0 -345
data/stdlib/dev.rb
CHANGED
@@ -3,14 +3,16 @@ require 'opal/lexer'
|
|
3
3
|
module Opal
|
4
4
|
|
5
5
|
def self.compile(source)
|
6
|
-
res =
|
6
|
+
res = Parser.new.parse source, :method_missing => true
|
7
7
|
res
|
8
8
|
end
|
9
9
|
|
10
10
|
def self.run_ruby_content(source, filename = "(opal)")
|
11
11
|
js = compile source
|
12
|
-
`
|
13
|
-
|
12
|
+
`opal.run(new Function("(" + js +
|
13
|
+
")(opal.runtime, opal.runtime.top, '')"));`
|
14
|
+
|
15
|
+
nil
|
14
16
|
end
|
15
17
|
|
16
18
|
# Load the ruby code at the remote url, parse and run it. This is typically
|
@@ -50,7 +52,7 @@ module Opal
|
|
50
52
|
|
51
53
|
if (script.type == "text/ruby") {
|
52
54
|
if (script.src) {
|
53
|
-
#{ run_remote_content `script.src` };
|
55
|
+
#{ run_remote_content `script.getAttribute('src', 2)` };
|
54
56
|
} else {
|
55
57
|
opal.run(function() {
|
56
58
|
#{ run_ruby_content `script.innerHTML`, "(script-tag)" };
|
@@ -0,0 +1,17 @@
|
|
1
|
+
<!doctype html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<meta http-equiv="content-type" content="text/html; charset=utf-8">
|
5
|
+
|
6
|
+
<title>__NAME__</title>
|
7
|
+
</head>
|
8
|
+
<body>
|
9
|
+
<script src="js/opal.js" type="text/javascript" charset="utf-8"></script>
|
10
|
+
<script src="__NAME__-0.0.1.js" type="text/javascript" charset="utf-8"></script>
|
11
|
+
|
12
|
+
<script type="text/javascript" charset="utf-8">
|
13
|
+
opal.require("__NAME__");
|
14
|
+
</script>
|
15
|
+
</body>
|
16
|
+
</html>
|
17
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: opal
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.11
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,9 +9,9 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-09-
|
12
|
+
date: 2011-09-26 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
|
-
description: Ruby runtime and core library for javascript
|
14
|
+
description: Ruby runtime and core library for javascript.
|
15
15
|
email:
|
16
16
|
- adam@adambeynon.com
|
17
17
|
executables:
|
@@ -29,40 +29,17 @@ files:
|
|
29
29
|
- lib/opal/parser.rb
|
30
30
|
- lib/opal/parser.y
|
31
31
|
- lib/opal/rake/bundle_task.rb
|
32
|
+
- lib/opal/version.rb
|
32
33
|
- lib/opal.rb
|
33
|
-
- runtime/class.js
|
34
|
-
- runtime/fs.js
|
35
|
-
- runtime/init.js
|
36
|
-
- runtime/loader.js
|
37
|
-
- runtime/module.js
|
38
|
-
- runtime/post.js
|
39
|
-
- runtime/pre.js
|
40
|
-
- runtime/runtime.js
|
41
|
-
- corelib/array.rb
|
42
|
-
- corelib/boolean.rb
|
43
|
-
- corelib/class.rb
|
44
|
-
- corelib/core.rb
|
45
|
-
- corelib/dir.rb
|
46
|
-
- corelib/enumerable.rb
|
47
|
-
- corelib/error.rb
|
48
|
-
- corelib/file.rb
|
49
|
-
- corelib/hash.rb
|
50
|
-
- corelib/kernel.rb
|
51
|
-
- corelib/load_order
|
52
|
-
- corelib/match_data.rb
|
53
|
-
- corelib/module.rb
|
54
|
-
- corelib/nil_class.rb
|
55
|
-
- corelib/numeric.rb
|
56
|
-
- corelib/object.rb
|
57
|
-
- corelib/proc.rb
|
58
|
-
- corelib/range.rb
|
59
|
-
- corelib/regexp.rb
|
60
|
-
- corelib/string.rb
|
61
|
-
- corelib/top_self.rb
|
62
34
|
- stdlib/dev.rb
|
63
35
|
- stdlib/racc/parser.rb
|
64
36
|
- stdlib/strscan.rb
|
37
|
+
- templates/init/index.html
|
38
|
+
- templates/init/lib/__NAME__.rb
|
39
|
+
- templates/init/Rakefile
|
65
40
|
- README.md
|
41
|
+
- opal.js
|
42
|
+
- opal-parser.js
|
66
43
|
homepage: http://opalscript.org
|
67
44
|
licenses: []
|
68
45
|
post_install_message:
|
data/corelib/array.rb
DELETED
@@ -1,1424 +0,0 @@
|
|
1
|
-
class Array
|
2
|
-
# include Enumerable
|
3
|
-
|
4
|
-
# Returns a new array populated with the given objects.
|
5
|
-
#
|
6
|
-
# @example
|
7
|
-
#
|
8
|
-
# Array['a', 'b', 'c'] # => ['a', 'b', 'c']
|
9
|
-
#
|
10
|
-
# @param [Object] objs
|
11
|
-
# @return [Array]
|
12
|
-
def self.[](*objs)
|
13
|
-
`var ary = #{allocate};
|
14
|
-
ary.splice.apply(ary, [0, 0].concat(objs));
|
15
|
-
return ary;`
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.allocate
|
19
|
-
`var ary = new self.$a();
|
20
|
-
return ary;`
|
21
|
-
end
|
22
|
-
|
23
|
-
def initialize(len = 0, fill = nil)
|
24
|
-
`for (var i = 0; i < len; i++) {
|
25
|
-
self[i] = fill;
|
26
|
-
}
|
27
|
-
|
28
|
-
self.length = len;
|
29
|
-
|
30
|
-
return self;`
|
31
|
-
end
|
32
|
-
|
33
|
-
# Returns a formatted, printable version of the array. {#inspect} is called
|
34
|
-
# on each of the elements and appended to the string.
|
35
|
-
#
|
36
|
-
# @return [String] string representation of the receiver
|
37
|
-
def inspect
|
38
|
-
`var description = [];
|
39
|
-
|
40
|
-
for (var i = 0, length = self.length; i < length; i++) {
|
41
|
-
description.push(#{`self[i]`.inspect});
|
42
|
-
}
|
43
|
-
|
44
|
-
return '[' + description.join(', ') + ']';`
|
45
|
-
end
|
46
|
-
|
47
|
-
# Returns a simple string version of the array. {#to_s} is applied to each
|
48
|
-
# of the child elements with no seperator.
|
49
|
-
def to_s
|
50
|
-
`var description = [];
|
51
|
-
|
52
|
-
for (var i = 0, length = self.length; i < length; i++) {
|
53
|
-
description.push(#{`self[i]`.to_s});
|
54
|
-
}
|
55
|
-
|
56
|
-
return description.join('');`
|
57
|
-
end
|
58
|
-
|
59
|
-
# Append - pushes the given object onto the end of this array. This
|
60
|
-
# expression returns the array itself, so several appends may be chained
|
61
|
-
# together.
|
62
|
-
#
|
63
|
-
# @example
|
64
|
-
#
|
65
|
-
# [1, 2] << "c" << "d" << [3, 4]
|
66
|
-
# # => [1, 2, "c", "d", [3, 4]]
|
67
|
-
#
|
68
|
-
# @param [Object] obj the object to append
|
69
|
-
# @return [Array] returns the receiver
|
70
|
-
def <<(obj)
|
71
|
-
`self.push(obj);`
|
72
|
-
self
|
73
|
-
end
|
74
|
-
|
75
|
-
# Returns the number of elements in `self`. May be zero.
|
76
|
-
#
|
77
|
-
# @example
|
78
|
-
#
|
79
|
-
# [1, 2, 3, 4, 5].length
|
80
|
-
# # => 5
|
81
|
-
#
|
82
|
-
# @return [Numeric] length
|
83
|
-
def length
|
84
|
-
`return self.length;`
|
85
|
-
end
|
86
|
-
|
87
|
-
alias_method :size, :length
|
88
|
-
|
89
|
-
# Yields the block once for each element in `self`, passing that element as
|
90
|
-
# a parameter.
|
91
|
-
#
|
92
|
-
# If no block is given, an enumerator is returned instead.
|
93
|
-
#
|
94
|
-
# @example
|
95
|
-
#
|
96
|
-
# a = ['a', 'b', 'c']
|
97
|
-
# a.each { |x| puts x }
|
98
|
-
# # => 'a'
|
99
|
-
# # => 'b'
|
100
|
-
# # => 'c'
|
101
|
-
#
|
102
|
-
# **TODO** needs to return enumerator for no block.
|
103
|
-
#
|
104
|
-
# @return [Array] returns the receiver
|
105
|
-
def each
|
106
|
-
raise "Array#each no block given" unless block_given?
|
107
|
-
|
108
|
-
`for (var i = 0, len = self.length; i < len; i++) {`
|
109
|
-
yield `self[i]`
|
110
|
-
`}`
|
111
|
-
self
|
112
|
-
end
|
113
|
-
|
114
|
-
# Similar to {#each}, but also passes in the current element index to the
|
115
|
-
# block.
|
116
|
-
def each_with_index
|
117
|
-
raise "Array#each_with_index no block given" unless block_given?
|
118
|
-
|
119
|
-
`for (var i = 0, len = self.length; i < len; i++) {`
|
120
|
-
yield `self[i]`, `i`
|
121
|
-
`}`
|
122
|
-
self
|
123
|
-
end
|
124
|
-
|
125
|
-
# Same as {#each}, but passes the index of the element instead of the
|
126
|
-
# element itself.
|
127
|
-
#
|
128
|
-
# If no block given, an enumerator is returned instead.
|
129
|
-
#
|
130
|
-
# **TODO** enumerator functionality not yet implemented.
|
131
|
-
#
|
132
|
-
# @example
|
133
|
-
#
|
134
|
-
# a = [1, 2, 3]
|
135
|
-
# a.each_index { |x| puts x }
|
136
|
-
# # => 0
|
137
|
-
# # => 1
|
138
|
-
# # => 2
|
139
|
-
#
|
140
|
-
# @return [Array] returns receiver
|
141
|
-
def each_index
|
142
|
-
raise "Array#each_index no block given" unless block_given?
|
143
|
-
|
144
|
-
`for (var i = 0, len = self.length; i < len; i++) {`
|
145
|
-
yield `i`
|
146
|
-
`}`
|
147
|
-
self
|
148
|
-
end
|
149
|
-
|
150
|
-
# Append - pushes the given object(s) onto the end of this array. This
|
151
|
-
# expression returns the array itself, so several appends may be chained
|
152
|
-
# together.
|
153
|
-
#
|
154
|
-
# @example
|
155
|
-
#
|
156
|
-
# a = ['a', 'b', 'c']
|
157
|
-
# a.push 'd', 'e', 'f'
|
158
|
-
# # => ['a', 'b', 'c', 'd', 'e', 'f'
|
159
|
-
#
|
160
|
-
# @param [Object] obj the object(s) to push onto the array
|
161
|
-
# @return [Array] returns the receiver
|
162
|
-
def push(*objs)
|
163
|
-
`for (var i = 0, ii = objs.length; i < ii; i++) {
|
164
|
-
self.push(objs[i]);
|
165
|
-
}
|
166
|
-
return self;`
|
167
|
-
end
|
168
|
-
|
169
|
-
# Returns the index of the first object in `self` such that it is `==` to
|
170
|
-
# `obj`. If a block is given instead of an argument, returns first object for
|
171
|
-
# which the block is true. Returns `nil` if no match is found. See also
|
172
|
-
# {#rindex}.
|
173
|
-
#
|
174
|
-
# @example
|
175
|
-
#
|
176
|
-
# a = ['a', 'b', 'c']
|
177
|
-
# a.index('a') # => 0
|
178
|
-
# a.index('z') # => nil
|
179
|
-
# a.index { |x| x == 'b' } # => 1
|
180
|
-
#
|
181
|
-
# @param [Object] obj the object to look for
|
182
|
-
# @return [Numeric, nil] result
|
183
|
-
def index(obj)
|
184
|
-
`for (var i = 0, len = self.length; i < len; i++) {
|
185
|
-
if (#{`self[i]` == obj}.$r) {
|
186
|
-
return i;
|
187
|
-
}
|
188
|
-
}
|
189
|
-
|
190
|
-
return nil;`
|
191
|
-
end
|
192
|
-
|
193
|
-
# Concatenation - returns a new array built by concatenating the two arrays
|
194
|
-
# together to produce a third array.
|
195
|
-
#
|
196
|
-
# @example
|
197
|
-
#
|
198
|
-
# [1, 2, 3] + [4, 5]
|
199
|
-
# # => [1, 2, 3, 4, 5]
|
200
|
-
#
|
201
|
-
# @param [Array] other the array to concat with
|
202
|
-
# @return [Array] returns new concatenated array
|
203
|
-
def +(other)
|
204
|
-
`return self.slice(0).concat(other.slice());`
|
205
|
-
end
|
206
|
-
|
207
|
-
# Difference. Creates a new array that is a copy of the original array,
|
208
|
-
# removing any items that also appear in `other`.
|
209
|
-
#
|
210
|
-
# @example
|
211
|
-
#
|
212
|
-
# [1, 2, 3, 3, 4, 4, 5] - [1, 2, 4]
|
213
|
-
# # => [3, 3, 5]
|
214
|
-
#
|
215
|
-
# @param [Array] other array to use for difference
|
216
|
-
# @return [Array] new array
|
217
|
-
def -(other)
|
218
|
-
raise "Array#- not yet implemented"
|
219
|
-
end
|
220
|
-
|
221
|
-
# Equality. Two arrays are equal if they contain the same number of elements
|
222
|
-
# and if each element is equal to (according to {BasicObject#==} the
|
223
|
-
# corresponding element in the second array.
|
224
|
-
#
|
225
|
-
# @example
|
226
|
-
#
|
227
|
-
# ['a', 'c'] == ['a', 'c', 7] # => false
|
228
|
-
# ['a', 'c', '7'] == ['a', 'c', 7] # => true
|
229
|
-
# ['a', 'c', 7] == ['a', 'd', 'f'] # => false
|
230
|
-
#
|
231
|
-
# @param [Array] other array to compare self with
|
232
|
-
# @return [Boolean] if the arrays are equal
|
233
|
-
def ==(other)
|
234
|
-
`if (self.$h() == other.$h()) return true;
|
235
|
-
if (self.length != other.length) return false;
|
236
|
-
|
237
|
-
for (var i = 0; i < self.length; i++) {
|
238
|
-
if (!#{`self[i]` == `other[i]`}) {
|
239
|
-
return false;
|
240
|
-
}
|
241
|
-
}
|
242
|
-
|
243
|
-
return true;`
|
244
|
-
end
|
245
|
-
|
246
|
-
# Searches through an array whose elements are also arrays, comparing `obj`
|
247
|
-
# with their first element of each contained array using {BasicObject#==}.
|
248
|
-
# Returns the first contained array that matches (that is, the first
|
249
|
-
# associated array) or `nil` if no match is found. See also {#rassoc}.
|
250
|
-
#
|
251
|
-
# @example
|
252
|
-
#
|
253
|
-
# s1 = ['colors', 'red', 'blue', 'green']
|
254
|
-
# s2 = ['letters', 'a', 'b', 'c']
|
255
|
-
# s3 = 'foo'
|
256
|
-
# a = [s1, s2, s3]
|
257
|
-
#
|
258
|
-
# a.assoc 'letters' # => ['letters', 'a', 'b', 'c']
|
259
|
-
# a.assoc 'foo' # => nil
|
260
|
-
def assoc(obj)
|
261
|
-
`var arg;
|
262
|
-
|
263
|
-
for (var i = 0; i < self.length; i++) {
|
264
|
-
arg = self[i];
|
265
|
-
|
266
|
-
if (arg.length && #{`arg[0]` == obj}.$r) {
|
267
|
-
return arg;
|
268
|
-
}
|
269
|
-
}
|
270
|
-
|
271
|
-
return nil;`
|
272
|
-
end
|
273
|
-
|
274
|
-
# Returns the element at `index`. A negative index counts from the end of the
|
275
|
-
# receiver. Returns `nil` if the given index is out of range. See also {#[]}.
|
276
|
-
#
|
277
|
-
# @example
|
278
|
-
#
|
279
|
-
# a = ['a', 'b', 'c', 'd', 'e']
|
280
|
-
# a.at 0 # => 'a'
|
281
|
-
# a.at -1 # => 'e'
|
282
|
-
# a.at 324 # => nil
|
283
|
-
#
|
284
|
-
# @param [Numeric] index the index to get
|
285
|
-
# @return [Object, nil] returns nil or the result
|
286
|
-
def at(idx)
|
287
|
-
`var size = self.length;
|
288
|
-
|
289
|
-
if (idx < 0) idx += size;
|
290
|
-
|
291
|
-
if (idx < 0 || idx >= size) return nil;
|
292
|
-
return self[idx];`
|
293
|
-
end
|
294
|
-
|
295
|
-
# Removes all elements from the receiver.
|
296
|
-
#
|
297
|
-
# @example
|
298
|
-
#
|
299
|
-
# a = ['a', 'b', 'c', 'd', 'e']
|
300
|
-
# a.clear # => []
|
301
|
-
#
|
302
|
-
# @return [Array] returns the receiver
|
303
|
-
def clear
|
304
|
-
`self.splice(0);
|
305
|
-
return self;`
|
306
|
-
end
|
307
|
-
|
308
|
-
# Yields the block, passing in successive elements from the receiver,
|
309
|
-
# returning an array containing those elements for which the block returns a
|
310
|
-
# true value.
|
311
|
-
#
|
312
|
-
# @example
|
313
|
-
#
|
314
|
-
# a = [1, 2, 3, 4, 5, 6]
|
315
|
-
# a.select { |x| x > 4 }
|
316
|
-
# # => [5, 6]
|
317
|
-
#
|
318
|
-
# @return [Array] returns a new array of selected elements
|
319
|
-
def select
|
320
|
-
`var result = [], arg;
|
321
|
-
|
322
|
-
for (var i = 0, ii = self.length; i < ii; i++) {
|
323
|
-
arg = self[i];
|
324
|
-
|
325
|
-
if (#{yield `arg`}.$r) {
|
326
|
-
result.push(arg);
|
327
|
-
}
|
328
|
-
}
|
329
|
-
|
330
|
-
return result;`
|
331
|
-
end
|
332
|
-
|
333
|
-
# Yields the block once for each element of the receiver. Creates a new array
|
334
|
-
# containing the values returned by the block. See also `Enumerable#collect`.
|
335
|
-
#
|
336
|
-
# @example
|
337
|
-
#
|
338
|
-
# a = ['a', 'b', 'c', 'd']
|
339
|
-
# a.collect { |x| x + '!' } # => ['a!', 'b!', 'c!', 'd!']
|
340
|
-
# a # => ['a', 'b', 'c', 'd']
|
341
|
-
#
|
342
|
-
# @return [Array] new array
|
343
|
-
def collect
|
344
|
-
raise "Array#collect no block given" unless block_given?
|
345
|
-
|
346
|
-
`var result = [];
|
347
|
-
|
348
|
-
for (var i = 0, ii = self.length; i < ii; i++) {
|
349
|
-
result.push(#{ yield `self[i]` });
|
350
|
-
}
|
351
|
-
|
352
|
-
return result;`
|
353
|
-
end
|
354
|
-
|
355
|
-
alias_method :map, :collect
|
356
|
-
|
357
|
-
# Yields the block once for each element of `self`, replacing the element with
|
358
|
-
# the value returned by the block. See also `Enumerable#collect`.
|
359
|
-
#
|
360
|
-
# @example
|
361
|
-
#
|
362
|
-
# a = ['a', 'b', 'c', 'd']
|
363
|
-
# a.collect { |x| x + '!' }
|
364
|
-
# # => ['a!', 'b!', 'c!', 'd!']
|
365
|
-
# a
|
366
|
-
# # => ['a!', 'b!', 'c!', 'd!']
|
367
|
-
#
|
368
|
-
# @return [Array] returns the receiver
|
369
|
-
def collect!
|
370
|
-
`for (var i = 0, ii = self.length; i < ii; i++) {
|
371
|
-
self[i] = #{yield `self[i]`};
|
372
|
-
}
|
373
|
-
|
374
|
-
return self;`
|
375
|
-
end
|
376
|
-
|
377
|
-
# Duplicate.
|
378
|
-
def dup
|
379
|
-
`return self.slice(0);`
|
380
|
-
end
|
381
|
-
|
382
|
-
# Returns a copy of the receiver with all nil elements removed
|
383
|
-
#
|
384
|
-
# @example
|
385
|
-
#
|
386
|
-
# ['a', nil, 'b', nil, 'c', nil].compact
|
387
|
-
# # => ['a', 'b', 'c']
|
388
|
-
#
|
389
|
-
# @return [Array] new Array
|
390
|
-
def compact
|
391
|
-
`var result = [], length = self.length;
|
392
|
-
|
393
|
-
for (var i = 0; i < length; i++) {
|
394
|
-
if (self[i] != nil) {
|
395
|
-
result.push(self[i]);
|
396
|
-
}
|
397
|
-
}
|
398
|
-
|
399
|
-
return result;`
|
400
|
-
end
|
401
|
-
|
402
|
-
# Removes nil elements from the receiver. Returns nil if no changes were made,
|
403
|
-
# otherwise returns self.
|
404
|
-
#
|
405
|
-
# @example
|
406
|
-
#
|
407
|
-
# ['a', nil, 'b', nil, 'c'].compact!
|
408
|
-
# # => ['a', 'b', 'c']
|
409
|
-
#
|
410
|
-
# ['a', 'b', 'c'].compact!
|
411
|
-
# # => nil
|
412
|
-
#
|
413
|
-
# @return [Array, nil] returns either the receiver or nil
|
414
|
-
def compact!
|
415
|
-
`var length = self.length;
|
416
|
-
|
417
|
-
for (var i = 0; i < length; i++) {
|
418
|
-
if (self[i] == nil) {
|
419
|
-
self.splice(i, 1);
|
420
|
-
i--;
|
421
|
-
}
|
422
|
-
}
|
423
|
-
|
424
|
-
return length == self.length ? nil : self;`
|
425
|
-
end
|
426
|
-
|
427
|
-
# Appends the elements of `other` to `self`.
|
428
|
-
#
|
429
|
-
# @example
|
430
|
-
#
|
431
|
-
# ['a', 'b'].concat ['c', 'd']
|
432
|
-
# # => ['a', 'b', 'c', 'd']
|
433
|
-
#
|
434
|
-
# @param [Array] other array to concat
|
435
|
-
# @return [Array] returns the receiver
|
436
|
-
def concat(other)
|
437
|
-
`var length = other.length;
|
438
|
-
|
439
|
-
for (var i = 0; i < length; i++) {
|
440
|
-
self.push(other[i]);
|
441
|
-
}
|
442
|
-
|
443
|
-
return self;`
|
444
|
-
end
|
445
|
-
|
446
|
-
# Returns the number of elements. If an argument is given, counts the number
|
447
|
-
# of elements which equals to `obj`. If a block is given, counts the number of
|
448
|
-
# elements yielding a true value.
|
449
|
-
#
|
450
|
-
# @example
|
451
|
-
#
|
452
|
-
# ary = [1, 2, 4, 2]
|
453
|
-
# ary.count # => 4
|
454
|
-
# ary.count(2) # =>2
|
455
|
-
#
|
456
|
-
# @param [Object] obj object to check
|
457
|
-
# @return [Numeric] count or count of obj
|
458
|
-
def count(obj = undefined)
|
459
|
-
`if (obj != undefined) {
|
460
|
-
var total = 0;
|
461
|
-
|
462
|
-
for (var i = 0; i < self.length; i++) {
|
463
|
-
if (#{`self[i]` == obj}.$r) {
|
464
|
-
total++;
|
465
|
-
}
|
466
|
-
}
|
467
|
-
|
468
|
-
return total;
|
469
|
-
} else {
|
470
|
-
return self.length;
|
471
|
-
}`
|
472
|
-
end
|
473
|
-
|
474
|
-
# Deletes items from `self` that are equal to `obj`. If any items are found,
|
475
|
-
# returns `obj`. If the item is not found, returns `nil`. If the optional code
|
476
|
-
# block is given, returns the result of block if the item is not found.
|
477
|
-
#
|
478
|
-
# @example
|
479
|
-
#
|
480
|
-
# a = ['a', 'b', 'b', 'b', 'c']
|
481
|
-
#
|
482
|
-
# a.delete 'b'
|
483
|
-
# # => 'b'
|
484
|
-
# a
|
485
|
-
# # => ['a', 'c']
|
486
|
-
#
|
487
|
-
# a.delete 'z'
|
488
|
-
# # => nil
|
489
|
-
#
|
490
|
-
# @param [Object] obj object to delete
|
491
|
-
# @return [Object, nil] returns obj or nil
|
492
|
-
def delete(obj)
|
493
|
-
`var length = self.length;
|
494
|
-
|
495
|
-
for (var i = 0; i < self.length; i++) {
|
496
|
-
if (#{`self[i]` == obj}.$r) {
|
497
|
-
self.splice(i, 1);
|
498
|
-
i--;
|
499
|
-
}
|
500
|
-
}
|
501
|
-
|
502
|
-
return length == self.length ? nil : obj;`
|
503
|
-
end
|
504
|
-
|
505
|
-
# Deletes the element at the specified index, returning that element, or nil
|
506
|
-
# if the index is out of range.
|
507
|
-
#
|
508
|
-
# @example
|
509
|
-
#
|
510
|
-
# a = ['ant', 'bat', 'cat', 'dog']
|
511
|
-
# a.delete_at 2
|
512
|
-
# # => 'cat'
|
513
|
-
# a
|
514
|
-
# # => ['ant', 'bat', 'dog']
|
515
|
-
# a.delete_at 99
|
516
|
-
# # => nil
|
517
|
-
#
|
518
|
-
# @param [Numeric] idx the index to delete
|
519
|
-
# @return [Object, nil] returns the deleted object or nil
|
520
|
-
def delete_at(idx)
|
521
|
-
`if (idx < 0) idx += self.length;
|
522
|
-
if (idx < 0 || idx >= self.length) return nil;
|
523
|
-
var res = self[idx];
|
524
|
-
self.splice(idx, 1);
|
525
|
-
return self;`
|
526
|
-
end
|
527
|
-
|
528
|
-
# Deletes every element of `self` for which `block` evaluates to true.
|
529
|
-
#
|
530
|
-
# @example
|
531
|
-
#
|
532
|
-
# a = [1, 2, 3]
|
533
|
-
# a.delete_if { |x| x >= 2 }
|
534
|
-
# # => [1]
|
535
|
-
#
|
536
|
-
# @return [Array] returns amended receiver
|
537
|
-
def delete_if
|
538
|
-
`for (var i = 0, ii = self.length; i < ii; i++) {
|
539
|
-
if (#{yield `self[i]`}.$r) {
|
540
|
-
self.splice(i, 1);
|
541
|
-
i--;
|
542
|
-
ii = self.length;
|
543
|
-
}
|
544
|
-
}
|
545
|
-
return self;`
|
546
|
-
end
|
547
|
-
|
548
|
-
|
549
|
-
# Drop first `n` elements from receiver, and returns remaining elements in
|
550
|
-
# array.
|
551
|
-
#
|
552
|
-
# @example
|
553
|
-
#
|
554
|
-
# a = [1, 2, 3, 4, 5, 6]
|
555
|
-
# a.drop 3
|
556
|
-
# # => [4, 5, 6]
|
557
|
-
#
|
558
|
-
# @param [Number] n number of elements to drop
|
559
|
-
# @return [Array] returns new array
|
560
|
-
def drop(n)
|
561
|
-
`if (n > self.length) return [];
|
562
|
-
return self.slice(n);`
|
563
|
-
end
|
564
|
-
|
565
|
-
# Drop elements up to, but not including, the first element for which the
|
566
|
-
# block returns nil or false, and returns an array containing the remaining
|
567
|
-
# elements.
|
568
|
-
#
|
569
|
-
# @example
|
570
|
-
#
|
571
|
-
# a = [1, 2, 3, 4, 5, 6]
|
572
|
-
# a.drop_while { |i| i < 3 }
|
573
|
-
# # => [3, 4, 5, 6]
|
574
|
-
#
|
575
|
-
# @return [Array] returns a new array
|
576
|
-
def drop_while
|
577
|
-
`for (var i = 0; i < self.length; i++) {
|
578
|
-
if (!#{yield `self[i]`}.$r) {
|
579
|
-
return self.slice(i);
|
580
|
-
}
|
581
|
-
}
|
582
|
-
|
583
|
-
return [];`
|
584
|
-
end
|
585
|
-
|
586
|
-
# Returns `true` if the receiver contains no elements, `false` otherwise.
|
587
|
-
#
|
588
|
-
# @example
|
589
|
-
#
|
590
|
-
# [].empty?
|
591
|
-
# # => true
|
592
|
-
#
|
593
|
-
# @return [false, true] empty or not
|
594
|
-
def empty?
|
595
|
-
`return self.length == 0;`
|
596
|
-
end
|
597
|
-
|
598
|
-
# Tries to return the element as position `index`. If the index lies outside
|
599
|
-
# the array, the first form throws an IndexError exception, the second form
|
600
|
-
# returns `default`, and the third form returns the value of invoking the
|
601
|
-
# block, passing in the index. Negative values of `index` count from the end
|
602
|
-
# of the array.
|
603
|
-
#
|
604
|
-
# @example First form
|
605
|
-
#
|
606
|
-
# a = [11, 22, 33, 44]
|
607
|
-
# a.fetch 1
|
608
|
-
# # => 22
|
609
|
-
# a.fetch -1
|
610
|
-
# # => 44
|
611
|
-
#
|
612
|
-
# @example Second form
|
613
|
-
#
|
614
|
-
# a.fetch 4, 'cat'
|
615
|
-
# # => 'cat'
|
616
|
-
#
|
617
|
-
# @example Third form
|
618
|
-
#
|
619
|
-
# a.fetch 4 { |i| i * i }
|
620
|
-
# # => 16
|
621
|
-
#
|
622
|
-
# @param [Numeric] idx
|
623
|
-
# @param [Object] defaults
|
624
|
-
# @return [Object] returns result
|
625
|
-
def fetch(idx, defaults = undefined)
|
626
|
-
`var original = idx;
|
627
|
-
|
628
|
-
if (idx < 0) idx += self.length;
|
629
|
-
if (idx < 0 || idx >= self.length) {
|
630
|
-
if (defaults == undefined)
|
631
|
-
raise(eIndexError, "Array#fetch");
|
632
|
-
else if (__block__)
|
633
|
-
return #{yield `original`};
|
634
|
-
else
|
635
|
-
return defaults;
|
636
|
-
}
|
637
|
-
|
638
|
-
return self[idx];`
|
639
|
-
end
|
640
|
-
|
641
|
-
# Returns the first element, or the first `n` elements, of the array. If the
|
642
|
-
# array is empty, the first form returns `nil`, and the second form returns an
|
643
|
-
# empty array.
|
644
|
-
#
|
645
|
-
# @example
|
646
|
-
#
|
647
|
-
# a = ['q', 'r', 's', 't']
|
648
|
-
# a.first
|
649
|
-
# # => q
|
650
|
-
# a.first 2
|
651
|
-
# # => ['q', 'r']
|
652
|
-
#
|
653
|
-
# @param [Numeric] count number of elements
|
654
|
-
# @return [Object, Array] object or array of objects
|
655
|
-
def first(count = undefined)
|
656
|
-
`if (count == undefined) {
|
657
|
-
if (self.length == 0) return nil;
|
658
|
-
return self[0];
|
659
|
-
}
|
660
|
-
return self.slice(0, count);`
|
661
|
-
end
|
662
|
-
|
663
|
-
# Returns a new array that is a one-dimensional flattening of this array
|
664
|
-
# (recursively). That is, for evey element that is an array, extract its
|
665
|
-
# elements into the new array. If the optional `level` argument determines the
|
666
|
-
# level of recursion to flatten.
|
667
|
-
#
|
668
|
-
# @example
|
669
|
-
#
|
670
|
-
# s = [1, 2, 3]
|
671
|
-
# # => [a, 2, 3]
|
672
|
-
# t = [4, 5, 6, [7, 8]]
|
673
|
-
# # => [4, 5, 6, [7, 8]]
|
674
|
-
# a = [s, t, 9, 10]
|
675
|
-
# # => [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
|
676
|
-
# a.flatten
|
677
|
-
# # => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
678
|
-
# a = [1, 2, [3, [4, 5]]]
|
679
|
-
# a.flatten 1
|
680
|
-
# # => [1, 2, 3, [4, 5]]
|
681
|
-
#
|
682
|
-
# @param [Numeric] level the level to flatten
|
683
|
-
# @return [Array] returns new array
|
684
|
-
def flatten(level = undefined)
|
685
|
-
`var result = [], item;
|
686
|
-
|
687
|
-
for (var i = 0; i < self.length; i++) {
|
688
|
-
item = self[i];
|
689
|
-
|
690
|
-
if (item.o$f & T_ARRAY) {
|
691
|
-
if (level == undefined)
|
692
|
-
result = result.concat(#{`item`.flatten});
|
693
|
-
else if (level == 0)
|
694
|
-
result.push(item);
|
695
|
-
else
|
696
|
-
result = result.concat(#{`item`.flatten `level - 1`});
|
697
|
-
} else {
|
698
|
-
result.push(item);
|
699
|
-
}
|
700
|
-
}
|
701
|
-
|
702
|
-
return result;`
|
703
|
-
end
|
704
|
-
|
705
|
-
# Flattens the receiver in place. Returns `nil` if no modifications were made.
|
706
|
-
# If the optional level argument determines the level of recursion to flatten.
|
707
|
-
#
|
708
|
-
# @example
|
709
|
-
#
|
710
|
-
# a = [1, 2, [3, [4, 5]]]
|
711
|
-
# a.flatten!
|
712
|
-
# # => [1, 2, 3, 4, 5]
|
713
|
-
# a.flatten!
|
714
|
-
# # => nil
|
715
|
-
# a
|
716
|
-
# # => [1, 2, 3, 4, 5]
|
717
|
-
#
|
718
|
-
# @param [Number] level to flatten to
|
719
|
-
# @return [Array] returns the receiver
|
720
|
-
def flatten!(level = undefined)
|
721
|
-
`var length = self.length;
|
722
|
-
var result = #{self.flatten level};
|
723
|
-
self.splice(0);
|
724
|
-
|
725
|
-
for (var i = 0; i < result.length; i++) {
|
726
|
-
self.push(result[i]);
|
727
|
-
}
|
728
|
-
|
729
|
-
if (self.length == length)
|
730
|
-
return nil;
|
731
|
-
|
732
|
-
return self;`
|
733
|
-
end
|
734
|
-
|
735
|
-
def grep(pattern)
|
736
|
-
`var result = [], arg;
|
737
|
-
|
738
|
-
for (var i = 0, ii = self.length; i < ii; i++) {
|
739
|
-
arg = self[i];
|
740
|
-
|
741
|
-
if (pattern.exec(arg)) {
|
742
|
-
result.push(arg);
|
743
|
-
}
|
744
|
-
}
|
745
|
-
|
746
|
-
return result;`
|
747
|
-
end
|
748
|
-
|
749
|
-
# Returns true if the given object is present in `self`, false otherwise.
|
750
|
-
#
|
751
|
-
# @example
|
752
|
-
#
|
753
|
-
# a = ['a', 'b', 'c']
|
754
|
-
# a.include? 'b'
|
755
|
-
# # => true
|
756
|
-
# a.include? 'z'
|
757
|
-
# # => false
|
758
|
-
def include?(member)
|
759
|
-
`for (var i = 0; i < self.length; i++) {
|
760
|
-
if (#{`self[i]` == member}) {
|
761
|
-
return #{true};
|
762
|
-
}
|
763
|
-
}
|
764
|
-
|
765
|
-
return #{false};`
|
766
|
-
end
|
767
|
-
|
768
|
-
def inject(initial = undefined)
|
769
|
-
`if (initial === undefined) initial = self[0];
|
770
|
-
var result;
|
771
|
-
|
772
|
-
for (var i = 0, ii = self.length; i < ii; i++) {
|
773
|
-
initial = #{ yield `initial`, `self[i]` };
|
774
|
-
}
|
775
|
-
|
776
|
-
return initial;`
|
777
|
-
end
|
778
|
-
|
779
|
-
# Replaces the contents of `self` with the contents of `other`, truncating or
|
780
|
-
# expanding if necessary.
|
781
|
-
#
|
782
|
-
# @example
|
783
|
-
#
|
784
|
-
# a = ['a', 'b', 'c', 'd', 'e']
|
785
|
-
# a.replace ['x', 'y', 'z']
|
786
|
-
# # => ['x', 'y', 'z']
|
787
|
-
# a
|
788
|
-
# # => ['x', 'y', 'z']
|
789
|
-
#
|
790
|
-
# @param [Array] other array to replace contents with
|
791
|
-
# @return [Array] returns the receiver
|
792
|
-
def replace(other)
|
793
|
-
`self.splice(0);
|
794
|
-
|
795
|
-
for (var i = 0; i < other.length; i++) {
|
796
|
-
self.push(other[i]);
|
797
|
-
}
|
798
|
-
|
799
|
-
return self;`
|
800
|
-
end
|
801
|
-
|
802
|
-
# Inserts the given values before the element with the given index (which may
|
803
|
-
# be negative).
|
804
|
-
#
|
805
|
-
# @example
|
806
|
-
#
|
807
|
-
# a = ['a', 'b', 'c', 'd']
|
808
|
-
# a.insert 2, 99
|
809
|
-
# # => ['a', 'b', 99, 'c', 'd']
|
810
|
-
# a.insert -2, 1, 2, 3
|
811
|
-
# # => ['a', 'b', 99, 'c', 1, 2, 3, 'd']
|
812
|
-
#
|
813
|
-
# @param [Numeric] idx the index for insertion
|
814
|
-
# @param [Object] objs objects to insert
|
815
|
-
# @return [Array] returns the receiver
|
816
|
-
def insert(idx, *objs)
|
817
|
-
`var size = self.length;
|
818
|
-
|
819
|
-
if (idx < 0) idx += size;
|
820
|
-
|
821
|
-
if (idx < 0 || idx >= size)
|
822
|
-
raise(eIndexError, "out of range");
|
823
|
-
|
824
|
-
self.splice.apply(self, [idx, 0].concat(objs));
|
825
|
-
return self;`
|
826
|
-
end
|
827
|
-
|
828
|
-
# Returns a string created by converting each element of the array to a string
|
829
|
-
# seperated by `sep`.
|
830
|
-
#
|
831
|
-
# @example
|
832
|
-
#
|
833
|
-
# ['a', 'b', 'c'].join
|
834
|
-
# # => 'abc'
|
835
|
-
# ['a', 'b', 'c'].join '-'
|
836
|
-
# # => 'a-b-c'
|
837
|
-
#
|
838
|
-
# @param [String] sep the separator
|
839
|
-
# @return [String] joined string
|
840
|
-
def join(sep = '')
|
841
|
-
`var result = [];
|
842
|
-
|
843
|
-
for (var i = 0; i < self.length; i++) {
|
844
|
-
result.push(#{`self[i]`.to_s});
|
845
|
-
}
|
846
|
-
|
847
|
-
return result.join(sep);`
|
848
|
-
end
|
849
|
-
|
850
|
-
# Deletes every element of `self` for which the block evaluates to false.
|
851
|
-
#
|
852
|
-
# @example
|
853
|
-
#
|
854
|
-
# a = [1, 2, 3, 4, 5, 6]
|
855
|
-
# a.keep_if { |x| x < 4 }
|
856
|
-
# # => [1, 2, 3]
|
857
|
-
#
|
858
|
-
# @return [Array] returns the receiver
|
859
|
-
def keep_if
|
860
|
-
`for (var i = 0; i < self.length; i++) {
|
861
|
-
if (!#{yield `self[i]`}.$r) {
|
862
|
-
self.splice(i, 1);
|
863
|
-
i--;
|
864
|
-
}
|
865
|
-
}
|
866
|
-
|
867
|
-
return self;`
|
868
|
-
end
|
869
|
-
|
870
|
-
# Return the last element(s) of `self`. If the array is empty, the first form
|
871
|
-
# returns `nil`.
|
872
|
-
#
|
873
|
-
# @example
|
874
|
-
#
|
875
|
-
# a = ['w', 'x', 'y', 'z']
|
876
|
-
# a.last
|
877
|
-
# # => 'z'
|
878
|
-
# a.last 2
|
879
|
-
# # => ['y', 'z']
|
880
|
-
#
|
881
|
-
# @param [Number] count the number of items to get
|
882
|
-
# @return [Object, Array] result
|
883
|
-
def last(count = undefined)
|
884
|
-
`var size = self.length;
|
885
|
-
|
886
|
-
if (count == undefined) {
|
887
|
-
if (size == 0) return nil;
|
888
|
-
return self[size - 1];
|
889
|
-
} else {
|
890
|
-
if (count > size) count = size;
|
891
|
-
return self.slice(size - count, size);
|
892
|
-
}`
|
893
|
-
end
|
894
|
-
|
895
|
-
# Removes the last element from `self` and returns it, or `nil` if the array
|
896
|
-
# is empty. If a count is given, returns an array of the last `count`
|
897
|
-
# elements (or less).
|
898
|
-
#
|
899
|
-
# @example
|
900
|
-
#
|
901
|
-
# a = ['a', 'b', 'c', 'd']
|
902
|
-
# a.pop
|
903
|
-
# # => 'd'
|
904
|
-
# a.pop 2
|
905
|
-
# # => 'b', 'c'
|
906
|
-
# a
|
907
|
-
# # => ['a']
|
908
|
-
#
|
909
|
-
# @param [Numeric] count number to pop
|
910
|
-
# @return [Array] returns popped items
|
911
|
-
def pop(count = undefined)
|
912
|
-
`var size = self.length;
|
913
|
-
|
914
|
-
if (count == undefined) {
|
915
|
-
if (size) return self.pop();
|
916
|
-
return nil;
|
917
|
-
} else {
|
918
|
-
return self.splice(size - count, size);
|
919
|
-
}`
|
920
|
-
end
|
921
|
-
|
922
|
-
# Searches through the array whose elements are also arrays. Compares `obj`
|
923
|
-
# with the second element of each contained array using `==`. Returns the
|
924
|
-
# first contained array that matches.
|
925
|
-
#
|
926
|
-
# @example
|
927
|
-
#
|
928
|
-
# a = [[1, 'one'], [2, 'two'], [3, 'three'], ['ii', 'two']]
|
929
|
-
# a.rassoc 'two'
|
930
|
-
# # => [2, 'two']
|
931
|
-
# a.rassoc 'four'
|
932
|
-
# # => nil
|
933
|
-
#
|
934
|
-
# @param [Object] obj object to search for
|
935
|
-
# @return [Object, nil] result or nil
|
936
|
-
def rassoc(obj)
|
937
|
-
`var test;
|
938
|
-
|
939
|
-
for (var i = 0; i < self.length; i++) {
|
940
|
-
test = self[i];
|
941
|
-
if (test.o$f & T_ARRAY && test[1] != undefined) {
|
942
|
-
if (#{`test[1]` == obj}.$r) return test;
|
943
|
-
}
|
944
|
-
}
|
945
|
-
|
946
|
-
return nil;`
|
947
|
-
end
|
948
|
-
|
949
|
-
# Returns a new array containing the items in `self` for which the block is
|
950
|
-
# not true. See also `#delete_if`.
|
951
|
-
#
|
952
|
-
# @example
|
953
|
-
#
|
954
|
-
# a = [1, 2, 3, 4, 5, 6]
|
955
|
-
# a.reject { |x| x > 3 }
|
956
|
-
# # => [1, 2, 3]
|
957
|
-
# a
|
958
|
-
# # => [1, 2, 3, 4, 5, 6]
|
959
|
-
#
|
960
|
-
# @return [Array] returns the receiver
|
961
|
-
def reject
|
962
|
-
`var result = [];
|
963
|
-
|
964
|
-
for (var i = 0; i < self.length; i++) {
|
965
|
-
if (!#{yield `self[i]`}.$r) {
|
966
|
-
result.push(self[i]);
|
967
|
-
}
|
968
|
-
}
|
969
|
-
|
970
|
-
return result;`
|
971
|
-
end
|
972
|
-
|
973
|
-
# Equivalent to `#delete_if!`, deleting elements from self for which the block
|
974
|
-
# evaluates to true, but returns nil if no changes were made.
|
975
|
-
#
|
976
|
-
# @example
|
977
|
-
#
|
978
|
-
# a = [1, 2, 3, 4, 5, 6]
|
979
|
-
# a.reject! { |x| x > 3 }
|
980
|
-
# # => [1, 2, 3]
|
981
|
-
# a.reject! { |x| x > 3 }
|
982
|
-
# # => nil
|
983
|
-
# a
|
984
|
-
# # => [1, 2, 3]
|
985
|
-
#
|
986
|
-
# @return [Array] returns receiver
|
987
|
-
def reject!
|
988
|
-
`var length = self.length;
|
989
|
-
|
990
|
-
for (var i = 0; i < self.length; i++) {
|
991
|
-
if (#{yield `self[i]`}.$r) {
|
992
|
-
self.splice(i, 1);
|
993
|
-
i--;
|
994
|
-
}
|
995
|
-
}
|
996
|
-
|
997
|
-
return self.length == length ? nil : self;`
|
998
|
-
end
|
999
|
-
|
1000
|
-
# Returns a new array containing the receiver's elements in reverse order.
|
1001
|
-
#
|
1002
|
-
# @example
|
1003
|
-
#
|
1004
|
-
# ['a', 'b', 'c'].reverse
|
1005
|
-
# # => ['c', 'b', 'a']
|
1006
|
-
# [1].reverse
|
1007
|
-
# # => [1]
|
1008
|
-
#
|
1009
|
-
# @return [Array] return new array
|
1010
|
-
def reverse
|
1011
|
-
`var result = [];
|
1012
|
-
|
1013
|
-
for (var i = self.length - 1; i >= 0; i--) {
|
1014
|
-
result.push(self[i]);
|
1015
|
-
}
|
1016
|
-
|
1017
|
-
return result;`
|
1018
|
-
end
|
1019
|
-
|
1020
|
-
# Reverses the receiver in place.
|
1021
|
-
#
|
1022
|
-
# @example
|
1023
|
-
#
|
1024
|
-
# a = ['a', 'b', 'c']
|
1025
|
-
# a.reverse!
|
1026
|
-
# # => ['c', 'b', 'a']
|
1027
|
-
# a
|
1028
|
-
# # => ['c', 'b', 'a']
|
1029
|
-
#
|
1030
|
-
# @return [Array] returns the receiver
|
1031
|
-
def reverse!
|
1032
|
-
`var length = self.length / 2, tmp;
|
1033
|
-
|
1034
|
-
for (var i = 0; i < length; i++) {
|
1035
|
-
tmp = self[i];
|
1036
|
-
self[i] = self[self.length - (i + 1)];
|
1037
|
-
self[self.length - (i + 1)] = tmp;
|
1038
|
-
}
|
1039
|
-
|
1040
|
-
return self;`
|
1041
|
-
end
|
1042
|
-
|
1043
|
-
# Same as {#each}, but traverses the receiver in reverse order
|
1044
|
-
#
|
1045
|
-
# @example
|
1046
|
-
#
|
1047
|
-
# a = ['a', 'b', 'c']
|
1048
|
-
# a.reverse_each { |x| puts x }
|
1049
|
-
# # => 'c'
|
1050
|
-
# # => 'b'
|
1051
|
-
# # => 'a'
|
1052
|
-
#
|
1053
|
-
# @return [Array] returns the receiver
|
1054
|
-
def reverse_each
|
1055
|
-
`var ary = self, len = ary.length;
|
1056
|
-
|
1057
|
-
for (var i = len - 1; i >= 0; i--) {
|
1058
|
-
#{yield `ary[i]`};
|
1059
|
-
}
|
1060
|
-
|
1061
|
-
return self;`
|
1062
|
-
end
|
1063
|
-
|
1064
|
-
# Returns the index of the last object in self that is == to object. If a
|
1065
|
-
# block is given instead of an argument, returns the first object for which
|
1066
|
-
# block is true, starting from the last object. Returns `nil` if no match is
|
1067
|
-
# found.
|
1068
|
-
#
|
1069
|
-
# @example
|
1070
|
-
#
|
1071
|
-
# a = ['a', 'b', 'b', 'b', 'c']
|
1072
|
-
# a.rindex 'b'
|
1073
|
-
# # => 3
|
1074
|
-
# a.rindex 'z'
|
1075
|
-
# # => nil
|
1076
|
-
# a.rindex { |x| x == 'b' }
|
1077
|
-
# # => 3
|
1078
|
-
#
|
1079
|
-
# @return [Object, nil] returns result or nil
|
1080
|
-
def rindex(obj = undefined)
|
1081
|
-
`if (obj != undefined) {
|
1082
|
-
for (var i = self.length - 1; i >=0; i--) {
|
1083
|
-
if (#{`self[i]` == obj}.$r) {
|
1084
|
-
return i;
|
1085
|
-
}
|
1086
|
-
}
|
1087
|
-
} else if (true || __block__) {
|
1088
|
-
raise(eException, "Array#rindex needs to do block action");
|
1089
|
-
}
|
1090
|
-
|
1091
|
-
return nil;`
|
1092
|
-
end
|
1093
|
-
|
1094
|
-
# Invokes the block passing in successive elements from `self`, deleting the
|
1095
|
-
# elements for which the block returns a false value. It returns `self` if
|
1096
|
-
# changes were made, otherwise it returns `nil`.
|
1097
|
-
#
|
1098
|
-
# @example
|
1099
|
-
#
|
1100
|
-
# a = [1, 2, 3, 4, 5, 6]
|
1101
|
-
# a.select! { |x| x > 4 }
|
1102
|
-
# # => [5, 6]
|
1103
|
-
# a.select! { |x| x > 4 }
|
1104
|
-
# # => nil
|
1105
|
-
# a
|
1106
|
-
# # => [5, 6]
|
1107
|
-
#
|
1108
|
-
# @return [Array] returns receiver
|
1109
|
-
def select!
|
1110
|
-
`var length = self.length;
|
1111
|
-
|
1112
|
-
for (var i = 0; i < self.length; i++) {
|
1113
|
-
if (!#{yield `self[i]`}.$r) {
|
1114
|
-
self.splice(i, 1);
|
1115
|
-
i--;
|
1116
|
-
}
|
1117
|
-
}
|
1118
|
-
|
1119
|
-
return self.length == length ? nil : self;`
|
1120
|
-
end
|
1121
|
-
|
1122
|
-
# Returns the first element of `self` and removes it (shifting all other
|
1123
|
-
# elements down by one). Returns `nil` if the array is empty.
|
1124
|
-
#
|
1125
|
-
# If a number `n` is given, returns an array of the first n elements (or
|
1126
|
-
# less), just like `#slice` does.
|
1127
|
-
#
|
1128
|
-
# @example
|
1129
|
-
#
|
1130
|
-
# a = ['a', 'b', 'c']
|
1131
|
-
# a.shift
|
1132
|
-
# # => 'a'
|
1133
|
-
# a
|
1134
|
-
# # => ['b', 'c']
|
1135
|
-
# a = ['a', 'b', 'c']
|
1136
|
-
# a.shift 2
|
1137
|
-
# # => ['a', 'b']
|
1138
|
-
# a
|
1139
|
-
# # => ['c']
|
1140
|
-
#
|
1141
|
-
# @param [Numeric] count elements to shift
|
1142
|
-
# @return [Array] result
|
1143
|
-
def shift(count = undefined)
|
1144
|
-
`if (count != undefined)
|
1145
|
-
return self.splice(0, count);
|
1146
|
-
|
1147
|
-
if (self.length)
|
1148
|
-
return self.shift();
|
1149
|
-
|
1150
|
-
return nil;`
|
1151
|
-
end
|
1152
|
-
|
1153
|
-
# Deletes the element(s) given by an `index` (optionally with a length) or
|
1154
|
-
# by a range. Returns the deleted object(s), or `nil` if the index is out of
|
1155
|
-
# range.
|
1156
|
-
#
|
1157
|
-
# @example
|
1158
|
-
#
|
1159
|
-
# a = ['a', 'b', 'c']
|
1160
|
-
# a.slice! 1
|
1161
|
-
# # => 'b'
|
1162
|
-
# a
|
1163
|
-
# # => ['a', 'c']
|
1164
|
-
# a.slice! -1
|
1165
|
-
# # => 'c'
|
1166
|
-
# a
|
1167
|
-
# # => ['a']
|
1168
|
-
# a.slice! 100
|
1169
|
-
# # => nil
|
1170
|
-
#
|
1171
|
-
# **TODO** does not yet work with ranges
|
1172
|
-
#
|
1173
|
-
# @param [Range, Number] index to begin with
|
1174
|
-
# @param [Number] length last index
|
1175
|
-
# @return [Array, nil] result
|
1176
|
-
def slice!(index, length = nil)
|
1177
|
-
`var size = self.length;
|
1178
|
-
|
1179
|
-
if (index < 0) index += size;
|
1180
|
-
|
1181
|
-
if (index >= size || index < 0) return nil;
|
1182
|
-
|
1183
|
-
if (length != nil) {
|
1184
|
-
if (length <= 0 || length > self.length) return nil;
|
1185
|
-
return self.splice(index, index + length);
|
1186
|
-
} else {
|
1187
|
-
return self.splice(index, 1)[0];
|
1188
|
-
}`
|
1189
|
-
end
|
1190
|
-
|
1191
|
-
# Returns first `count` elements from ary.
|
1192
|
-
#
|
1193
|
-
# @example
|
1194
|
-
#
|
1195
|
-
# a = [1, 2, 3, 4, 5, 6]
|
1196
|
-
# a.take 3
|
1197
|
-
# # => [1, 2, 3]
|
1198
|
-
#
|
1199
|
-
# @return [Array] array of elements
|
1200
|
-
def take(count)
|
1201
|
-
`return self.slice(0, count);`
|
1202
|
-
end
|
1203
|
-
|
1204
|
-
|
1205
|
-
# Passes elements to the block until the block returns a false value, then
|
1206
|
-
# stops iterating and returns an array of all prior elements.
|
1207
|
-
#
|
1208
|
-
# @example
|
1209
|
-
#
|
1210
|
-
# a = [1, 2, 3, 4, 5, 6]
|
1211
|
-
# a.take_while { |i| i < 3 }
|
1212
|
-
# # => [1, 2]
|
1213
|
-
#
|
1214
|
-
# @return [Array] new array with elements
|
1215
|
-
def take_while
|
1216
|
-
`var result = [], arg;
|
1217
|
-
|
1218
|
-
for (var i = 0, ii = self.length; i < ii; i++) {
|
1219
|
-
arg = self[i];
|
1220
|
-
if (#{yield `arg`}.$r) {
|
1221
|
-
result.push(self[i]);
|
1222
|
-
} else {
|
1223
|
-
break;
|
1224
|
-
}
|
1225
|
-
}
|
1226
|
-
|
1227
|
-
return result;`
|
1228
|
-
end
|
1229
|
-
|
1230
|
-
# Returns the receiver.
|
1231
|
-
#
|
1232
|
-
# @example
|
1233
|
-
#
|
1234
|
-
# a = [1, 2, 3]
|
1235
|
-
# a.to_a
|
1236
|
-
# # => [1, 2, 3]
|
1237
|
-
#
|
1238
|
-
# @return [Array] returns the receiver
|
1239
|
-
def to_a
|
1240
|
-
self
|
1241
|
-
end
|
1242
|
-
|
1243
|
-
# Returns a new array by removing duplicate values in `self`.
|
1244
|
-
#
|
1245
|
-
# @example
|
1246
|
-
#
|
1247
|
-
# a = ['a', 'a', 'b', 'b', 'c']
|
1248
|
-
# a.uniq
|
1249
|
-
# # => ['a', 'b', 'c']
|
1250
|
-
# a
|
1251
|
-
# # => ['a', 'a', 'b', 'b', 'c']
|
1252
|
-
#
|
1253
|
-
# @return [Array]
|
1254
|
-
def uniq
|
1255
|
-
`var result = [], seen = [];
|
1256
|
-
|
1257
|
-
for (var i = 0; i < self.length; i++) {
|
1258
|
-
var test = self[i], hash = test.$h();
|
1259
|
-
if (seen.indexOf(hash) == -1) {
|
1260
|
-
seen.push(hash);
|
1261
|
-
result.push(test);
|
1262
|
-
}
|
1263
|
-
}
|
1264
|
-
|
1265
|
-
return result;`
|
1266
|
-
end
|
1267
|
-
|
1268
|
-
# Removes duplicate elements from `self`. Returns `nil` if no changes are
|
1269
|
-
# made (that is, no duplicates are found).
|
1270
|
-
#
|
1271
|
-
# @example
|
1272
|
-
#
|
1273
|
-
# a = ['a', 'a', 'b', 'b', 'c']
|
1274
|
-
# a.uniq!
|
1275
|
-
# # => ['a', 'b', 'c']
|
1276
|
-
# a.uniq!
|
1277
|
-
# # => nil
|
1278
|
-
#
|
1279
|
-
# @return [Array] returns receiver
|
1280
|
-
def uniq!
|
1281
|
-
`var seen = [], length = self.length;
|
1282
|
-
|
1283
|
-
for (var i = 0; i < self.length; i++) {
|
1284
|
-
var test = self[i], hash = test.$h();
|
1285
|
-
if (seen.indexOf(hash) == -1) {
|
1286
|
-
seen.push(hash);
|
1287
|
-
} else {
|
1288
|
-
self.splice(i, 1);
|
1289
|
-
i--;
|
1290
|
-
}
|
1291
|
-
}
|
1292
|
-
|
1293
|
-
return self.length == length ? nil : self;`
|
1294
|
-
end
|
1295
|
-
|
1296
|
-
# Prepends objects to the front of `self`, moving other elements upwards.
|
1297
|
-
#
|
1298
|
-
# @example
|
1299
|
-
#
|
1300
|
-
# a = ['b', 'c', 'd']
|
1301
|
-
# a.unshift 'a'
|
1302
|
-
# # => ['a', 'b', 'c', 'd']
|
1303
|
-
# a.unshift 1, 2
|
1304
|
-
# # => [1, 2, 'a', 'b', 'c', 'd']
|
1305
|
-
#
|
1306
|
-
# @param [Object] objs objects to add
|
1307
|
-
# @return [Array] returns the receiver
|
1308
|
-
def unshift(*objs)
|
1309
|
-
`for (var i = objs.length - 1; i >= 0; i--) {
|
1310
|
-
self.unshift(objs[i]);
|
1311
|
-
}
|
1312
|
-
|
1313
|
-
return self;`
|
1314
|
-
end
|
1315
|
-
|
1316
|
-
# Set intersection - Returns a new array containing elements common to the
|
1317
|
-
# two arrays, with no duplicates.
|
1318
|
-
#
|
1319
|
-
# @example
|
1320
|
-
#
|
1321
|
-
# [1, 1, 3, 5] & [1, 2, 3]
|
1322
|
-
# # => [1, 3]
|
1323
|
-
#
|
1324
|
-
# @param [Array] other second array to intersect
|
1325
|
-
# @return [Array] new intersected array
|
1326
|
-
def &(other)
|
1327
|
-
`var result = [], seen = [];
|
1328
|
-
|
1329
|
-
for (var i = 0; i < self.length; i++) {
|
1330
|
-
var test = self[i], hash = test.$h();
|
1331
|
-
|
1332
|
-
if (seen.indexOf(hash) == -1) {
|
1333
|
-
for (var j = 0; j < other.length; j++) {
|
1334
|
-
var test_b = other[j], hash_b = test_b.$h();
|
1335
|
-
|
1336
|
-
if ((hash == hash_b) && seen.indexOf(hash) == -1) {
|
1337
|
-
seen.push(hash);
|
1338
|
-
result.push(test);
|
1339
|
-
}
|
1340
|
-
}
|
1341
|
-
}
|
1342
|
-
}
|
1343
|
-
|
1344
|
-
return result;`
|
1345
|
-
end
|
1346
|
-
|
1347
|
-
# Repitition - When given a string argument, acts the same as {#join}.
|
1348
|
-
# Otherwise, returns a new array build by concatenating the `num` copies of
|
1349
|
-
# self.
|
1350
|
-
#
|
1351
|
-
# @example With Number
|
1352
|
-
#
|
1353
|
-
# [1, 2, 3] * 3
|
1354
|
-
# # => [1, 2, 3, 1, 2, 3, 1, 2, 3]
|
1355
|
-
#
|
1356
|
-
# @example With String
|
1357
|
-
#
|
1358
|
-
# [1, 2, 3] * ','
|
1359
|
-
# # => '1,2,3'
|
1360
|
-
#
|
1361
|
-
# @param [String, Number] num string or number used to join or concat
|
1362
|
-
# @return [String, Array] depending on argument
|
1363
|
-
def *(arg)
|
1364
|
-
`if (arg.o$f & T_STRING) {
|
1365
|
-
return #{self.join `arg`};
|
1366
|
-
} else {
|
1367
|
-
var result = [];
|
1368
|
-
for (var i = 0; i < parseInt(arg); i++) {
|
1369
|
-
result = result.concat(self);
|
1370
|
-
}
|
1371
|
-
|
1372
|
-
return result;
|
1373
|
-
}`
|
1374
|
-
end
|
1375
|
-
|
1376
|
-
# Element Reference - Returns the element at `index`, or returns a subarray
|
1377
|
-
# at index and counting for length elements, or returns a subarray if index
|
1378
|
-
# is a range. Negative indecies count backward from the end of the array (-1
|
1379
|
-
# is the last element). Returns `nil` if the index (or starting index) are
|
1380
|
-
# out of range.
|
1381
|
-
#
|
1382
|
-
# @example
|
1383
|
-
#
|
1384
|
-
# a = ['a', 'b', 'c', 'd', 'e']
|
1385
|
-
# a[2] + a[0] + a[1] # => 'cab'
|
1386
|
-
# a[6] # => nil
|
1387
|
-
# a[1, 2] # => ['b', 'c']
|
1388
|
-
# a[1..3] # => ['b', 'c', 'd']
|
1389
|
-
# a[4..7] # => ['e']
|
1390
|
-
# a[6..10] # => nil
|
1391
|
-
# a[-3, 3] # => ['c', 'd', 'e']
|
1392
|
-
# a[5] # => nil
|
1393
|
-
# a[5, 1] # => []
|
1394
|
-
# a[5..10] # => []
|
1395
|
-
#
|
1396
|
-
# **TODO** does not yet work with ranges
|
1397
|
-
#
|
1398
|
-
# @param [Range, Numeric] index to begin
|
1399
|
-
# @param [Numeric] length last index
|
1400
|
-
# @return [Array, Object, nil] result
|
1401
|
-
def [](index, length = undefined)
|
1402
|
-
`var ary = self, size = ary.length;
|
1403
|
-
|
1404
|
-
if (index < 0) index += size;
|
1405
|
-
|
1406
|
-
if (index >= size || index < 0) return nil;
|
1407
|
-
|
1408
|
-
if (length != undefined) {
|
1409
|
-
if (length <= 0) return [];
|
1410
|
-
return ary.slice(index, index + length);
|
1411
|
-
} else {
|
1412
|
-
return ary[index];
|
1413
|
-
}`
|
1414
|
-
end
|
1415
|
-
|
1416
|
-
# Element reference setting.
|
1417
|
-
#
|
1418
|
-
# **TODO** need to expand functionlaity.
|
1419
|
-
def []=(index, value)
|
1420
|
-
`if (index < 0) index += self.length;
|
1421
|
-
return self[index] = value;`
|
1422
|
-
end
|
1423
|
-
end
|
1424
|
-
|