fancy 0.3.3 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +14 -14
- data/Rakefile +16 -4
- data/bin/fancy +3 -0
- data/bin/fspec +20 -0
- data/bin/fyi +27 -11
- data/bin/ifancy +1 -1
- data/boot/fancy_ext.rb +1 -0
- data/boot/fancy_ext/block_env.rb +1 -9
- data/boot/fancy_ext/delegator.rb +64 -0
- data/boot/fancy_ext/object.rb +4 -0
- data/boot/fancy_ext/thread.rb +4 -0
- data/boot/load.rb +5 -1
- data/boot/rbx-compiler/compiler/ast.rb +0 -1
- data/boot/rbx-compiler/compiler/ast/class_def.rb +2 -0
- data/boot/rbx-compiler/compiler/ast/method_def.rb +2 -0
- data/boot/rbx-compiler/compiler/ast/node.rb +10 -0
- data/boot/rbx-compiler/compiler/ast/ruby_args.rb +12 -0
- data/boot/rbx-compiler/compiler/ast/singleton_method_def.rb +2 -0
- data/boot/rbx-compiler/parser/fancy_parser.bundle +0 -0
- data/boot/rbx-compiler/parser/lexer.lex +5 -11
- data/boot/rbx-compiler/parser/parser.rb +16 -5
- data/boot/rbx-compiler/parser/parser.y +39 -24
- data/doc/api/fancy.css +1 -1
- data/doc/api/fancy.jsonp +1 -1
- data/doc/api/fdoc.js +22 -4
- data/doc/api/index.html +5 -6
- data/doc/api/jquery-ui.min.js +401 -0
- data/doc/api/jquery.tools.min.js +192 -0
- data/doc/api/themeswitchertool.js +250 -0
- data/doc/features.md +17 -0
- data/examples/actor_bunnies.fy +32 -0
- data/examples/actors.fy +26 -0
- data/examples/actors_primitive.fy +27 -0
- data/examples/actors_ring.fy +37 -0
- data/examples/armstrong_numbers.fy +1 -1
- data/examples/array.fy +7 -9
- data/examples/async_send.fy +1 -2
- data/examples/blocks.fy +4 -4
- data/examples/call_with_receiver.fy +1 -1
- data/examples/class.fy +1 -1
- data/examples/default_args.fy +4 -1
- data/examples/define_methods.fy +2 -2
- data/examples/echo.fy +1 -1
- data/examples/factorial.fy +1 -1
- data/examples/future_composition.fy +2 -2
- data/examples/futures.fy +0 -5
- data/examples/game_of_life.fy +1 -1
- data/examples/person.fy +1 -1
- data/lib/argv.fy +7 -2
- data/lib/array.fy +109 -42
- data/lib/block.fy +39 -14
- data/lib/boot.fy +2 -0
- data/lib/class.fy +2 -0
- data/lib/compiler/ast.fy +2 -1
- data/lib/compiler/ast/assign.fy +2 -3
- data/lib/compiler/ast/async_send.fy +1 -15
- data/lib/compiler/ast/class_def.fy +2 -1
- data/lib/compiler/ast/expression_list.fy +4 -5
- data/lib/compiler/ast/future_send.fy +1 -10
- data/lib/compiler/ast/goto.fy +46 -0
- data/lib/compiler/ast/identifier.fy +9 -7
- data/lib/compiler/ast/literals.fy +8 -1
- data/lib/compiler/ast/match.fy +14 -4
- data/lib/compiler/ast/message_send.fy +34 -6
- data/lib/compiler/ast/method_def.fy +6 -6
- data/lib/compiler/ast/node.fy +3 -3
- data/lib/compiler/ast/range.fy +1 -0
- data/lib/compiler/ast/script.fy +0 -2
- data/lib/compiler/ast/singleton_method_def.fy +2 -4
- data/lib/compiler/ast/string_interpolation.fy +17 -0
- data/lib/compiler/ast/super.fy +5 -4
- data/lib/compiler/ast/try_catch.fy +8 -6
- data/lib/compiler/ast/tuple_literal.fy +3 -2
- data/lib/compiler/command.fy +0 -1
- data/lib/compiler/compiler.fy +1 -5
- data/lib/compiler/stages.fy +6 -14
- data/lib/documentation.fy +57 -46
- data/lib/enumerable.fy +257 -23
- data/lib/enumerator.fy +122 -15
- data/lib/false_class.fy +10 -1
- data/lib/fancy_spec.fy +263 -61
- data/lib/fdoc.fy +11 -25
- data/lib/fiber.fy +11 -0
- data/lib/file.fy +8 -11
- data/lib/future.fy +84 -5
- data/lib/hash.fy +65 -14
- data/lib/integer.fy +35 -0
- data/lib/iteration.fy +54 -29
- data/lib/message.fy +6 -0
- data/lib/method.fy +0 -16
- data/lib/nil_class.fy +58 -8
- data/lib/number.fy +49 -22
- data/lib/object.fy +371 -65
- data/lib/package.fy +24 -1
- data/lib/package/installer.fy +5 -9
- data/lib/package/specification.fy +2 -2
- data/lib/parser/ext/lexer.lex +15 -11
- data/lib/parser/ext/parser.y +70 -23
- data/lib/parser/methods.fy +33 -14
- data/lib/proxy.fy +33 -3
- data/lib/range.fy +28 -0
- data/lib/rbx.fy +3 -1
- data/lib/rbx/actor.fy +53 -0
- data/lib/rbx/alpha.fy +31 -0
- data/lib/rbx/array.fy +21 -12
- data/lib/rbx/bignum.fy +6 -2
- data/lib/rbx/block.fy +23 -26
- data/lib/rbx/class.fy +54 -2
- data/lib/rbx/code_loader.fy +8 -4
- data/lib/rbx/date.fy +9 -0
- data/lib/rbx/directory.fy +18 -0
- data/lib/rbx/environment_variables.fy +1 -0
- data/lib/rbx/exception.fy +9 -2
- data/lib/rbx/fiber.fy +22 -4
- data/lib/rbx/file.fy +5 -5
- data/lib/rbx/fixnum.fy +5 -0
- data/lib/rbx/float.fy +11 -3
- data/lib/rbx/hash.fy +31 -16
- data/lib/rbx/integer.fy +1 -0
- data/lib/rbx/io.fy +17 -7
- data/lib/rbx/match_data.fy +15 -4
- data/lib/rbx/method.fy +40 -7
- data/lib/rbx/name_error.fy +4 -0
- data/lib/rbx/object.fy +92 -24
- data/lib/rbx/range.fy +20 -6
- data/lib/rbx/regexp.fy +11 -3
- data/lib/rbx/string.fy +51 -1
- data/lib/rbx/stringio.fy +17 -0
- data/lib/rbx/symbol.fy +15 -1
- data/lib/rbx/system.fy +20 -2
- data/lib/rbx/tcp_server.fy +4 -1
- data/lib/rbx/tcp_socket.fy +11 -0
- data/lib/rbx/time.fy +6 -0
- data/lib/rbx/tuple.fy +14 -5
- data/lib/set.fy +144 -29
- data/lib/stack.fy +42 -11
- data/lib/string.fy +118 -8
- data/lib/struct.fy +13 -3
- data/lib/symbol.fy +21 -2
- data/lib/thread_pool.fy +2 -1
- data/lib/true_class.fy +45 -7
- data/lib/tuple.fy +27 -9
- data/lib/version.fy +2 -2
- data/ruby_lib/fancy +43 -0
- data/ruby_lib/fdoc +23 -0
- data/ruby_lib/fspec +3 -0
- data/ruby_lib/fyi +3 -0
- data/ruby_lib/ifancy +3 -0
- data/tests/argv.fy +5 -9
- data/tests/array.fy +323 -196
- data/tests/assignment.fy +29 -29
- data/tests/block.fy +72 -59
- data/tests/class.fy +227 -138
- data/tests/control_flow.fy +83 -51
- data/tests/documentation.fy +8 -8
- data/tests/enumerable.fy +8 -0
- data/tests/enumerator.fy +47 -29
- data/tests/exception.fy +49 -32
- data/tests/file.fy +28 -28
- data/tests/fixnum.fy +170 -0
- data/tests/future.fy +24 -7
- data/tests/hash.fy +55 -38
- data/tests/method.fy +50 -43
- data/tests/nil_class.fy +37 -37
- data/tests/object.fy +152 -70
- data/tests/pattern_matching.fy +67 -31
- data/tests/range.fy +6 -6
- data/tests/set.fy +101 -4
- data/tests/stack.fy +14 -5
- data/tests/string.fy +115 -61
- data/tests/stringio.fy +18 -0
- data/tests/struct.fy +27 -0
- data/tests/symbol.fy +19 -6
- data/tests/true_class.fy +34 -34
- data/tests/tuple.fy +30 -12
- metadata +103 -81
- data/boot/rbx-compiler/compiler/ast/require.rb +0 -20
- data/examples/actor.fy +0 -37
- data/examples/curl_async.fy +0 -37
- data/lib/compiler/ast/require.fy +0 -15
- data/tests/number.fy +0 -135
- data/tests/parsing/sexp.fy +0 -50
data/lib/version.fy
CHANGED
data/ruby_lib/fancy
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
#!/usr/bin/env rbx
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
base = File.dirname(__FILE__)
|
5
|
+
require File.expand_path("../boot/fancy_ext", base)
|
6
|
+
|
7
|
+
# Use the bootstrapping code loader.
|
8
|
+
require File.expand_path("../boot/load", base)
|
9
|
+
|
10
|
+
do_retry = true
|
11
|
+
begin
|
12
|
+
Fancy::CodeLoader.load_compiled_file File.expand_path("../lib/boot", base)
|
13
|
+
|
14
|
+
# Remove the bootstrapping code loader
|
15
|
+
bcl = Fancy.send :remove_const, :CodeLoader
|
16
|
+
bcl.load_compiled_file File.expand_path("../lib/rbx/code_loader", base)
|
17
|
+
|
18
|
+
# Initialize the load path
|
19
|
+
Fancy::CodeLoader.push_loadpath File.expand_path("../lib", base)
|
20
|
+
|
21
|
+
# Load compiler+eval support
|
22
|
+
Fancy::CodeLoader.load_compiled_file File.expand_path("../lib/eval", base)
|
23
|
+
|
24
|
+
# Run main
|
25
|
+
Fancy::CodeLoader.load_compiled_file File.expand_path("../lib/main", base)
|
26
|
+
|
27
|
+
rescue RuntimeError => e
|
28
|
+
if e.message =~ /File not found (.*)lib\/boot.fyc/
|
29
|
+
if do_retry
|
30
|
+
puts "Fancy hasn't been bootstrapped yet. Doing that now.\n\n"
|
31
|
+
`cd #{base} && rbx -S rake clean && rbx -S rake`
|
32
|
+
do_retry = false
|
33
|
+
retry
|
34
|
+
else
|
35
|
+
raise e
|
36
|
+
end
|
37
|
+
else
|
38
|
+
raise e
|
39
|
+
end
|
40
|
+
rescue Interrupt => e
|
41
|
+
puts e
|
42
|
+
exit
|
43
|
+
end
|
data/ruby_lib/fdoc
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
#!/usr/bin/env rbx
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
class Fancy; end
|
5
|
+
class Fancy::Parser; end
|
6
|
+
|
7
|
+
# fdoc is a documentation generator for fancy.
|
8
|
+
# This is a ruby script because we need to setup a hook
|
9
|
+
# BEFORE any fancy code is loaded. So we can create
|
10
|
+
# documentation for Fancy's builtin objects as well.
|
11
|
+
# See fdoc.
|
12
|
+
base = File.expand_path("../boot", File.dirname(__FILE__))
|
13
|
+
require File.expand_path("rbx-compiler/parser/fancy_parser", base)
|
14
|
+
require File.expand_path("rbx-compiler/compiler", base)
|
15
|
+
require File.expand_path("code_loader", base)
|
16
|
+
require File.expand_path("fancy_ext", base)
|
17
|
+
|
18
|
+
Fancy::CodeLoader.load_compiled_file File.expand_path("../lib/rbx/documentation.fyc", base)
|
19
|
+
Fancy::CodeLoader.load_compiled_file File.expand_path("../lib/fdoc_hook.fyc", base)
|
20
|
+
|
21
|
+
Fancy::CodeLoader.load_compiled_file File.expand_path("../lib/rbx.fyc", base)
|
22
|
+
|
23
|
+
Fancy::CodeLoader.load_compiled_file File.expand_path("../lib/fdoc.fyc", base)
|
data/ruby_lib/fspec
ADDED
data/ruby_lib/fyi
ADDED
data/ruby_lib/ifancy
ADDED
data/tests/argv.fy
CHANGED
@@ -1,14 +1,10 @@
|
|
1
1
|
FancySpec describe: "ARGV & predefined values" with: {
|
2
|
-
it: "
|
3
|
-
ARGV empty?
|
2
|
+
it: "has ARGV correctly defined" when: {
|
3
|
+
ARGV empty? is_not: true
|
4
4
|
}
|
5
5
|
|
6
|
-
it: "
|
7
|
-
__FILE__
|
8
|
-
__FILE__
|
9
|
-
}
|
10
|
-
|
11
|
-
it: "should have the __FILE__ value be an element in ARGV" when: {
|
12
|
-
ARGV any?: |a| { File basename(a) == (File basename(__FILE__)) } . should == true
|
6
|
+
it: "has a __FILE__ variable defined" when: {
|
7
|
+
__FILE__ is_not: nil
|
8
|
+
__FILE__ is =~ /\/argv.fy$/
|
13
9
|
}
|
14
10
|
}
|
data/tests/array.fy
CHANGED
@@ -1,374 +1,501 @@
|
|
1
1
|
FancySpec describe: Array with: {
|
2
|
-
it: "
|
2
|
+
it: "contains three number values after adding them" with: '<< when: {
|
3
3
|
arr = []
|
4
4
|
arr << 1
|
5
5
|
arr << 2
|
6
6
|
arr << 3
|
7
|
-
arr
|
8
|
-
arr size
|
7
|
+
arr is: [1,2,3]
|
8
|
+
arr size is: 3
|
9
9
|
}
|
10
10
|
|
11
|
-
it: "
|
11
|
+
it: "iterates over all elements, calling a block" with: 'each: when: {
|
12
12
|
sum = 0
|
13
13
|
[1,2,3,4,5] each: |x| { sum = sum + x }
|
14
|
-
sum
|
14
|
+
sum is: $ [1,2,3,4,5] sum
|
15
15
|
}
|
16
16
|
|
17
|
-
it: "
|
17
|
+
it: "iterates over all elements in reverse" with: 'reverse_each: when: {
|
18
|
+
sum = 0
|
19
|
+
last = 5
|
20
|
+
count = 0
|
21
|
+
[1,2,3,4,5] reverse_each: |x| {
|
22
|
+
x <= last is: true
|
23
|
+
last = x
|
24
|
+
sum = sum + x
|
25
|
+
count = count + 1
|
26
|
+
}
|
27
|
+
sum is: $ [1,2,3,4,5] sum
|
28
|
+
count is: 5
|
29
|
+
last is: 1
|
30
|
+
}
|
31
|
+
|
32
|
+
it: "iterates over all elements with their index" with: 'each_with_index: when: {
|
33
|
+
sum = 0
|
34
|
+
idx_sum = 0
|
35
|
+
[10,20,30,40,50] each_with_index: |x i| {
|
36
|
+
sum = sum + x
|
37
|
+
idx_sum = idx_sum + i
|
38
|
+
}
|
39
|
+
sum is: 150
|
40
|
+
idx_sum is: 10
|
41
|
+
}
|
42
|
+
|
43
|
+
it: "is empty after clearing it" with: 'clear when: {
|
18
44
|
arr = [1,2,3]
|
19
|
-
arr size
|
20
|
-
arr
|
45
|
+
arr size is: 3
|
46
|
+
arr is: [1,2,3]
|
21
47
|
arr clear
|
22
|
-
arr size
|
23
|
-
arr
|
48
|
+
arr size is: 0
|
49
|
+
arr is: []
|
24
50
|
}
|
25
51
|
|
26
|
-
it: "
|
27
|
-
[] empty?
|
28
|
-
[1] empty?
|
29
|
-
[1,2] empty?
|
30
|
-
[1,2,3] empty?
|
52
|
+
it: "is true for empty? when it's empty" with: 'empty? when: {
|
53
|
+
[] empty? is: true
|
54
|
+
[1] empty? is: false
|
55
|
+
[1,2] empty? is: false
|
56
|
+
[1,2,3] empty? is: false
|
31
57
|
}
|
32
58
|
|
33
|
-
it: "
|
59
|
+
it: "is an empty array after initialization" with: 'new when: {
|
34
60
|
arr = Array new
|
35
|
-
arr size
|
61
|
+
arr size is: 0
|
36
62
|
}
|
37
63
|
|
38
|
-
it: "
|
64
|
+
it: "returns the correct value via index access" with: 'at: when: {
|
39
65
|
arr = ['a, 10, "hello, world"]
|
40
|
-
arr at: 2 .
|
41
|
-
arr at: 1 .
|
42
|
-
arr at: 0 .
|
66
|
+
arr at: 2 . is: "hello, world"
|
67
|
+
arr at: 1 . is: 10
|
68
|
+
arr at: 0 . is: 'a
|
43
69
|
}
|
44
70
|
|
45
|
-
it: "
|
71
|
+
it: "sets the value for a given index" with: '[]: when: {
|
46
72
|
arr = [1,2,3]
|
47
|
-
arr
|
48
|
-
arr
|
49
|
-
arr
|
50
|
-
arr
|
51
|
-
arr
|
52
|
-
arr
|
53
|
-
arr
|
54
|
-
arr
|
73
|
+
arr[0]: 10
|
74
|
+
arr is: [10, 2, 3]
|
75
|
+
arr[-1]: 30
|
76
|
+
arr is: [10, 2, 30]
|
77
|
+
arr[1]: 20
|
78
|
+
arr is: [10,20,30]
|
79
|
+
arr[3]: 40
|
80
|
+
arr is: [10,20,30,40]
|
55
81
|
}
|
56
82
|
|
57
|
-
it: "
|
83
|
+
it: "does NOT include the items" with: 'includes?: when: {
|
58
84
|
arr = ['a, 10, "hello, world"]
|
59
|
-
arr includes?: "hello" .
|
60
|
-
arr includes?: 11 .
|
61
|
-
arr includes?: 'b .
|
85
|
+
arr includes?: "hello" . is: false
|
86
|
+
arr includes?: 11 . is: false
|
87
|
+
arr includes?: 'b . is: false
|
62
88
|
}
|
63
89
|
|
64
|
-
it: "
|
90
|
+
it: "includes the items" with: 'includes?: when: {
|
65
91
|
arr = ['a, 10, "hello, world"]
|
66
|
-
arr includes?: "hello, world" .
|
67
|
-
arr includes?: 10 .
|
68
|
-
arr includes?: 'a .
|
92
|
+
arr includes?: "hello, world" . is: true
|
93
|
+
arr includes?: 10 . is: true
|
94
|
+
arr includes?: 'a . is: true
|
69
95
|
}
|
70
96
|
|
71
|
-
it: "
|
97
|
+
it: "returns the correct index (or nil) for an element" with: 'index: when: {
|
72
98
|
arr = [1, 2, 'a, 3, 4]
|
73
|
-
arr index: 1 .
|
74
|
-
arr index: 2 .
|
75
|
-
arr index: 'a .
|
76
|
-
arr index: 3 .
|
77
|
-
arr index: 4 .
|
78
|
-
arr index: 'foo .
|
99
|
+
arr index: 1 . is: 0
|
100
|
+
arr index: 2 . is: 1
|
101
|
+
arr index: 'a . is: 2
|
102
|
+
arr index: 3 . is: 3
|
103
|
+
arr index: 4 . is: 4
|
104
|
+
arr index: 'foo . is: nil
|
79
105
|
}
|
80
106
|
|
81
|
-
it: "
|
82
|
-
['foo, 'bar, 'baz] indices
|
83
|
-
[] indices
|
84
|
-
['foo] indices
|
107
|
+
it: "returns an Array of all its indices" with: 'indices when: {
|
108
|
+
['foo, 'bar, 'baz] indices is: [0,1,2]
|
109
|
+
[] indices is: []
|
110
|
+
['foo] indices is: [0]
|
85
111
|
}
|
86
112
|
|
87
|
-
it: "
|
113
|
+
it: "returns all indices for an element as an Array" with: 'indices_of: when: {
|
88
114
|
arr = [1, 2, 'a, 3, 2, 'a]
|
89
|
-
arr indices_of: 1 .
|
90
|
-
arr indices_of: 2 .
|
91
|
-
arr indices_of: 'a .
|
92
|
-
arr indices_of: 3.
|
93
|
-
arr indices_of: 'foo .
|
115
|
+
arr indices_of: 1 . is: [0]
|
116
|
+
arr indices_of: 2 . is: [1, 4]
|
117
|
+
arr indices_of: 'a . is: [2, 5]
|
118
|
+
arr indices_of: 3. is: [3]
|
119
|
+
arr indices_of: 'foo . is: []
|
94
120
|
}
|
95
121
|
|
96
|
-
it: "
|
122
|
+
it: "finds the value" with: 'find: when: {
|
97
123
|
arr = ['foo, "bar", 'baz, 1234]
|
98
124
|
|
99
|
-
arr find: "bar" .
|
125
|
+
arr find: "bar" . is: "bar"
|
100
126
|
|
101
127
|
arr find: |x| {
|
102
128
|
x is_a?: String . if_true: {
|
103
129
|
x from: 0 to: 1 == "ba"
|
104
130
|
}
|
105
|
-
} .
|
131
|
+
} . is: "bar"
|
106
132
|
|
107
|
-
arr find: "foo" .
|
133
|
+
arr find: "foo" . is: nil
|
108
134
|
}
|
109
135
|
|
110
|
-
it: "
|
136
|
+
it: "does NOT find the value" with: 'find: when: {
|
111
137
|
arr = ['foo, "bar", 'baz, 1234]
|
112
138
|
|
113
|
-
arr find: "ba" .
|
139
|
+
arr find: "ba" . is: nil
|
114
140
|
|
115
141
|
arr find: |x| {
|
116
142
|
x is_a?: String . if_true: {
|
117
143
|
x from: 0 to: 1 == "aa"
|
118
144
|
}
|
119
|
-
} .
|
145
|
+
} . is: nil
|
120
146
|
|
121
|
-
arr find: "foobar" .
|
147
|
+
arr find: "foobar" . is: nil
|
122
148
|
}
|
123
149
|
|
124
|
-
it: "
|
150
|
+
it: "finds the value via a block" with: 'find_by: when: {
|
125
151
|
arr = [1, 2, 'foo, "yo", nil, true]
|
126
|
-
arr find_by: |x| { x is_a?: String } .
|
127
|
-
arr find_by: |x| { x is_a?: Block } .
|
152
|
+
arr find_by: |x| { x is_a?: String } . is: "yo"
|
153
|
+
arr find_by: |x| { x is_a?: Block } . is: nil
|
128
154
|
}
|
129
155
|
|
130
|
-
it: "
|
156
|
+
it: "returns the last element" with: 'last when: {
|
131
157
|
arr = [1, 2, 3, 'foo, "bar"]
|
132
|
-
arr last
|
133
|
-
(arr last == 'foo)
|
158
|
+
arr last is: "bar"
|
159
|
+
(arr last == 'foo) is: false
|
134
160
|
}
|
135
161
|
|
136
|
-
it: "
|
162
|
+
it: "returns the last n element" with: 'last: when: {
|
137
163
|
arr = [1, 2, 3, 'foo, "bar"]
|
138
|
-
arr last: 1 .
|
139
|
-
arr last: 2 .
|
140
|
-
arr last: 3 .
|
141
|
-
arr last: 4 .
|
142
|
-
arr last: 5 .
|
143
|
-
arr last: (arr size) .
|
144
|
-
arr last: (arr size + 1) .
|
164
|
+
arr last: 1 . is: [arr last]
|
165
|
+
arr last: 2 . is: ['foo, "bar"]
|
166
|
+
arr last: 3 . is: [3, 'foo, "bar"]
|
167
|
+
arr last: 4 . is: [2, 3, 'foo, "bar"]
|
168
|
+
arr last: 5 . is: [1, 2, 3, 'foo, "bar"]
|
169
|
+
arr last: (arr size) . is: arr
|
170
|
+
arr last: (arr size + 1) . is: arr
|
145
171
|
}
|
146
172
|
|
147
|
-
it: "
|
173
|
+
it: "returns an array containing the values at the given indices" with: 'values_at: when: {
|
148
174
|
arr = [1, 2, 3, 'foo, "bar"]
|
149
|
-
arr values_at: [1, 3, 4, 10] .
|
175
|
+
arr values_at: [1, 3, 4, 10] . is: [2, 'foo, "bar", nil]
|
150
176
|
}
|
151
177
|
|
152
|
-
it: "
|
178
|
+
it: "returns unique values only" with: 'uniq when: {
|
153
179
|
arr = ['foo, 'bar, "baz", 'foo, "baz", "hello", 1, 0, 0, 1, 'bar, 'foo, "hello"]
|
154
|
-
arr uniq
|
180
|
+
arr uniq is: ['foo, 'bar, "baz", "hello", 1, 0]
|
155
181
|
}
|
156
182
|
|
157
|
-
it: "
|
183
|
+
it: "prepends self to another array" with: '>> when: {
|
158
184
|
arr1 = ['foo, 'bar, 'baz]
|
159
185
|
arr2 = [1, 2, 3]
|
160
|
-
(arr1 >> arr2)
|
186
|
+
(arr1 >> arr2) is: ['foo, 'bar, 'baz, 1, 2, 3]
|
161
187
|
}
|
162
188
|
|
163
|
-
it: "
|
189
|
+
it: "returns an element by the []-operator" with: '[] when: {
|
164
190
|
arr = ['foo, 'bar, 'baz]
|
165
|
-
arr[0]
|
166
|
-
arr[1]
|
167
|
-
arr[2]
|
191
|
+
arr[0] is: 'foo
|
192
|
+
arr[1] is: 'bar
|
193
|
+
arr[2] is: 'baz
|
168
194
|
}
|
169
195
|
|
170
|
-
it: "
|
196
|
+
it: "returns a sub-array by the []-operator" with: '[] when: {
|
171
197
|
arr = ['foo, 'bar, 'baz]
|
172
|
-
arr[[0,2]]
|
173
|
-
arr[[0,1]]
|
174
|
-
arr[[0,1]]
|
175
|
-
arr[[0,0]]
|
176
|
-
arr[[1,1]]
|
177
|
-
arr[[2,2]]
|
178
|
-
arr[[0,-1]]
|
179
|
-
arr[[-1,-1]]
|
180
|
-
arr[[-2,-1]]
|
181
|
-
arr[[-2,-1]]
|
182
|
-
}
|
183
|
-
|
184
|
-
it: "
|
198
|
+
arr[[0,2]] is: arr
|
199
|
+
arr[[0,1]] is: ['foo, 'bar]
|
200
|
+
arr[[0,1]] is: (arr from: 0 to: 1)
|
201
|
+
arr[[0,0]] is: [arr[0]]
|
202
|
+
arr[[1,1]] is: [arr[1]]
|
203
|
+
arr[[2,2]] is: [arr[2]]
|
204
|
+
arr[[0,-1]] is: arr
|
205
|
+
arr[[-1,-1]] is: [arr last]
|
206
|
+
arr[[-2,-1]] is: ['bar, 'baz]
|
207
|
+
arr[[-2,-1]] is: (arr last: 2)
|
208
|
+
}
|
209
|
+
|
210
|
+
it: "joins all elements with a string to a new string" with: 'join: when: {
|
185
211
|
arr = ['foo, 'bar, 'baz]
|
186
|
-
arr join: "," .
|
212
|
+
arr join: "," . is: "foo,bar,baz"
|
187
213
|
}
|
188
214
|
|
189
|
-
it: "
|
215
|
+
it: "joins all elements with the empty string to a new string" with: 'join when: {
|
190
216
|
arr = ['foo, 'bar, 'baz]
|
191
|
-
arr join
|
217
|
+
arr join is: "foobarbaz"
|
192
218
|
}
|
193
219
|
|
194
|
-
it: "
|
220
|
+
it: "removes an element at a given index" with: 'remove_at: when: {
|
195
221
|
arr = [1, 'foo, 2, 'bar, 3, 'baz]
|
196
222
|
# remove_at: returns the removed element
|
197
|
-
arr remove_at: 1 .
|
198
|
-
arr
|
223
|
+
arr remove_at: 1 . is: 'foo
|
224
|
+
arr is: [1, 2, 'bar, 3, 'baz]
|
199
225
|
arr remove_at: 3
|
200
|
-
arr
|
226
|
+
arr is: [1, 2, 'bar, 'baz]
|
201
227
|
arr remove_at: [2, 3]
|
202
|
-
arr
|
228
|
+
arr is: [1, 2]
|
203
229
|
arr = [1, 'hello, 2, 'world]
|
204
230
|
# remove_at: returns the removed elements as an array
|
205
231
|
# if it was passed an array of indexes
|
206
|
-
arr remove_at: [0, 2, 3] .
|
207
|
-
arr
|
232
|
+
arr remove_at: [0, 2, 3] . is: [1, 2, 'world]
|
233
|
+
arr is: ['hello]
|
208
234
|
}
|
209
235
|
|
210
|
-
it: "
|
236
|
+
it: "removes all occurances of a given object in-place" with: 'remove: when: {
|
211
237
|
arr = [1, 2, 'foo, 3, 'foo, 2, 4]
|
212
238
|
arr remove: 2
|
213
|
-
arr
|
239
|
+
arr is: [1, 'foo, 3, 'foo, 4]
|
214
240
|
arr remove: 'foo
|
215
|
-
arr
|
241
|
+
arr is: [1, 3, 4]
|
216
242
|
}
|
217
243
|
|
218
|
-
it: "
|
244
|
+
it: "removes all elements with the given Array of indices" with: 'remove_at: when: {
|
219
245
|
arr = [1, 2, 'foo, 3, 'foo, 2, 4]
|
220
246
|
arr remove_at: [0, 2, 4]
|
221
|
-
arr
|
247
|
+
arr is: [2, 3, 2, 4]
|
222
248
|
}
|
223
249
|
|
224
|
-
it: "
|
250
|
+
it: "removes all elements that meet a given condition block" with: 'remove_if: when: {
|
225
251
|
arr = [1, 2, 3, 2, 5, 4]
|
226
252
|
arr remove_if: |x| { x < 3 }
|
227
|
-
arr
|
253
|
+
arr is: [3, 5, 4]
|
228
254
|
}
|
229
255
|
|
230
|
-
it: "
|
231
|
-
['foo, nil, 'bar, nil, 'baz] compact
|
232
|
-
[] compact
|
233
|
-
[nil] compact
|
234
|
-
['foo] compact
|
256
|
+
it: "removes all nil-value entries when calling compact" with: 'compact when: {
|
257
|
+
['foo, nil, 'bar, nil, 'baz] compact is: ['foo, 'bar, 'baz]
|
258
|
+
[] compact is: []
|
259
|
+
[nil] compact is: []
|
260
|
+
['foo] compact is: ['foo]
|
235
261
|
}
|
236
262
|
|
237
|
-
it: "
|
263
|
+
it: "removes all nil-value entries in place when calling compact!" with: 'compact! when: {
|
238
264
|
arr = ['foo, nil, 'bar, nil, 'baz]
|
239
|
-
arr compact!
|
240
|
-
arr
|
241
|
-
[] compact!
|
242
|
-
[nil] compact!
|
243
|
-
['foo] compact!
|
265
|
+
arr compact! is: ['foo, 'bar, 'baz]
|
266
|
+
arr is: ['foo, 'bar, 'baz]
|
267
|
+
[] compact! is: []
|
268
|
+
[nil] compact! is: []
|
269
|
+
['foo] compact! is: ['foo]
|
244
270
|
}
|
245
271
|
|
246
|
-
it: "
|
272
|
+
it: "removes all values that meet a condition" with: 'reject!: when: {
|
247
273
|
arr = ['foo, 'bar, 1, 2, 'baz, "hello"]
|
248
|
-
arr reject!: |x| { x is_a?: String } .
|
249
|
-
arr
|
274
|
+
arr reject!: |x| { x is_a?: String } . is: ['foo, 'bar, 1, 2, 'baz]
|
275
|
+
arr is: ['foo, 'bar, 1, 2, 'baz]
|
250
276
|
arr reject!: |x| { x is_a?: Fixnum }
|
251
|
-
arr
|
277
|
+
arr is: ['foo, 'bar, 'baz]
|
252
278
|
}
|
253
279
|
|
254
|
-
it: "
|
280
|
+
it: "removes all values that don't meet a condition" with: 'select!: when: {
|
255
281
|
arr = ['foo, 'bar, 1, 2, 'baz, "hello"]
|
256
282
|
arr select!: |x| { x is_a?: Fixnum }
|
257
|
-
arr
|
283
|
+
arr is: [1, 2]
|
258
284
|
}
|
259
285
|
|
260
|
-
it: "
|
286
|
+
it: "returns a new Array with all elements that meet a given condition" with: 'select: when: {
|
261
287
|
arr = [1, 2, "foo", 'bar, 120]
|
262
|
-
arr select: |x| { x is_a?: Fixnum } .
|
263
|
-
arr
|
288
|
+
arr select: |x| { x is_a?: Fixnum } . is: [1,2,120]
|
289
|
+
arr is: [1, 2, "foo", 'bar, 120] # select: is non-destructive
|
264
290
|
}
|
265
291
|
|
266
|
-
it: "
|
267
|
-
[1,2,3,4] max
|
268
|
-
[1,5,-3,2,6,-4,-2] max
|
292
|
+
it: "returns the maximum value in the list" with: 'max when: {
|
293
|
+
[1,2,3,4] max is: 4
|
294
|
+
[1,5,-3,2,6,-4,-2] max is: 6
|
269
295
|
}
|
270
296
|
|
271
|
-
it: "
|
272
|
-
[1,2,3,4] min
|
273
|
-
[1,5,-3,2,6,-4,-2] min
|
297
|
+
it: "prints the minimum value in the list" with: 'min when: {
|
298
|
+
[1,2,3,4] min is: 1
|
299
|
+
[1,5,-3,2,6,-4,-2] min is: -4
|
274
300
|
}
|
275
301
|
|
276
|
-
it: "
|
277
|
-
[1,2,3,4,5] * 2
|
278
|
-
[1,2,3] * 2
|
279
|
-
['a,'b,'c] * 4
|
302
|
+
it: "returns an Array containing the elements n times." with: '* when: {
|
303
|
+
[1,2,3,4,5] * 2 is: [1,2,3,4,5,1,2,3,4,5]
|
304
|
+
[1,2,3] * 2 is: ([1,2,3] + [1,2,3])
|
305
|
+
['a,'b,'c] * 4 is: ['a,'b,'c, 'a,'b,'c, 'a,'b,'c, 'a,'b,'c]
|
280
306
|
}
|
281
307
|
|
282
|
-
it: "
|
283
|
-
([1,2,3,4] + [-1,-2,-3,-4])
|
308
|
+
it: "returns the concatenation of two Arrays" with: '+ when: {
|
309
|
+
([1,2,3,4] + [-1,-2,-3,-4]) is: [1,2,3,4,-1,-2,-3,-4]
|
284
310
|
}
|
285
311
|
|
286
|
-
it: "
|
287
|
-
[1,2,3,4] all?: |x| { x < 5 } .
|
288
|
-
[1,2,3,4] all?: |x| { x > 0 } .
|
289
|
-
[1,2,3,4] all?: |x| { x > 4 } .
|
312
|
+
it: "returns true for all elements" with: 'all?: when: {
|
313
|
+
[1,2,3,4] all?: |x| { x < 5 } . is: true
|
314
|
+
[1,2,3,4] all?: |x| { x > 0 } . is: true
|
315
|
+
[1,2,3,4] all?: |x| { x > 4 } . is: false
|
290
316
|
}
|
291
317
|
|
292
|
-
it: "
|
293
|
-
[1,2,3,4] any?: |x| { x > 3 } .
|
294
|
-
[1,2,3,4] any?: |x| { x < 4 } .
|
295
|
-
[1,2,3,4] any?: |x| { x > 4 } .
|
318
|
+
it: "returns true for any elements" with: 'any?: when: {
|
319
|
+
[1,2,3,4] any?: |x| { x > 3 } . is: true
|
320
|
+
[1,2,3,4] any?: |x| { x < 4 } . is: true
|
321
|
+
[1,2,3,4] any?: |x| { x > 4 } . is: false
|
296
322
|
}
|
297
323
|
|
298
|
-
it: "
|
299
|
-
["yooo",2,3,1,'foo,"bar"] select_with_index: |x i| { x is_a?: Fixnum } .
|
324
|
+
it: "is selected from it with each index" with: 'select_with_index: when: {
|
325
|
+
["yooo",2,3,1,'foo,"bar"] select_with_index: |x i| { x is_a?: Fixnum } . is: [[2,1], [3,2], [1,3]]
|
300
326
|
}
|
301
327
|
|
302
|
-
it: "
|
303
|
-
[1,2,3,4] rest
|
304
|
-
[] rest
|
305
|
-
100 upto: 1000 . rest
|
328
|
+
it: "returns its remaining (all but the first) elements as a new Array" with: 'rest when: {
|
329
|
+
[1,2,3,4] rest is: [2,3,4]
|
330
|
+
[] rest is: []
|
331
|
+
100 upto: 1000 . rest is: (101 upto: 1000)
|
306
332
|
}
|
307
333
|
|
308
|
-
it: "
|
309
|
-
[1,2,3] to_s
|
334
|
+
it: "returns itself as a string" with: 'to_s when: {
|
335
|
+
[1,2,3] to_s is: "123"
|
310
336
|
}
|
311
337
|
|
312
|
-
it: "
|
338
|
+
it: "calls a given block between calling the each block" with: 'each:in_between: when: {
|
313
339
|
arr = []
|
314
340
|
[1,2,3] each: |x| { arr << x } in_between: { arr << "-" }
|
315
|
-
arr
|
341
|
+
arr is: [1, "-", 2, "-", 3]
|
316
342
|
|
317
343
|
str = ""
|
318
344
|
['foo, 'bar, 'baz] each: |x| { str = str ++ (x to_s) } in_between: { str = str ++ " " }
|
319
|
-
str
|
345
|
+
str is: "foo bar baz"
|
320
346
|
}
|
321
347
|
|
322
|
-
it: "
|
348
|
+
it: "returns the reduced value for a given block and initial value" with: 'reduce:init_val: when: {
|
323
349
|
arr = 1 upto: 10
|
324
|
-
arr sum
|
325
|
-
arr product
|
326
|
-
arr to_s
|
350
|
+
arr sum is: (arr reduce: '+ init_val: 0)
|
351
|
+
arr product is: (arr reduce: '* init_val: 1)
|
352
|
+
arr to_s is: (arr reduce: '++ init_val: "")
|
327
353
|
}
|
328
354
|
|
329
|
-
it: "
|
330
|
-
[1,2,3] reverse
|
331
|
-
1 upto: 10 . reverse
|
355
|
+
it: "returns the reverse of itself" with: 'reverse when: {
|
356
|
+
[1,2,3] reverse is: [3,2,1]
|
357
|
+
1 upto: 10 . reverse is: (10 downto: 1)
|
332
358
|
}
|
333
359
|
|
334
|
-
it: "
|
360
|
+
it: "inverses in-place" with: 'reverse! when: {
|
335
361
|
arr = [1,2,3]
|
336
|
-
arr reverse!
|
337
|
-
arr
|
362
|
+
arr reverse! is: [3,2,1]
|
363
|
+
arr is: [3,2,1]
|
338
364
|
}
|
339
365
|
|
340
|
-
it: "
|
341
|
-
1 upto: 15 . take_while: |x| { x < 10 } .
|
366
|
+
it: "takes elements from itself as long a block yields true" with: 'take_while: when: {
|
367
|
+
1 upto: 15 . take_while: |x| { x < 10 } . is: (1 upto: 9)
|
342
368
|
}
|
343
369
|
|
344
370
|
|
345
|
-
it: "
|
346
|
-
1 upto: 15 . drop_while: |x| { x < 10 } .
|
371
|
+
it: "drops elements from itself as long a block yields true" with: 'drop_while: when: {
|
372
|
+
1 upto: 15 . drop_while: |x| { x < 10 } . is: (10 upto: 15)
|
347
373
|
}
|
348
374
|
|
349
|
-
it: "
|
375
|
+
it: "partitions an array via a given block" with: 'partition_by: when: {
|
350
376
|
arr = [1,2,2,3,3,3,4,4,4,4,5]
|
351
|
-
arr partition_by: 'identity .
|
352
|
-
arr partition_by: @{== 2} .
|
377
|
+
arr partition_by: 'identity . is: [[1], [2,2], [3,3,3], [4,4,4,4], [5]]
|
378
|
+
arr partition_by: @{== 2} . is: [[1], [2,2], [3,3,3,4,4,4,4,5]]
|
353
379
|
}
|
354
380
|
|
355
|
-
it: "
|
381
|
+
it: "removes the first value" with: 'shift when: {
|
356
382
|
a = [1,2,3]
|
357
|
-
a shift
|
358
|
-
a
|
383
|
+
a shift is: 1
|
384
|
+
a is: [2,3]
|
359
385
|
|
360
386
|
a = []
|
361
|
-
a shift
|
362
|
-
a
|
387
|
+
a shift is: nil
|
388
|
+
a is: []
|
363
389
|
}
|
364
390
|
|
365
|
-
it: "
|
391
|
+
it: "appends a value at the front" with: 'unshift: when: {
|
366
392
|
a = []
|
367
|
-
a unshift: 1 .
|
368
|
-
a
|
393
|
+
a unshift: 1 . is: a # is return self
|
394
|
+
a is: [1]
|
369
395
|
|
370
396
|
a = [1,2,3]
|
371
|
-
a unshift: (a shift) .
|
372
|
-
a
|
397
|
+
a unshift: (a shift) . is: a
|
398
|
+
a is: [1,2,3]
|
399
|
+
}
|
400
|
+
|
401
|
+
it: "compares 2 arrays without regard to order" with: '=? when: {
|
402
|
+
[1,2,3] =? [2,1,3] is: true
|
403
|
+
[1,2,3,4] =? [2,1,4,3] is: true
|
404
|
+
[1,2] =? [1,2,3] is: false
|
405
|
+
[1,2] =? [2] is: false
|
406
|
+
[2] =? [1,2] is: false
|
407
|
+
[] =? [] is: true
|
408
|
+
[1] =? [1] is: true
|
409
|
+
}
|
410
|
+
|
411
|
+
it: "returns the first element" with: 'first when: {
|
412
|
+
[1,2] first is: 1
|
413
|
+
[1] first is: 1
|
414
|
+
[] first is: nil
|
415
|
+
}
|
416
|
+
|
417
|
+
it: "returns the second element" with: 'second when: {
|
418
|
+
[1,2,3] second is: 2
|
419
|
+
[1,2] second is: 2
|
420
|
+
[1] second is: nil
|
421
|
+
[] second is: nil
|
422
|
+
}
|
423
|
+
|
424
|
+
it: "returns the third element" with: 'third when: {
|
425
|
+
[1,2,3,4] third is: 3
|
426
|
+
[1,2,3] third is: 3
|
427
|
+
[1,2] third is: nil
|
428
|
+
[1] third is: nil
|
429
|
+
[] third is: nil
|
430
|
+
}
|
431
|
+
|
432
|
+
it: "returns the fourth element" with: 'fourth when: {
|
433
|
+
[1,2,3,4,5] fourth is: 4
|
434
|
+
[1,2,3,4] fourth is: 4
|
435
|
+
[1,2,3] fourth is: nil
|
436
|
+
[1,2] fourth is: nil
|
437
|
+
[1] fourth is: nil
|
438
|
+
[] fourth is: nil
|
439
|
+
}
|
440
|
+
|
441
|
+
it: "returns all but the first element" with: 'rest when: {
|
442
|
+
[1,2,3,4] rest is: [2,3,4]
|
443
|
+
[2,3] rest is: [3]
|
444
|
+
[1] rest is: []
|
445
|
+
[] rest is: []
|
446
|
+
}
|
447
|
+
|
448
|
+
it: "returns a clone" with: 'clone when: {
|
449
|
+
[1,2,3] clone is: [1,2,3]
|
450
|
+
[1] clone is: [1]
|
451
|
+
[] clone is: []
|
452
|
+
}
|
453
|
+
|
454
|
+
it: "calculates the sum for an Array of numbers" with: 'sum when: {
|
455
|
+
[1,2,3] sum is: 6
|
456
|
+
[1] sum is: 1
|
457
|
+
[] sum is: 0
|
458
|
+
}
|
459
|
+
|
460
|
+
it: "calculates the product for an Array of numbers" with: 'product when: {
|
461
|
+
[1,2,3,4] product is: 24
|
462
|
+
[1] product is: 1
|
463
|
+
[] product is: 1
|
464
|
+
}
|
465
|
+
|
466
|
+
it: "calculates the average for an Array of numbers" with: 'average when: {
|
467
|
+
[] average is: 0
|
468
|
+
[-1] average is: -1
|
469
|
+
[0] average is: 0
|
470
|
+
[1] average is: 1
|
471
|
+
[1,2] average is: 1.5
|
472
|
+
[1,2,3] average is: 2
|
473
|
+
[1,2,3,4] average is: 2.5
|
474
|
+
}
|
475
|
+
|
476
|
+
it: "sorts the array" with: 'sort when: {
|
477
|
+
arr = [1,5,4,2,3]
|
478
|
+
arr sort is: [1,2,3,4,5]
|
479
|
+
arr is: [1,5,4,2,3]
|
480
|
+
}
|
481
|
+
|
482
|
+
it: "sorts the array with a given comparison block" with: 'sort_by: when: {
|
483
|
+
arr = [1,5,4,2,3]
|
484
|
+
sorted = [1,2,3,4,5]
|
485
|
+
arr sort_by: |a b| { a <=> b } . is: sorted
|
486
|
+
arr is: [1,5,4,2,3]
|
487
|
+
|
488
|
+
arr = [(1,2), (0,1), (3,0)]
|
489
|
+
sorted = [(3,0), (0,1), (1,2)]
|
490
|
+
arr sort_by: |a b| { a second <=> (b second) } . is: sorted
|
491
|
+
arr sort_by: 'second . is: sorted
|
492
|
+
}
|
493
|
+
|
494
|
+
it: "returns the array in groups of 3" with: 'in_groups_of: when: {
|
495
|
+
['a,'b,'c] in_groups_of: 1 . is: [['a],['b],['c]]
|
496
|
+
array = 1 upto: 10
|
497
|
+
array in_groups_of: 3 . is: [[1,2,3], [4,5,6], [7,8,9], [10]]
|
498
|
+
|
499
|
+
(20,30,40) in_groups_of: 2 . is: [[20,30], [40]]
|
373
500
|
}
|
374
501
|
}
|