fancy 0.3.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (182) hide show
  1. data/README.md +14 -14
  2. data/Rakefile +16 -4
  3. data/bin/fancy +3 -0
  4. data/bin/fspec +20 -0
  5. data/bin/fyi +27 -11
  6. data/bin/ifancy +1 -1
  7. data/boot/fancy_ext.rb +1 -0
  8. data/boot/fancy_ext/block_env.rb +1 -9
  9. data/boot/fancy_ext/delegator.rb +64 -0
  10. data/boot/fancy_ext/object.rb +4 -0
  11. data/boot/fancy_ext/thread.rb +4 -0
  12. data/boot/load.rb +5 -1
  13. data/boot/rbx-compiler/compiler/ast.rb +0 -1
  14. data/boot/rbx-compiler/compiler/ast/class_def.rb +2 -0
  15. data/boot/rbx-compiler/compiler/ast/method_def.rb +2 -0
  16. data/boot/rbx-compiler/compiler/ast/node.rb +10 -0
  17. data/boot/rbx-compiler/compiler/ast/ruby_args.rb +12 -0
  18. data/boot/rbx-compiler/compiler/ast/singleton_method_def.rb +2 -0
  19. data/boot/rbx-compiler/parser/fancy_parser.bundle +0 -0
  20. data/boot/rbx-compiler/parser/lexer.lex +5 -11
  21. data/boot/rbx-compiler/parser/parser.rb +16 -5
  22. data/boot/rbx-compiler/parser/parser.y +39 -24
  23. data/doc/api/fancy.css +1 -1
  24. data/doc/api/fancy.jsonp +1 -1
  25. data/doc/api/fdoc.js +22 -4
  26. data/doc/api/index.html +5 -6
  27. data/doc/api/jquery-ui.min.js +401 -0
  28. data/doc/api/jquery.tools.min.js +192 -0
  29. data/doc/api/themeswitchertool.js +250 -0
  30. data/doc/features.md +17 -0
  31. data/examples/actor_bunnies.fy +32 -0
  32. data/examples/actors.fy +26 -0
  33. data/examples/actors_primitive.fy +27 -0
  34. data/examples/actors_ring.fy +37 -0
  35. data/examples/armstrong_numbers.fy +1 -1
  36. data/examples/array.fy +7 -9
  37. data/examples/async_send.fy +1 -2
  38. data/examples/blocks.fy +4 -4
  39. data/examples/call_with_receiver.fy +1 -1
  40. data/examples/class.fy +1 -1
  41. data/examples/default_args.fy +4 -1
  42. data/examples/define_methods.fy +2 -2
  43. data/examples/echo.fy +1 -1
  44. data/examples/factorial.fy +1 -1
  45. data/examples/future_composition.fy +2 -2
  46. data/examples/futures.fy +0 -5
  47. data/examples/game_of_life.fy +1 -1
  48. data/examples/person.fy +1 -1
  49. data/lib/argv.fy +7 -2
  50. data/lib/array.fy +109 -42
  51. data/lib/block.fy +39 -14
  52. data/lib/boot.fy +2 -0
  53. data/lib/class.fy +2 -0
  54. data/lib/compiler/ast.fy +2 -1
  55. data/lib/compiler/ast/assign.fy +2 -3
  56. data/lib/compiler/ast/async_send.fy +1 -15
  57. data/lib/compiler/ast/class_def.fy +2 -1
  58. data/lib/compiler/ast/expression_list.fy +4 -5
  59. data/lib/compiler/ast/future_send.fy +1 -10
  60. data/lib/compiler/ast/goto.fy +46 -0
  61. data/lib/compiler/ast/identifier.fy +9 -7
  62. data/lib/compiler/ast/literals.fy +8 -1
  63. data/lib/compiler/ast/match.fy +14 -4
  64. data/lib/compiler/ast/message_send.fy +34 -6
  65. data/lib/compiler/ast/method_def.fy +6 -6
  66. data/lib/compiler/ast/node.fy +3 -3
  67. data/lib/compiler/ast/range.fy +1 -0
  68. data/lib/compiler/ast/script.fy +0 -2
  69. data/lib/compiler/ast/singleton_method_def.fy +2 -4
  70. data/lib/compiler/ast/string_interpolation.fy +17 -0
  71. data/lib/compiler/ast/super.fy +5 -4
  72. data/lib/compiler/ast/try_catch.fy +8 -6
  73. data/lib/compiler/ast/tuple_literal.fy +3 -2
  74. data/lib/compiler/command.fy +0 -1
  75. data/lib/compiler/compiler.fy +1 -5
  76. data/lib/compiler/stages.fy +6 -14
  77. data/lib/documentation.fy +57 -46
  78. data/lib/enumerable.fy +257 -23
  79. data/lib/enumerator.fy +122 -15
  80. data/lib/false_class.fy +10 -1
  81. data/lib/fancy_spec.fy +263 -61
  82. data/lib/fdoc.fy +11 -25
  83. data/lib/fiber.fy +11 -0
  84. data/lib/file.fy +8 -11
  85. data/lib/future.fy +84 -5
  86. data/lib/hash.fy +65 -14
  87. data/lib/integer.fy +35 -0
  88. data/lib/iteration.fy +54 -29
  89. data/lib/message.fy +6 -0
  90. data/lib/method.fy +0 -16
  91. data/lib/nil_class.fy +58 -8
  92. data/lib/number.fy +49 -22
  93. data/lib/object.fy +371 -65
  94. data/lib/package.fy +24 -1
  95. data/lib/package/installer.fy +5 -9
  96. data/lib/package/specification.fy +2 -2
  97. data/lib/parser/ext/lexer.lex +15 -11
  98. data/lib/parser/ext/parser.y +70 -23
  99. data/lib/parser/methods.fy +33 -14
  100. data/lib/proxy.fy +33 -3
  101. data/lib/range.fy +28 -0
  102. data/lib/rbx.fy +3 -1
  103. data/lib/rbx/actor.fy +53 -0
  104. data/lib/rbx/alpha.fy +31 -0
  105. data/lib/rbx/array.fy +21 -12
  106. data/lib/rbx/bignum.fy +6 -2
  107. data/lib/rbx/block.fy +23 -26
  108. data/lib/rbx/class.fy +54 -2
  109. data/lib/rbx/code_loader.fy +8 -4
  110. data/lib/rbx/date.fy +9 -0
  111. data/lib/rbx/directory.fy +18 -0
  112. data/lib/rbx/environment_variables.fy +1 -0
  113. data/lib/rbx/exception.fy +9 -2
  114. data/lib/rbx/fiber.fy +22 -4
  115. data/lib/rbx/file.fy +5 -5
  116. data/lib/rbx/fixnum.fy +5 -0
  117. data/lib/rbx/float.fy +11 -3
  118. data/lib/rbx/hash.fy +31 -16
  119. data/lib/rbx/integer.fy +1 -0
  120. data/lib/rbx/io.fy +17 -7
  121. data/lib/rbx/match_data.fy +15 -4
  122. data/lib/rbx/method.fy +40 -7
  123. data/lib/rbx/name_error.fy +4 -0
  124. data/lib/rbx/object.fy +92 -24
  125. data/lib/rbx/range.fy +20 -6
  126. data/lib/rbx/regexp.fy +11 -3
  127. data/lib/rbx/string.fy +51 -1
  128. data/lib/rbx/stringio.fy +17 -0
  129. data/lib/rbx/symbol.fy +15 -1
  130. data/lib/rbx/system.fy +20 -2
  131. data/lib/rbx/tcp_server.fy +4 -1
  132. data/lib/rbx/tcp_socket.fy +11 -0
  133. data/lib/rbx/time.fy +6 -0
  134. data/lib/rbx/tuple.fy +14 -5
  135. data/lib/set.fy +144 -29
  136. data/lib/stack.fy +42 -11
  137. data/lib/string.fy +118 -8
  138. data/lib/struct.fy +13 -3
  139. data/lib/symbol.fy +21 -2
  140. data/lib/thread_pool.fy +2 -1
  141. data/lib/true_class.fy +45 -7
  142. data/lib/tuple.fy +27 -9
  143. data/lib/version.fy +2 -2
  144. data/ruby_lib/fancy +43 -0
  145. data/ruby_lib/fdoc +23 -0
  146. data/ruby_lib/fspec +3 -0
  147. data/ruby_lib/fyi +3 -0
  148. data/ruby_lib/ifancy +3 -0
  149. data/tests/argv.fy +5 -9
  150. data/tests/array.fy +323 -196
  151. data/tests/assignment.fy +29 -29
  152. data/tests/block.fy +72 -59
  153. data/tests/class.fy +227 -138
  154. data/tests/control_flow.fy +83 -51
  155. data/tests/documentation.fy +8 -8
  156. data/tests/enumerable.fy +8 -0
  157. data/tests/enumerator.fy +47 -29
  158. data/tests/exception.fy +49 -32
  159. data/tests/file.fy +28 -28
  160. data/tests/fixnum.fy +170 -0
  161. data/tests/future.fy +24 -7
  162. data/tests/hash.fy +55 -38
  163. data/tests/method.fy +50 -43
  164. data/tests/nil_class.fy +37 -37
  165. data/tests/object.fy +152 -70
  166. data/tests/pattern_matching.fy +67 -31
  167. data/tests/range.fy +6 -6
  168. data/tests/set.fy +101 -4
  169. data/tests/stack.fy +14 -5
  170. data/tests/string.fy +115 -61
  171. data/tests/stringio.fy +18 -0
  172. data/tests/struct.fy +27 -0
  173. data/tests/symbol.fy +19 -6
  174. data/tests/true_class.fy +34 -34
  175. data/tests/tuple.fy +30 -12
  176. metadata +103 -81
  177. data/boot/rbx-compiler/compiler/ast/require.rb +0 -20
  178. data/examples/actor.fy +0 -37
  179. data/examples/curl_async.fy +0 -37
  180. data/lib/compiler/ast/require.fy +0 -15
  181. data/tests/number.fy +0 -135
  182. data/tests/parsing/sexp.fy +0 -50
@@ -0,0 +1,32 @@
1
+ # Inspired by actors example at http://www.artima.com/weblogs/viewpost.jsp?thread=328540
2
+
3
+ class Bunny {
4
+ def initialize: @id {
5
+ self @@ hop
6
+ }
7
+ def hop {
8
+ self ++ " " print
9
+ self @@ hop
10
+ Thread sleep: 0.5
11
+ }
12
+ def stop: respond_to {
13
+ "Stopping #{self}" println
14
+ die!
15
+ respond_to @@ stopped
16
+ }
17
+ def to_s {
18
+ "Bunny(#{@id})"
19
+ }
20
+ }
21
+
22
+ @thread = Thread current # save current Thread so it can be resumed
23
+ def self stopped {
24
+ @amount = @amount - 1
25
+ { @thread run } if: (@amount == 0)
26
+ }
27
+
28
+ @amount = 10
29
+ bunnies = (0..@amount) map: |i| { Bunny new: i }
30
+ Console readln: "\n\nPress ENTER to stop bunnies\n\n"
31
+ bunnies each: |b| { b @@ stop: self }
32
+ Thread stop # wait to be resumed when all bunnies are killed
@@ -0,0 +1,26 @@
1
+ # Does the same as actors_primitive.fy but using
2
+ # normal objects as actors by sending messages asynchronously (via @@ syntax).
3
+ class PingPong {
4
+ Done = false
5
+ def initialize: @block {
6
+ }
7
+
8
+ def count: count reply: other {
9
+ @block call
10
+ if: (count > 1000) then: {
11
+ count println
12
+ Done = true
13
+ } else: {
14
+ other @@ count: (count + 1) reply: self
15
+ }
16
+ }
17
+ }
18
+
19
+ pong = PingPong new: { "-" print }
20
+ ping = PingPong new: { "." print }
21
+
22
+ ping @@ count: 1 reply: pong
23
+
24
+ until: { PingPong Done } do: {
25
+ Thread sleep: 0.1
26
+ }
@@ -0,0 +1,27 @@
1
+ pong = nil
2
+ done = false
3
+
4
+ ping = Actor spawn: {
5
+ loop: {
6
+ count = Actor receive
7
+ "." print
8
+ { count println; done = true; break } if: (count > 1000)
9
+ pong ! (count + 1)
10
+ }
11
+ }
12
+
13
+ pong = Actor spawn: {
14
+ loop: {
15
+ count = Actor receive
16
+ "-" print
17
+ { count println; done = true; break } if: (count > 1000)
18
+ ping ! (count + 1)
19
+ }
20
+ }
21
+
22
+ ping ! 1
23
+
24
+ # Let the actors process while the main thread sleeps...
25
+ until: { done } do: {
26
+ Thread sleep: 0.1
27
+ }
@@ -0,0 +1,37 @@
1
+ class Ring {
2
+ class Node {
3
+ def initialize: @next ring: @ring { }
4
+
5
+ def count: count {
6
+ if: @next then: {
7
+ "." print
8
+ @next @@ count: (count + 1)
9
+ } else: {
10
+ "DONE: " ++ count println
11
+ @ring done: true
12
+ }
13
+ die! # let this actor die to free resources
14
+ }
15
+ }
16
+
17
+ read_write_slot: 'done
18
+ def initialize: amount {
19
+ node = nil
20
+ amount times: {
21
+ node = Node new: node ring: self
22
+ }
23
+ @start = node
24
+ @done = false
25
+ }
26
+
27
+ def start {
28
+ @start count: 1
29
+ }
30
+ }
31
+
32
+ # create ring and run through it
33
+ ring = Ring new: 4000
34
+ ring start
35
+ until: { ring done } do: {
36
+ Thread sleep: 0.1
37
+ }
@@ -12,7 +12,7 @@ class Fixnum {
12
12
  tmp = self
13
13
  while: { tmp >= 10 } do: {
14
14
  decimals << (tmp modulo: 10)
15
- tmp = (tmp div: 10)
15
+ tmp = tmp div: 10
16
16
  }
17
17
  decimals << tmp
18
18
  decimals
@@ -5,9 +5,7 @@
5
5
  arr = [1,2,3,4,5,6]
6
6
 
7
7
  # print each element squared
8
- arr each: |x| {
9
- x squared println
10
- }
8
+ arr each: @{ squared println }
11
9
 
12
10
  # display each element with its index in the array
13
11
  arr each_with_index: |x i| {
@@ -21,22 +19,22 @@ arr map: 'squared . inspect println
21
19
  arr map: 'doubled . inspect println
22
20
 
23
21
  # print array of all elements smaller than 4
24
- arr select: |x| { x < 4 } . inspect println
22
+ arr select: @{ < 4 } . inspect println
25
23
 
26
24
  # print array of all elements that are not smaller than 4
27
- arr reject: |x| { x < 4 } . inspect println
25
+ arr reject: @{ < 4 } . inspect println
28
26
 
29
27
  # prints: [5, 6]
30
- arr take_while: |x| { x < 5 } . inspect println
28
+ arr take_while: @{ < 5 } . inspect println
31
29
 
32
30
  "testing reduce:init_val: " print
33
31
  arr reduce: |acc x| { acc * x } init_val: 1 . println # same as: 1*1*2*3*4*5*6
34
32
 
35
33
  "testing any?: " print
36
- arr any?: |x| { x > 3 } . println # prints: true
34
+ arr any?: @{ > 3 } . println # prints: true
37
35
 
38
36
  "testing all?: " print
39
- arr all?: |x| { x < 7 } . println # prints: true
37
+ arr all?: @{ < 7 } . println # prints: true
40
38
 
41
39
  "testing from:to: " print
42
40
  arr [[3,5]] . inspect println # prints: [4, 5, 6]
@@ -49,4 +47,4 @@ arr size println # prints: 6
49
47
  arr to_s println # prints: 123456
50
48
 
51
49
  "testing inspect: " print
52
- arr inspect println # prints: [1, 2, 3, 4, 5, 6] : Array
50
+ arr inspect println # prints: [1, 2, 3, 4, 5, 6]
@@ -1,10 +1,9 @@
1
1
  def bar {
2
2
  "In bar!" println
3
- yield
3
+ Thread sleep: 1
4
4
  "OK, done!" println
5
5
  }
6
6
 
7
- self @@ bar
8
7
  self @@ bar
9
8
  Thread sleep: 1
10
9
  self @@ bar
@@ -8,8 +8,8 @@ y = |x y| { Console println: $ x + y }
8
8
  y call: [2, 3] # calls y and prints: 5
9
9
 
10
10
  # prints numbers 0 to 20
11
- zahl = 0
12
- while: { zahl <= 20 } do: {
13
- Console println: zahl
14
- zahl = zahl + 1
11
+ num = 0
12
+ while: { num <= 20 } do: {
13
+ Console println: num
14
+ num = num + 1
15
15
  }
@@ -1,4 +1,4 @@
1
- block = { self foo }
1
+ block = { foo }
2
2
 
3
3
  class Foo {
4
4
  def foo {
@@ -20,7 +20,7 @@ class Foo : Bar {
20
20
  def say_hello {
21
21
  Console print: "Hello, "
22
22
  Console println: @name
23
- {@block call} if: @block
23
+ { @block call } if: @block
24
24
  }
25
25
  def on_hello_do: block {
26
26
  @block = block
@@ -1,7 +1,7 @@
1
1
  # default_args.fy
2
2
  # Example of fancy's default arguments
3
3
 
4
- def arg1: arg1 arg2: arg2 ("default_arg2") arg3: arg3 ("default_arg3") {
4
+ def arg1: arg1 ("default_arg1") arg2: arg2 ("default_arg2") arg3: arg3 ("default_arg3") {
5
5
  "arguments are: " println
6
6
  arg1 println
7
7
  arg2 println
@@ -15,3 +15,6 @@ arg1: "hello" arg2: "world"
15
15
 
16
16
  Console newline
17
17
  arg1: "hello"
18
+
19
+ Console newline
20
+ arg1
@@ -4,12 +4,12 @@ arr define_singleton_method: "foo" with: {
4
4
  self inspect println
5
5
  }
6
6
 
7
- arr send: 'foo
7
+ arr receive_message: 'foo
8
8
 
9
9
  arr undefine_singleton_method: "foo"
10
10
 
11
11
  try {
12
- arr send: 'foo
12
+ arr receive_message: 'foo
13
13
  } catch NoMethodError => e {
14
14
  e println
15
15
  }
@@ -1,7 +1,7 @@
1
1
  # echo.fy
2
2
  # Outputs contents of files
3
3
 
4
- ARGV[1] if_true: |filename| {
4
+ if: (ARGV[1]) then: |filename| {
5
5
  try {
6
6
  File open: filename modes: ['read] with: |f| {
7
7
  until: { f eof? } do: {
@@ -8,5 +8,5 @@ class Fixnum {
8
8
  }
9
9
 
10
10
  1 upto: 10 do: |i| {
11
- i to_s ++ "! = " ++ (i factorial) println
11
+ "#{i}! = #{i factorial}" println
12
12
  }
@@ -4,8 +4,8 @@ def do_large_computation: x {
4
4
  x upto: (x ** x)
5
5
  }
6
6
 
7
- # Future#&& takes a Block (or a Callable - something that implements 'call:)
8
- # and creates a new Future that executes the given Block with the value of the first Future
7
+ # FutureSend#&& takes a Block (or a Callable - something that implements 'call:)
8
+ # and creates a new FutureSend that executes the given Block with the value of the first FutureSend
9
9
  # when it has finished its computation. This makes pipelining tasks easy.
10
10
 
11
11
  f = self @ do_large_computation: 5 && @{select: 'even?} && @{inspect println}
@@ -1,8 +1,3 @@
1
- if: (ARGV[1]) then: |n| {
2
- "Using #{n} threads" println
3
- Future pool: $ n to_i
4
- }
5
-
6
1
  fs = []
7
2
  100 times: |i| {
8
3
  f = { Thread sleep: 0.5; i println } @ call
@@ -30,7 +30,7 @@ class World {
30
30
  }
31
31
  }
32
32
 
33
- def [] index {
33
+ def [index] {
34
34
  "Return the row for a given index."
35
35
  @matrix[index]
36
36
  }
@@ -2,7 +2,7 @@
2
2
  # Annotated example of fancy's classes mechanism
3
3
 
4
4
  class City {
5
- read_slots: ['name]
5
+ read_slot: 'name
6
6
  def initialize: @name {
7
7
  }
8
8
 
@@ -1,8 +1,13 @@
1
+ ARGV documentation: """
2
+ ARGV is a singleton instance of @Array@ and holds all (command-line)
3
+ arguments passed to a Fancy programm before it starts executing.
4
+ """
5
+
1
6
  def ARGV for_option: op_name do: block {
2
7
  "Runs a given block if an option is in ARGV."
3
8
 
4
9
  ARGV index: op_name . if_true: |idx| {
5
- if: (block argcount > 0) then: {
10
+ if: (block arity > 0) then: {
6
11
  ARGV[idx + 1] if_true: |arg| {
7
12
  block call: [arg]
8
13
  ARGV remove_at: idx
@@ -20,7 +25,7 @@ def ARGV for_options: op_names do: block {
20
25
 
21
26
  op_names size times: |i| {
22
27
  if: (ARGV index: (op_names[i])) then: |idx| {
23
- if: (block argcount > 0) then: {
28
+ if: (block arity > 0) then: {
24
29
  if: (ARGV[idx + 1]) then: |arg| {
25
30
  block call: [arg]
26
31
  ARGV remove_at: idx
@@ -8,13 +8,22 @@ class Array {
8
8
  include: FancyEnumerable
9
9
 
10
10
  def Array new: size {
11
- "Creates a new Array with a given size (default value is nil)."
11
+ """
12
+ @size Initial size of the @Array@ to be created (values default to @nil).
13
+
14
+ Creates a new Array with a given @size (default value is @nil).
15
+ """
12
16
 
13
17
  Array new: size with: nil
14
18
  }
15
19
 
16
20
  def clone {
17
- "Clones (shallow copy) the Array."
21
+ """
22
+ @return A shallow copy of the @Array@.
23
+
24
+ Clones (shallow copy) the @Array@.
25
+ """
26
+
18
27
  new = []
19
28
  each: |x| {
20
29
  new << x
@@ -23,7 +32,17 @@ class Array {
23
32
  }
24
33
 
25
34
  def append: arr {
26
- "Appends another Array onto this one."
35
+ """
36
+ @arr Other @Array@ to be appended to @self.
37
+ @return @self
38
+
39
+ Appends another @Array@ onto this one.
40
+
41
+ Example:
42
+ a = [1,2,3]
43
+ a append: [3,4,5]
44
+ a # => [1,2,3,3,4,5]
45
+ """
27
46
 
28
47
  arr each: |x| {
29
48
  self << x
@@ -31,11 +50,13 @@ class Array {
31
50
  self
32
51
  }
33
52
 
34
- def [] index {
53
+ def [index] {
35
54
  """
36
- Given an Array of 2 Numbers, it returns the sub-array between the
37
- given indices.
38
- If given a Number, returns the element at that index.
55
+ @index Index to get the value for or @Array@ of 2 indices used for a sub-array.
56
+ @return Element(s) stored in @self at @index, possibly @nil or an empty @Array@.
57
+
58
+ Given an @Array@ of 2 @Fixnum@s, it returns the sub-array between the given indices.
59
+ If given a single @Fixnum@, returns the element at that index.
39
60
  """
40
61
 
41
62
  # if given an Array, interpret it as a from:to: range subarray
@@ -47,34 +68,46 @@ class Array {
47
68
  }
48
69
 
49
70
  def first {
50
- "Returns the first element in the Array."
71
+ """
72
+ @return The first element in the @Array@.
73
+ """
51
74
  at: 0
52
75
  }
53
76
 
54
77
  def second {
55
- "Returns the second element in the Array"
78
+ """
79
+ @return The second element in the @Array@.
80
+ """
56
81
  at: 1
57
82
  }
58
83
 
59
84
  def third {
60
- "Returns the third element in the Array"
85
+ """
86
+ @return The third element in the @Array@.
87
+ """
61
88
  at: 2
62
89
  }
63
90
 
64
91
  def fourth {
65
- "Returns the fourth element in the Array"
92
+ """
93
+ @return The fourth element in the @Array@.
94
+ """
66
95
  at: 3
67
96
  }
68
97
 
69
98
  def rest {
70
- "Returns all elements except the first one as a new Array."
99
+ """
100
+ @return All elements in an @Array@ after the first one.
101
+
102
+ Returns all elements except the first one as a new @Array@.
103
+ """
71
104
  from: 1 to: -1
72
105
  }
73
106
 
74
107
  def each: block {
75
108
  """
76
109
  @block @Block@ to be called for each element in @self.
77
- @return Return value of calling @block on the last item in @self.
110
+ @return @self
78
111
 
79
112
  Calls a given @Block@ with each element in the @Array@.
80
113
  """
@@ -86,17 +119,26 @@ class Array {
86
119
  } catch (Fancy NextIteration) => ex {
87
120
  }
88
121
  }
89
- self
122
+ return self
90
123
  } catch (Fancy BreakIteration) => ex {
91
- ex return_value
124
+ ex result
92
125
  }
93
126
  }
94
127
 
128
+ def reverse_each: block {
129
+ size - 1 downto: 0 do: |i| {
130
+ block call: [at: i]
131
+ }
132
+ self
133
+ }
134
+
95
135
  def each_with_index: block {
96
136
  """
97
137
  @block @Block@ to be called with each element and its inde in the @Array@.
138
+ @return @self
98
139
 
99
- Iterate over all elements in Array. Calls a given Block with each element and its index.
140
+ Iterate over all elements in @Array@.
141
+ Calls a given @Block@ with each element and its index.
100
142
  """
101
143
 
102
144
  i = 0
@@ -104,19 +146,19 @@ class Array {
104
146
  block call: [x, i]
105
147
  i = i + 1
106
148
  }
107
- nil
108
149
  }
109
150
 
110
151
  def =? other {
111
152
  """
112
153
  @other Other @Array@ to compare this one to.
154
+ @return @true, if all elements of @other are in @self, @false otherwise.
113
155
 
114
156
  Compares two Arrays where order does not matter.
115
157
  """
116
158
 
117
159
  if: (other is_a?: Array) then: {
118
160
  if: (size != (other size)) then: {
119
- nil
161
+ false
120
162
  } else: {
121
163
  all?: |x| { other includes?: x }
122
164
  }
@@ -188,7 +230,7 @@ class Array {
188
230
  @return Elements of @Array@ joined to a @String@.
189
231
 
190
232
  Joins all elements with the empty @String@.
191
- [\"hello\", \"world\", \"!\"] join # => \"hello, world!\"
233
+ [\"hello\", \"world\", \"!\"] join # => \"hello,world!\"
192
234
  """
193
235
 
194
236
  join: ""
@@ -242,7 +284,9 @@ class Array {
242
284
  }
243
285
 
244
286
  def println {
245
- "Prints each element on a seperate line."
287
+ """
288
+ Prints each element on a seperate line.
289
+ """
246
290
 
247
291
  each: |x| {
248
292
  x println
@@ -250,11 +294,41 @@ class Array {
250
294
  }
251
295
 
252
296
  def to_s {
253
- "Returns @String@ representation of @Array@."
297
+ """
298
+ @return @String@ representation of @Array@.
299
+
300
+ Returns @String@ representation of @Array@.
301
+ """
254
302
 
255
303
  reduce: |x y| { x ++ y } init_val: ""
256
304
  }
257
305
 
306
+ def inspect {
307
+ """
308
+ @return Pretty-printed @String@ representation of @self.
309
+
310
+ Returns a pretty-printed @String@ representation of @self.
311
+ Example:
312
+ [1, 'foo, \"bar\", 42] inspect # => \"[1, 'foo, \\\"bar\\\", 42]\"
313
+ """
314
+
315
+ str = "["
316
+ each: |x| {
317
+ str = str + (x inspect)
318
+ } in_between: {
319
+ str = str + ", "
320
+ }
321
+ str + "]"
322
+ }
323
+
324
+ def to_a {
325
+ """
326
+ @return @self.
327
+ """
328
+
329
+ self
330
+ }
331
+
258
332
  def * num {
259
333
  """
260
334
  Returns a new @Array@ that contains the elements of self num times
@@ -269,13 +343,23 @@ class Array {
269
343
  }
270
344
 
271
345
  def + other_arr {
272
- "Returns concatenation with another @Array@."
346
+ """
347
+ @return Concatenation of @self with another @Array@
348
+
349
+ Returns concatenation with another @Array@.
350
+ [1,2,3] + [3,4,5] # => [1,2,3,3,4,5]
351
+ """
273
352
 
274
353
  clone append: other_arr
275
354
  }
276
355
 
277
356
  def indices {
278
- "Returns an @Array@ of all the indices of an @Array@."
357
+ """
358
+ @return @Array@ of all indices of @self.
359
+
360
+ Returns an @Array@ of all the indices of an @Array@.
361
+ [1,2,3] indices # => [0,1,2]
362
+ """
279
363
 
280
364
  0 upto: (size - 1)
281
365
  }
@@ -286,6 +370,7 @@ class Array {
286
370
  @return @Array@ of all indices for a given value within an @Array@ (possibly empty).
287
371
 
288
372
  Returns an Array of all indices of this item. Empty Array if item does not occur.
373
+ [1, 'foo, 2, 'foo] indices_of: 'foo # => [1, 3]
289
374
  """
290
375
 
291
376
  tmp = []
@@ -338,7 +423,7 @@ class Array {
338
423
 
339
424
  def select_with_index: block {
340
425
  """
341
- Same as select:, just gets also called with an additional argument
426
+ Same as #select:, just gets also called with an additional argument
342
427
  for each element's index value.
343
428
  """
344
429
 
@@ -363,22 +448,4 @@ class Array {
363
448
  return [object] + object
364
449
  }
365
450
  }
366
-
367
- def sum {
368
- """
369
- Calculates the sum of all the elements in the Enumerable
370
- (assuming them to be Numbers (implementing '+' & '*')).
371
- """
372
-
373
- reduce: |x y| { x + y } init_val: 0
374
- }
375
-
376
- def product {
377
- """
378
- Calculates the product of all the elements in the Enumerable
379
- (assuming them to be Numbers (implementing '+' & '*')).
380
- """
381
-
382
- reduce: |x y| { x * y } init_val: 1
383
- }
384
451
  }