opal 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (325) hide show
  1. data/.gitignore +5 -10
  2. data/LICENSE +75 -0
  3. data/README.md +55 -3
  4. data/Rakefile +62 -139
  5. data/bin/opal +7 -10
  6. data/gems/core/README.md +5 -0
  7. data/gems/core/Rakefile +7 -0
  8. data/gems/core/core.gemspec +13 -0
  9. data/gems/core/lib/core.rb +33 -0
  10. data/gems/core/lib/core/array.rb +1470 -0
  11. data/gems/core/lib/core/basic_object.rb +15 -0
  12. data/gems/core/lib/core/class.rb +31 -0
  13. data/gems/core/lib/core/dir.rb +26 -0
  14. data/gems/core/lib/core/error.rb +43 -0
  15. data/gems/core/lib/core/false_class.rb +21 -0
  16. data/gems/core/lib/core/file.rb +54 -0
  17. data/gems/core/lib/core/hash.rb +725 -0
  18. data/gems/core/lib/core/kernel.rb +240 -0
  19. data/gems/core/lib/core/module.rb +98 -0
  20. data/gems/core/lib/core/nil_class.rb +41 -0
  21. data/gems/core/lib/core/numeric.rb +370 -0
  22. data/gems/core/lib/core/proc.rb +11 -0
  23. data/gems/core/lib/core/range.rb +17 -0
  24. data/gems/core/lib/core/regexp.rb +18 -0
  25. data/gems/core/lib/core/string.rb +328 -0
  26. data/gems/core/lib/core/symbol.rb +15 -0
  27. data/gems/core/lib/core/top_self.rb +8 -0
  28. data/gems/core/lib/core/true_class.rb +20 -0
  29. data/gems/core/lib/core/vm.rb +16 -0
  30. data/{opals/opal/opal → gems/core}/spec/core/array/append_spec.rb +0 -0
  31. data/{opals/opal/opal → gems/core}/spec/core/array/assoc_spec.rb +0 -0
  32. data/{opals/opal/opal → gems/core}/spec/core/array/at_spec.rb +0 -0
  33. data/{opals/opal/opal → gems/core}/spec/core/array/clear_spec.rb +1 -1
  34. data/{opals/opal/opal → gems/core}/spec/core/array/collect_bang_spec.rb +0 -0
  35. data/{opals/opal/opal → gems/core}/spec/core/array/collect_spec.rb +0 -0
  36. data/gems/core/spec/core/array/compact_spec.rb +41 -0
  37. data/{opals/opal/opal → gems/core}/spec/core/array/concat_spec.rb +0 -0
  38. data/{opals/opal/opal → gems/core}/spec/core/array/constructor_spec.rb +0 -0
  39. data/{opals/opal/opal → gems/core}/spec/core/array/each_spec.rb +0 -0
  40. data/{opals/opal/opal → gems/core}/spec/core/array/element_reference_spec.rb +0 -0
  41. data/{opals/opal/opal → gems/core}/spec/core/array/first_spec.rb +1 -1
  42. data/{opals/opal/opal → gems/core}/spec/core/array/include_spec.rb +0 -0
  43. data/gems/core/spec/core/array/join_spec.rb +6 -0
  44. data/gems/core/spec/core/array/last_spec.rb +51 -0
  45. data/gems/core/spec/core/array/length_spec.rb +6 -0
  46. data/{opals/opal/opal → gems/core}/spec/core/array/map_spec.rb +2 -0
  47. data/gems/core/spec/core/array/reverse_spec.rb +6 -0
  48. data/{opals/opal/opal → gems/core}/spec/core/builtin_constants/builtin_constants_spec.rb +0 -0
  49. data/{opals/opal/opal → gems/core}/spec/core/false/and_spec.rb +0 -0
  50. data/{opals/opal/opal → gems/core}/spec/core/false/inspect_spec.rb +0 -0
  51. data/{opals/opal/opal → gems/core}/spec/core/false/or_spec.rb +0 -0
  52. data/{opals/opal/opal → gems/core}/spec/core/false/to_s_spec.rb +0 -0
  53. data/{opals/opal/opal → gems/core}/spec/core/false/xor_spec.rb +0 -0
  54. data/{opals/opal/opal → gems/core}/spec/core/file/join_spec.rb +1 -1
  55. data/gems/core/spec/core/hash/assoc_spec.rb +32 -0
  56. data/{opals/opal/opal → gems/core}/spec/core/kernel/instance_eval_spec.rb +0 -0
  57. data/{opals/opal/opal → gems/core}/spec/core/kernel/loop_spec.rb +0 -0
  58. data/{opals/opal/opal → gems/core}/spec/core/kernel/raise_spec.rb +0 -0
  59. data/{opals/opal/opal → gems/core}/spec/core/module/attr_accessor_spec.rb +0 -0
  60. data/{opals/opal/opal → gems/core}/spec/core/number/lt_spec.rb +0 -0
  61. data/gems/core/spec/core/string/sub_spec.rb +24 -0
  62. data/{opals/opal/opal → gems/core}/spec/core/true/and_spec.rb +0 -0
  63. data/{opals/opal/opal → gems/core}/spec/core/true/inspect_spec.rb +0 -0
  64. data/{opals/opal/opal → gems/core}/spec/core/true/or_spec.rb +0 -0
  65. data/{opals/opal/opal → gems/core}/spec/core/true/to_s_spec.rb +0 -0
  66. data/{opals/opal/opal → gems/core}/spec/core/true/xor_spec.rb +0 -0
  67. data/{opals/opal/opal → gems/core}/spec/language/and_spec.rb +2 -3
  68. data/{opals/opal/opal → gems/core}/spec/language/array_spec.rb +21 -5
  69. data/gems/core/spec/language/block_spec.rb +38 -0
  70. data/{opals/opal/opal → gems/core}/spec/language/break_spec.rb +0 -0
  71. data/gems/core/spec/language/case_spec.rb +103 -0
  72. data/{opals/opal/opal → gems/core}/spec/language/def_spec.rb +11 -1
  73. data/{opals/opal/opal → gems/core}/spec/language/eigenclass_spec.rb +0 -0
  74. data/gems/core/spec/language/file_spec.rb +13 -0
  75. data/gems/core/spec/language/fixtures/block.rb +21 -0
  76. data/gems/core/spec/language/fixtures/super.rb +293 -0
  77. data/{opals/opal/opal → gems/core}/spec/language/hash_spec.rb +0 -0
  78. data/{opals/opal/opal → gems/core}/spec/language/if_spec.rb +0 -0
  79. data/{opals/opal/opal → gems/core}/spec/language/loop_spec.rb +0 -0
  80. data/gems/core/spec/language/metaclass_spec.rb +21 -0
  81. data/{opals/opal/opal → gems/core}/spec/language/method_spec.rb +60 -0
  82. data/{opals/opal/opal → gems/core}/spec/language/next_spec.rb +0 -0
  83. data/{opals/opal/opal → gems/core}/spec/language/or_spec.rb +0 -0
  84. data/{opals/opal/opal → gems/core}/spec/language/redo_spec.rb +0 -0
  85. data/gems/core/spec/language/regexp_spec.rb +26 -0
  86. data/{opals/opal/opal → gems/core}/spec/language/rescue_spec.rb +0 -0
  87. data/{opals/opal/opal → gems/core}/spec/language/return_spec.rb +0 -0
  88. data/{opals/opal/opal → gems/core}/spec/language/string_spec.rb +0 -0
  89. data/gems/core/spec/language/super_spec.rb +32 -0
  90. data/{opals/opal/opal → gems/core}/spec/language/until_spec.rb +47 -47
  91. data/gems/core/spec/language/variables_spec.rb +155 -0
  92. data/{opals/opal/opal → gems/core}/spec/language/while_spec.rb +47 -47
  93. data/{opals/opal/opal → gems/core}/spec/spec_helper.rb +1 -1
  94. data/gems/core_fs/README.md +19 -0
  95. data/gems/dev/Rakefile +5 -0
  96. data/gems/dev/lib/dev.js +99 -0
  97. data/gems/dev/lib/dev/generator.js +1264 -0
  98. data/gems/dev/lib/dev/parser.js +979 -0
  99. data/gems/dev/lib/dev/ruby_parser.js +1088 -0
  100. data/gems/dev/lib/dev/ruby_parser.y +1267 -0
  101. data/gems/dev/lib/dev/string_scanner.js +38 -0
  102. data/gems/dev/tools/racc2js/README.md +39 -0
  103. data/gems/dev/tools/racc2js/math_parser.js +222 -0
  104. data/gems/dev/tools/racc2js/math_parser.rb +133 -0
  105. data/gems/dev/tools/racc2js/math_parser.y +28 -0
  106. data/gems/dev/tools/racc2js/parser.js +218 -0
  107. data/gems/dev/tools/racc2js/racc2js.rb +153 -0
  108. data/gems/json/README.md +4 -0
  109. data/gems/json/json.gemspec +14 -0
  110. data/gems/json/lib/json.rb +64 -0
  111. data/gems/json/lib/json/ext.rb +51 -0
  112. data/{opals/opal/browser/lib/browser/json_parse.js → gems/json/lib/json/json2.js} +197 -37
  113. data/gems/ospec/README.md +0 -0
  114. data/gems/ospec/lib/ospec.rb +6 -0
  115. data/gems/ospec/lib/ospec/autorun.rb +3 -0
  116. data/gems/ospec/lib/ospec/dsl.rb +15 -0
  117. data/gems/ospec/lib/ospec/example.rb +11 -0
  118. data/gems/ospec/lib/ospec/example/before_and_after_hooks.rb +56 -0
  119. data/gems/ospec/lib/ospec/example/errors.rb +17 -0
  120. data/gems/ospec/lib/ospec/example/example_group.rb +12 -0
  121. data/gems/ospec/lib/ospec/example/example_group_factory.rb +21 -0
  122. data/gems/ospec/lib/ospec/example/example_group_hierarchy.rb +20 -0
  123. data/{opals/opal/spec/lib/spec → gems/ospec/lib/ospec}/example/example_group_methods.rb +26 -68
  124. data/gems/ospec/lib/ospec/example/example_group_proxy.rb +14 -0
  125. data/gems/ospec/lib/ospec/example/example_methods.rb +46 -0
  126. data/gems/ospec/lib/ospec/example/example_proxy.rb +18 -0
  127. data/gems/ospec/lib/ospec/expectations.rb +19 -0
  128. data/gems/ospec/lib/ospec/expectations/errors.rb +8 -0
  129. data/gems/ospec/lib/ospec/expectations/fail_with.rb +8 -0
  130. data/gems/ospec/lib/ospec/expectations/handler.rb +27 -0
  131. data/gems/ospec/lib/ospec/matchers.rb +24 -0
  132. data/{opals/opal/Rakefile → gems/ospec/lib/ospec/matchers/be.rb} +0 -0
  133. data/gems/ospec/lib/ospec/matchers/generated_descriptions.rb +20 -0
  134. data/gems/ospec/lib/ospec/matchers/operator_matcher.rb +52 -0
  135. data/gems/ospec/lib/ospec/runner.rb +40 -0
  136. data/gems/ospec/lib/ospec/runner/example_group_runner.rb +44 -0
  137. data/{opals/opal/spec/lib/spec → gems/ospec/lib/ospec}/runner/formatter/html_formatter.rb +31 -40
  138. data/gems/ospec/lib/ospec/runner/formatter/terminal_formatter.rb +48 -0
  139. data/gems/ospec/lib/ospec/runner/options.rb +36 -0
  140. data/{opals/opal/spec/lib/spec → gems/ospec/lib/ospec}/runner/reporter.rb +23 -55
  141. data/gems/ospec/ospec.gemspec +0 -0
  142. data/gems/rquery/README.md +9 -0
  143. data/gems/rquery/lib/rquery.rb +10 -0
  144. data/gems/rquery/lib/rquery/ajax.rb +4 -0
  145. data/gems/rquery/lib/rquery/css.rb +96 -0
  146. data/gems/rquery/lib/rquery/document.rb +25 -0
  147. data/gems/rquery/lib/rquery/element.rb +292 -0
  148. data/gems/rquery/lib/rquery/event.rb +108 -0
  149. data/gems/rquery/lib/rquery/jquery.js +8177 -0
  150. data/gems/rquery/lib/rquery/request.rb +138 -0
  151. data/gems/rquery/lib/rquery/response.rb +49 -0
  152. data/gems/rquery/rquery.gemspec +16 -0
  153. data/lib/opal.js +1597 -0
  154. data/lib/opal.rb +6 -69
  155. data/lib/opal/builder.rb +115 -0
  156. data/lib/opal/bundle.rb +131 -0
  157. data/lib/opal/command.rb +11 -0
  158. data/lib/opal/context.rb +69 -0
  159. data/lib/opal/context/console.rb +12 -0
  160. data/lib/opal/context/file_system.rb +19 -0
  161. data/lib/opal/context/loader.rb +117 -0
  162. data/lib/opal/gem.rb +153 -0
  163. data/lib/opal/ruby/nodes.rb +1302 -0
  164. data/lib/opal/ruby/parser.rb +780 -0
  165. data/lib/opal/ruby/ruby_parser.rb +5170 -0
  166. data/lib/opal/ruby/ruby_parser.y +1298 -0
  167. data/opal.gemspec +11 -256
  168. metadata +179 -240
  169. data/.gitmodules +0 -6
  170. data/Opalfile +0 -384
  171. data/VERSION +0 -1
  172. data/demos/apps/browser_demo/index.html +0 -11
  173. data/demos/apps/browser_demo/lib/browser_demo.rb +0 -31
  174. data/demos/apps/simple_opal/Opalfile +0 -13
  175. data/demos/apps/simple_opal/index.html +0 -11
  176. data/demos/apps/simple_opal/lib/app_controller.rb +0 -62
  177. data/demos/apps/simple_opal/lib/main_window.rb +0 -146
  178. data/demos/browser/request/index.html +0 -52
  179. data/demos/browser/request/request.rb +0 -48
  180. data/gen/browser/__PROJECT_NAME__/Rakefile +0 -12
  181. data/gen/browser/__PROJECT_NAME__/index.html +0 -11
  182. data/gen/browser/__PROJECT_NAME__/javascripts/opal.debug.js +0 -4687
  183. data/gen/browser/__PROJECT_NAME__/javascripts/opal.min.js +0 -175
  184. data/gen/browser/__PROJECT_NAME__/lib/__PROJECT_NAME__.rb +0 -1
  185. data/lib/opal/builders/base.rb +0 -50
  186. data/lib/opal/builders/css.rb +0 -46
  187. data/lib/opal/builders/javascript.rb +0 -44
  188. data/lib/opal/builders/opal.rb +0 -79
  189. data/lib/opal/builders/ruby.rb +0 -50
  190. data/lib/opal/builders/ruby/generate.rb +0 -1851
  191. data/lib/opal/builders/ruby/nodes.rb +0 -210
  192. data/lib/opal/builders/ruby/ruby.rb +0 -916
  193. data/lib/opal/builders/ruby/ruby_parser.rb +0 -6008
  194. data/lib/opal/builders/ruby/ruby_parser.rb.y +0 -1451
  195. data/lib/opal/models/build_item.rb +0 -104
  196. data/lib/opal/models/hash_struct.rb +0 -40
  197. data/lib/opal/models/project.rb +0 -252
  198. data/lib/opal/models/struct_accessors.rb +0 -58
  199. data/lib/opal/models/target.rb +0 -176
  200. data/lib/opal/opal/build.rb +0 -169
  201. data/lib/opal/opal/env/console.rb +0 -66
  202. data/lib/opal/opal/env/fs.rb +0 -98
  203. data/lib/opal/opal/env/object.rb +0 -48
  204. data/lib/opal/opal/environment.rb +0 -139
  205. data/lib/opal/opal/gen.rb +0 -62
  206. data/lib/opal/opal/opal.rb +0 -75
  207. data/lib/opal/opal/repl.rb +0 -38
  208. data/lib/opal/opalfile/dsl.rb +0 -62
  209. data/lib/opal/opalfile/opalfile.rb +0 -133
  210. data/lib/opal/opalfile/task.rb +0 -96
  211. data/lib/opal/opalfile/task_manager.rb +0 -63
  212. data/lib/opal/opalfile/task_scope.rb +0 -52
  213. data/lib/opal/rack/app_server.rb +0 -119
  214. data/lib/opal/rake/opal_task.rb +0 -34
  215. data/opals/opal/README.md +0 -37
  216. data/opals/opal/browser/Opalfile +0 -11
  217. data/opals/opal/browser/README.md +0 -146
  218. data/opals/opal/browser/SIZZLE_LICESNSE.txt +0 -148
  219. data/opals/opal/browser/lib/browser.rb +0 -118
  220. data/opals/opal/browser/lib/browser/builder.rb +0 -41
  221. data/opals/opal/browser/lib/browser/canvas_context.rb +0 -115
  222. data/opals/opal/browser/lib/browser/dimensions.rb +0 -50
  223. data/opals/opal/browser/lib/browser/document.rb +0 -146
  224. data/opals/opal/browser/lib/browser/element.rb +0 -487
  225. data/opals/opal/browser/lib/browser/element/attributes.rb +0 -88
  226. data/opals/opal/browser/lib/browser/element/css.rb +0 -290
  227. data/opals/opal/browser/lib/browser/element/form.rb +0 -146
  228. data/opals/opal/browser/lib/browser/event/dom_events.rb +0 -81
  229. data/opals/opal/browser/lib/browser/event/event.rb +0 -177
  230. data/opals/opal/browser/lib/browser/event/trigger_events.rb +0 -53
  231. data/opals/opal/browser/lib/browser/geometry.rb +0 -97
  232. data/opals/opal/browser/lib/browser/json.rb +0 -32
  233. data/opals/opal/browser/lib/browser/request/request.rb +0 -201
  234. data/opals/opal/browser/lib/browser/sizzle.js +0 -1068
  235. data/opals/opal/browser/lib/browser/string.rb +0 -42
  236. data/opals/opal/browser/lib/browser/touch.rb +0 -37
  237. data/opals/opal/browser/lib/browser/vml_context.js +0 -33
  238. data/opals/opal/browser/lib/browser/window.rb +0 -36
  239. data/opals/opal/browser/spec/browser/browser_detection_spec.rb +0 -7
  240. data/opals/opal/browser/spec/document/aref_spec.rb +0 -110
  241. data/opals/opal/browser/spec/document/ready_spec.rb +0 -16
  242. data/opals/opal/browser/spec/element/body_spec.rb +0 -11
  243. data/opals/opal/browser/spec/element/clear_spec.rb +0 -26
  244. data/opals/opal/browser/spec/element/empty_spec.rb +0 -29
  245. data/opals/opal/browser/spec/element/has_class_spec.rb +0 -40
  246. data/opals/opal/browser/spec/element/hidden_spec.rb +0 -23
  247. data/opals/opal/browser/spec/element/hide_spec.rb +0 -31
  248. data/opals/opal/browser/spec/element/remove_spec.rb +0 -25
  249. data/opals/opal/browser/spec/element/show_spec.rb +0 -31
  250. data/opals/opal/browser/spec/element/style_spec.rb +0 -69
  251. data/opals/opal/browser/spec/element/toggle_spec.rb +0 -31
  252. data/opals/opal/browser/spec/element/visible_spec.rb +0 -23
  253. data/opals/opal/browser/spec/spec_helper.rb +0 -1
  254. data/opals/opal/opal/Opalfile +0 -14
  255. data/opals/opal/opal/spec/core/array/compact_spec.rb +0 -15
  256. data/opals/opal/opal/spec/fixtures/super.rb +0 -70
  257. data/opals/opal/opal/spec/language/____temp_remove_this.rb +0 -12
  258. data/opals/opal/opal/spec/language/block_spec.rb +0 -18
  259. data/opals/opal/opal/spec/language/case_spec.rb +0 -103
  260. data/opals/opal/opal/spec/language/metaclass_spec.rb +0 -21
  261. data/opals/opal/opal/spec/language/super_spec.rb +0 -26
  262. data/opals/opal/runtime/Opalfile +0 -78
  263. data/opals/opal/runtime/README.md +0 -12
  264. data/opals/opal/runtime/docs/debugging.md +0 -51
  265. data/opals/opal/runtime/lib/array.rb +0 -1516
  266. data/opals/opal/runtime/lib/basic_object.rb +0 -49
  267. data/opals/opal/runtime/lib/class.rb +0 -54
  268. data/opals/opal/runtime/lib/dir.rb +0 -36
  269. data/opals/opal/runtime/lib/error.rb +0 -49
  270. data/opals/opal/runtime/lib/false_class.rb +0 -52
  271. data/opals/opal/runtime/lib/file.rb +0 -79
  272. data/opals/opal/runtime/lib/hash.rb +0 -791
  273. data/opals/opal/runtime/lib/io.rb +0 -39
  274. data/opals/opal/runtime/lib/kernel.rb +0 -288
  275. data/opals/opal/runtime/lib/match_data.rb +0 -36
  276. data/opals/opal/runtime/lib/module.rb +0 -109
  277. data/opals/opal/runtime/lib/nil_class.rb +0 -69
  278. data/opals/opal/runtime/lib/number.rb +0 -398
  279. data/opals/opal/runtime/lib/proc.rb +0 -77
  280. data/opals/opal/runtime/lib/range.rb +0 -63
  281. data/opals/opal/runtime/lib/regexp.rb +0 -111
  282. data/opals/opal/runtime/lib/ruby.rb +0 -30
  283. data/opals/opal/runtime/lib/string.rb +0 -328
  284. data/opals/opal/runtime/lib/symbol.rb +0 -40
  285. data/opals/opal/runtime/lib/top_self.rb +0 -33
  286. data/opals/opal/runtime/lib/true_class.rb +0 -45
  287. data/opals/opal/runtime/runtime/browser.js +0 -287
  288. data/opals/opal/runtime/runtime/debug.js +0 -180
  289. data/opals/opal/runtime/runtime/opal.js +0 -1008
  290. data/opals/opal/runtime/runtime/post_opal.js +0 -1
  291. data/opals/opal/runtime/runtime/pre_opal.js +0 -2
  292. data/opals/opal/runtime/runtime/server_side.js +0 -50
  293. data/opals/opal/spec/LICENSE.txt +0 -26
  294. data/opals/opal/spec/Opalfile +0 -5
  295. data/opals/opal/spec/bin/spec.rb +0 -43
  296. data/opals/opal/spec/lib/spec.rb +0 -33
  297. data/opals/opal/spec/lib/spec/dsl.rb +0 -41
  298. data/opals/opal/spec/lib/spec/example.rb +0 -35
  299. data/opals/opal/spec/lib/spec/example/before_and_after_hooks.rb +0 -81
  300. data/opals/opal/spec/lib/spec/example/errors.rb +0 -42
  301. data/opals/opal/spec/lib/spec/example/example_group.rb +0 -37
  302. data/opals/opal/spec/lib/spec/example/example_group_factory.rb +0 -43
  303. data/opals/opal/spec/lib/spec/example/example_group_hierarchy.rb +0 -45
  304. data/opals/opal/spec/lib/spec/example/example_group_proxy.rb +0 -41
  305. data/opals/opal/spec/lib/spec/example/example_methods.rb +0 -73
  306. data/opals/opal/spec/lib/spec/example/example_proxy.rb +0 -48
  307. data/opals/opal/spec/lib/spec/expectations.rb +0 -46
  308. data/opals/opal/spec/lib/spec/expectations/errors.rb +0 -35
  309. data/opals/opal/spec/lib/spec/expectations/fail_with.rb +0 -37
  310. data/opals/opal/spec/lib/spec/expectations/handler.rb +0 -48
  311. data/opals/opal/spec/lib/spec/matchers.rb +0 -50
  312. data/opals/opal/spec/lib/spec/matchers/be.rb +0 -26
  313. data/opals/opal/spec/lib/spec/matchers/generated_descriptions.rb +0 -47
  314. data/opals/opal/spec/lib/spec/matchers/operator_matcher.rb +0 -66
  315. data/opals/opal/spec/lib/spec/runner.rb +0 -48
  316. data/opals/opal/spec/lib/spec/runner/example_group_runner.rb +0 -71
  317. data/opals/opal/spec/lib/spec/runner/formatter/terminal_formatter.rb +0 -82
  318. data/opals/opal/spec/lib/spec/runner/options.rb +0 -63
  319. data/opals/opal/spec/resources/index.html +0 -25
  320. data/opals/opal/spec/resources/spec.css +0 -132
  321. data/spec/cherry_kit/iseq_spec.rb +0 -38
  322. data/spec/spec_helper.rb +0 -16
  323. data/spec/vienna_spec.rb +0 -7
  324. data/yard/index.html +0 -43
  325. data/yard/style.css +0 -765
@@ -1,26 +0,0 @@
1
- #
2
- # describe "The super keyword" do
3
- #
4
- # it "calls the method on the calling class" do
5
- # Super::S1::A.new.foo([]).should == ["A#foo", "A#bar"]
6
- # Super::S1::A.new.bar([]).should == ["A#bar"]
7
- # Super::S1::B.new.foo([]).should == ["B#foo","A#foo","B#bar","A#bar"]
8
- # Super::S1::B.new.bar([]).should == ["B#bar", "A#bar"]
9
- # end
10
- #
11
- # it "searches the full inheritence chain" do
12
- # Super::S2::B.new.foo([]).should == ["B#foo", "A#baz"]
13
- # Super::S2::B.new.baz([]).should == ["A#baz"]
14
- # Super::S2::C.new.foo([]).should == ["B#foo","C#baz","A#baz"]
15
- # Super::S2::C.new.baz([]).should == ["C#baz","A#baz"]
16
- # end
17
- #
18
- # it "searches class methods" do
19
- # Super::S3::A.new.foo([]).should == ["A#foo"]
20
- # Super::S3::A.foo([]).should == ["A::foo"]
21
- # Super::S3::A.bar([]).should == ["A::bar","A::foo"]
22
- # Super::S3::B.new.foo([]).should == ["A#foo"]
23
- # Super::S3::B.foo([]).should == ["B::foo","A::foo"]
24
- # Super::S3::B.bar([]).should == ["B::bar","A::bar","B::foo","A::foo"]
25
- # end
26
- # end
@@ -1,78 +0,0 @@
1
- # For spec..
2
- mode :spec do
3
- # opal must require the spec opal itself. Also, we are the main target, so we
4
- # can set the bin file aswell (to use the spec's bin ruby file)
5
- config :runtime,
6
- :required => [:spec],
7
- :bin_file => :spec
8
-
9
- # Configure :spec to include its bin file, so opal can use it
10
- config :spec,
11
- :include_bin => true
12
- end
13
-
14
- namespace :build do
15
-
16
- # opal needs a completely custom opal (gem) builder as its not actually an
17
- # opal..ish...kinda
18
- task :opal do
19
-
20
- # puts "building opal for opal"
21
-
22
- FileUtils.mkdir_p File.dirname(@dst_path)
23
-
24
- # Get all ruby sources, then sort them (kernel.rb then module.rb then any)
25
- rb_sources = @build_item.source_items.select do |item|
26
- item.resource == :ruby
27
- end
28
-
29
- # make sure these two are at the start
30
- ['lib/kernel.rb', 'lib/module.rb'].reverse.each do |order|
31
- rb_sources.unshift rb_sources.select { |item|
32
- item.filename == order
33
- }.first
34
- end
35
- # make sure we remove the duplicates left over
36
- rb_sources.uniq!
37
-
38
- File.open(@dst_path, 'w') do |out|
39
- js_sources = %w(pre_opal opal browser debug post_opal)
40
-
41
- # puts "build mode for opal is #{@project.build_mode.inspect}"
42
-
43
- unless @project.build_mode == :debug
44
- puts "deleting debug!!!!!"
45
- js_sources.delete 'debug'
46
- end
47
-
48
- puts js_sources.inspect
49
-
50
- js_str = []
51
-
52
- js_sources.each do |src|
53
- # puts @target.target_root, 'runtime',
54
- js_str << File.read(File.join(@target.target_root, 'runtime', "#{src}.js"))
55
- end
56
-
57
- out.puts js_str.join
58
-
59
- # out.puts "var opal = {};"
60
- # out.puts "(function(global, exports) {"
61
- # js_sources.each do |js|
62
- # puts js.filename
63
- # out.puts File.read(js.stage!.staging_path)
64
- # end
65
- # out.puts "})(this.window || global, opal);"
66
-
67
-
68
-
69
-
70
- rb_sources.each do |rb|
71
- out.puts "// ##################### #{rb.filename} #####################"
72
- out.puts "opal.load_raw_file('opal/#{rb.filename}', "
73
- out.puts File.read(rb.stage!.staging_path)
74
- out.puts ");"
75
- end
76
- end
77
- end
78
- end
@@ -1,12 +0,0 @@
1
- # Opal
2
-
3
- Opal comprises of the actual ruby runtime, as well as all the core libraries that are default in ruby. This includes the usual {Array}, {String}, {Proc}, {Hash} classes etc.
4
-
5
- ## Spec
6
-
7
- Opal also has some (of the many!) specs from RSpec in it's `spec/` directory. Opal attempts to be as sytactially compatible with ruby as possible, so uses the very same specs as vanilla ruby does so completeness can be determined. The specs are accessible from this documentation at [spec/index.html](spec/index.html). This is not the result of the specs, as instead the specs are run in the browser on demand. What you will see is the ruby specs being run within the browser itself, and the results output directly. This also helps determine any errors that happen to be browser specific.
8
-
9
- ## Debugging
10
-
11
- Opal, when built in debug mode, contains some useful tools for debugging applications including stack traces and method missing errors.
12
- See {file:docs/debugging.md debugging documentation} for more.
@@ -1,51 +0,0 @@
1
- Debugging
2
- =========
3
-
4
- As described in other places, the code generated by opal and the vienna build tools is not very easy to read, which can become a problem when debugging errors in code. A lot of the properties and methods added to javascript objects are very unfriendly and are often coded. For example, searching a constant uses the `.cs()` method found on every ruby object, and a long chained command might contain many of these method calls before an error occurs, making it difficult to see what is going on.
5
-
6
- Stack traces
7
- ------------
8
- The ruby stack trace is a great (and often only) place to catch errors. The ruby stack trace lists all the method calls that lead to an error. Opal provides a stack trace, which is available within the browser. Opal additionally adds some extra information on the stack trace. Every method call lists the file and line number that the method implementation is on (in order), but in addition to ruby, opal lists the receiver of the method call (using `inspect`) as well as all the arguments passed to a method. This clearly helps to identify problem code. Inspecting the arguments passed to methods quickly helps to identify where a problem occurred (for example, seeing a `nil` passed into a method is often a good indication that something has gone wrong).
9
-
10
- ### Example stack trace
11
-
12
- Create a new project and enter the code into the document. This is an obvious error, but works well to show the problem.
13
-
14
- def something(a, b)
15
- raise a
16
- end
17
-
18
- def something_a(a, b, c)
19
- something "adam", [a, b, c]
20
- end
21
-
22
- something_a 100, 200, 400
23
-
24
- Here we know eventually `something` will be called which will raise an error. So on running the given file, the console shows us the following error:
25
-
26
- !!!plain
27
- RuntimeError: adam
28
- from request/request.rb:29:in main.something("adam", [100, 200, 400])
29
- from request/request.rb:33:in main.something_a(100, 200, 400)
30
-
31
- The output is clean and simple. The error print out is on the top string. The stack is listed to the left with the filename and line number of each method in the stack. Now, instead of the usual ruby method of just listing the method call, opal lists the receiver (using `inspect`), the method call as expected but also every argument passed to the method (again using `inspect`). It is very easy now to follow what is being passed to what, and where possible errors come from. And, as expected, execution stops on the error.
32
-
33
- ### Dealing with null and undefined
34
-
35
- Occasionally, when writing code that talks to javascript objects, it is possible that null and undefined might slip into the ruby side of things. Trying to send messages to either of these will result in an error. To assist in point out these errors, opal marks a method which takes one of these natives as a warning in the stack trace (supported by Firefox, Safari, IE8, Opera, Chrome) as an indication that this might be the root of the problem. It is not always the cause of the problem, as some methods might accept these objects, but it is a useful indication for most circumstances. Also, any object that slips in which is not a ruby object (like a JSON object for instance) will also get marked with this warning. Try the following code to see an example:
36
-
37
- def some_method(a, b, c)
38
- puts a
39
- puts b
40
- puts c
41
- end
42
-
43
- some_method 10, 20, `null`
44
-
45
- Which should produce the following stack trace:
46
-
47
- !!!plain
48
- RuntimeError
49
- from browser_test.rb:1: in main.some_method(10, 20, <null>)
50
-
51
- The second output line should be marked in yellow, or however your browser console identifies warnings. Try a different browser if you do not notice a difference.
@@ -1,1516 +0,0 @@
1
- #
2
- # array.rb
3
- # vienna
4
- #
5
- # Created by Adam Beynon.
6
- # Copyright 2010 Adam Beynon.
7
- #
8
- # Permission is hereby granted, free of charge, to any person obtaining a copy
9
- # of this software and associated documentation files (the "Software"), to deal
10
- # in the Software without restriction, including without limitation the rights
11
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
- # copies of the Software, and to permit persons to whom the Software is
13
- # furnished to do so, subject to the following conditions:
14
- #
15
- # The above copyright notice and this permission notice shall be included in
16
- # all copies or substantial portions of the Software.
17
- #
18
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
- # THE SOFTWARE.
25
- #
26
-
27
- # Arrays are ordered, indexed by integers starting at 0.
28
- #
29
- # ## Implementation details
30
- #
31
- # For efficiency, an array instance is simply a native javascript array. There
32
- # is no wrapping or referencing, it is simply a toll-free class. All the core
33
- # runtime methods are applied to `Array.prototype` in opal, so it is fully
34
- # functional as a ruby object. All methods, as standard, are prefixed with a
35
- # `$`. This means the following ruby:
36
- #
37
- # [1, 2, 3].length
38
- #
39
- # is converted to the following javascript:
40
- #
41
- # [1, 2, 3].$length();
42
- #
43
- class Array
44
-
45
- # Returns a new array populated with the given objects.
46
- #
47
- # @example
48
- # Array.[](1, 2, 3)
49
- # # => [1, 2, 3]
50
- #
51
- # Array["a", "b", "c"]
52
- # # => ["a", "b", "c"]
53
- #
54
- # @param [Object] objs all objects to add to the array
55
- # @return [Array] returns a new array instance
56
- def self.[](*objs)
57
- `return #{objs};`
58
- end
59
-
60
- # Set Intersection - Returns a new array containing elements common to the two
61
- # arrays, with no duplicates.
62
- #
63
- # @example
64
- # [1, 1, 3, 5] & [1, 2, 3]
65
- # # => [1, 3]
66
- #
67
- # @param [Array] other another array to intersect.
68
- # @return [Array] intersected array
69
- def &(other)
70
- result = []
71
- `var seen = [];
72
- for (var i = 0; i < #{self}.length; i++) {
73
- var test = #{self}[i], hash = test.hash();
74
- if (seen.indexOf(hash) == -1) {
75
- for (var j = 0; j < #{other}.length; j++) {
76
- var test_b = #{other}[j], hash_b = test_b.hash();
77
- if ((hash == hash_b) && seen.indexOf(hash) == -1) {
78
- seen.push(hash);
79
- #{result}.push(test);
80
- }
81
- }
82
- }
83
- }`
84
- result
85
- end
86
-
87
- # Repitition - When given a string argument, acts the same as {#join}.
88
- # Otherwise, returns a new array built by concatenating the `num` copies of
89
- # `self`.
90
- #
91
- # @example With Number
92
- # [1, 2, 3] * 3
93
- # # => [1, 2, 3, 1, 2, 3, 1, 2, 3]
94
- #
95
- # @example With String
96
- # [1, 2, 3] * ','
97
- # # => "1,2,3"
98
- #
99
- # @param [String, Number] num string or number used for joining/concat
100
- # @result [String, Array] depending on argument
101
- def *(arg)
102
- if arg.is_a? String
103
- join arg
104
- else # number
105
- `var result = [];
106
- for (var i = 0; i < parseInt(#{arg}); i++) {
107
- result = result.concat(#{self});
108
- }
109
- return result;`
110
- end
111
- end
112
-
113
- # Concatenation - returns a new array built by concatenating the two arrays
114
- # together to produce a third array.
115
- #
116
- # @example
117
- # [1, 2, 3] + [4, 5]
118
- # # => [1, 2, 3, 4, 5]
119
- #
120
- # @param [Array] other_ary the array to concat with
121
- # @return [Array] returns new concatenated array
122
- def +(other_ary)
123
- `return #{self}.concat(#{other_ary});`
124
- end
125
-
126
- # Array difference. Removes a new array that is a copy of the original array,
127
- # removing any items that also appear in other_ary.
128
- #
129
- # @example
130
- # [1, 1, 2, 2, 3, 3, 4, 5] - [1, 2, 4]
131
- # # => [3, 3, 5]
132
- #
133
- # @param [Array] other_ary array to use for difference
134
- # @return [Array] new array
135
- def -(other_ary)
136
- raise "Array#- not implemented"
137
- end
138
-
139
- # Append - Pushes the given object on to the end of this array. This
140
- # expression returns the array itself, so several appends may be chained
141
- # together.
142
- #
143
- # @example
144
- # [1, 2] << "c" << "d" << [3, 4]
145
- # # => [1, 2, "c", "d", [3, 4]]
146
- #
147
- # @param [Object] obj object to append
148
- # @return [Array] returns the receiver
149
- def <<(obj)
150
- `#{self}.push(#{obj});`
151
- self
152
- end
153
-
154
- # Append - Pushes the given object(s) on to the end of this array. This
155
- # expression returns the array itself, so several appends may be chained
156
- # together
157
- #
158
- # @example
159
- # a = ["a", "b", "c"]
160
- # a.push("d", "e", "f")
161
- # # => ["a", "b", "c", "d", "e", "f"]
162
- #
163
- # @param [Object] obj the object(s) to push on to the array
164
- # @return [Array] returns the receiver
165
- def push(*objs)
166
- `for (var i = 0; i < #{objs}.length; i++) {
167
- #{self}.push(#{objs}[i]);
168
- }`
169
- self
170
- end
171
-
172
- # Equality - Two arrays are equal if they contain the same number of elements
173
- # and if each element is equal to (according to {Object#==}) the corresponding
174
- # element in the other array.
175
- #
176
- # @example
177
- # ["a", "c"] == ["a", "c", 7]
178
- # # => false
179
- #
180
- # ["a", "c", 7] == ["a", "c", 7]
181
- # # => true
182
- #
183
- # ["a", "c", 7] == ["a", "d", "f"]
184
- # # => false
185
- #
186
- # @param [Array] other array to compare
187
- # @return [Boolean] are arrays equal
188
- def ==(other)
189
- `if (#{self} === #{other}) return #{true};
190
- if (!(#{other}.info & #{self}.TA)) return #{false};
191
- if (#{self}.length !== #{other}.length) return #{false};
192
- for (var i = 0; i < #{self}.length; i++) {
193
- if (!#{self}[i]['$=='](#{other}[i]).r) return #{false};
194
- }`
195
- true
196
- end
197
-
198
- # Element Reference - Returns the element at `index`, or returns a subarray at
199
- # `index` and continuing for `length` elements, or returns a subarray if
200
- # `index` is a range. Negative indices count backward from the end of the
201
- # array (`-1` is the last element). Returns `nil` if the index (or starting
202
- # index) are out of range.
203
- #
204
- # @example
205
- # a = ["a", "b", "c", "d", "e"]
206
- # a[2] + a[0] + a[1]
207
- # # => "cab"
208
- # a[6]
209
- # # => nil
210
- # a[1, 2]
211
- # # => ["b", c""]
212
- # a[1..3]
213
- # # => ["b", "c", "d"]
214
- # a[4..7]
215
- # # => ["e"]
216
- # a[6..10]
217
- # # => nil
218
- # a[-3, 3]
219
- # # => ["c", "d", "e"]
220
- # a[5]
221
- # # => nil
222
- # a[5, 1]
223
- # # => []
224
- # a[5..10]
225
- # # => []
226
- #
227
- # @todo Does not yet work with ranges.
228
- #
229
- # @param [Range, Number] index to begin with
230
- # @param [Number] length last index
231
- # @return [Array, nil] result
232
- def [](index, length = nil)
233
- size = `#{self}.length;`
234
-
235
- if index.is_a? Range
236
- raise "need to implement range"
237
- else
238
- `if (#{index} < 0) #{index} += #{size};`
239
- end
240
- `if (#{index} >= #{size} || #{index} < 0) return #{nil};`
241
-
242
- if length
243
- `if (#{length} <= 0) return [];`
244
- `return #{self}.slice(#{index}, #{index} + #{length});`
245
- else
246
- `return #{self}[#{index}];`
247
- end
248
- end
249
-
250
- alias_method :slice, :[]
251
-
252
- # @todo Need to expand functionality
253
- def []=(index, value)
254
- `return #{self}[#{index}] = #{value};`
255
- end
256
-
257
- # Searches through an array whose elements are also arrays comparing `obj`
258
- # with the first element of each contained array using `obj.==`. Returns the
259
- # first contained array that matches (that is, the first associated array), or
260
- # `nil` if no match is found. See also {Array#rassoc}
261
- #
262
- # @example
263
- # s1 = ["colors", "red", "blue", "green"]
264
- # s2 = ["letters", "a", "b", "c"]
265
- # s3 = "foo"
266
- # a = [s1, s2, s3]
267
- # a.assoc "letters"
268
- # # => ["letter", "a", "b", "c"]
269
- # a.assoc "foo"
270
- # # => nil
271
- def assoc(obj)
272
- `for (var i = 0; i < #{self}.length; i++) {
273
- var test = #{self}[i];
274
- if (test.info & #{self}.TA && test[0] !== undefined && test[0]===#{obj}) {
275
- return test;
276
- }
277
- }`
278
- nil
279
- end
280
-
281
- # Returns the element at `index`. A negative index count from the end of the
282
- # receiver. Returns `nil` if the index is out of range. See also `Array#[]`.
283
- #
284
- # @example
285
- # a = ["a", "b", "c", "d", "e"]
286
- # a.at 0
287
- # # => "a"
288
- # a.at -1
289
- # # => "e"
290
- #
291
- # @param [Number] index index to get
292
- # @return [Object, nil] returns nil or the result
293
- def at(index)
294
- `if (#{index} < 0) {
295
- #{index} += #{self}.length;
296
- }
297
- if (#{index} < 0 || #{index} >= #{self}.length) {
298
- return #{nil};
299
- }
300
- return #{self}[#{index}];`
301
- end
302
-
303
- # Removes all elements from `self`.
304
- #
305
- # @example
306
- # a = ["a", "b", "c", "d", "e"]
307
- # a.clear
308
- # # => []
309
- #
310
- # @return [Array] returns receiver
311
- def clear
312
- `return #{self}.splice(0, #{self}.length);`
313
- end
314
-
315
- # Invokes `block` once for each element of `self`. Creates a new array
316
- # containing the values returned by the block. See also {Enumerable#collect}.
317
- #
318
- # If no block is given, an anumerator is returned instead.
319
- #
320
- # @todo No enumerator is returned when no block given.
321
- #
322
- # @example
323
- # a = ["a", "b", "c", "d"]
324
- # a.collect { |x| x + "!" }
325
- # # => ["a!", "b!", "c!", "d!"]
326
- # a
327
- # # => ["a", "b", "c", "d"]
328
- #
329
- # @return [Array] new array
330
- def collect(&block)
331
- result = []
332
- `for (var i = 0; i < #{self}.length; i++) {
333
- try {
334
- #{result}.push(#{block}.apply(#{block}.__self__, [#{self}[i]]));
335
- } catch (e) {
336
- if (e.__keyword__ == 'break') {
337
- return e.opal_value;
338
- }
339
-
340
- throw e;
341
- }
342
- }`
343
- result
344
- end
345
-
346
- alias_method :map, :collect
347
-
348
- # Invokes the `block` once for each element of `self`, replacing the element
349
- # with the value returned by `block`. See also Enumerable#collect.
350
- #
351
- # If no block is given, an enumerator is returned instead.
352
- #
353
- # @todo no block given does not return an enumerator
354
- #
355
- # @example
356
- # a = ["a", "b", "c", "d"]
357
- # a.collect { |x| x + "!" }
358
- # # => ["a!", "b!", "c!", "d!"]
359
- # a
360
- # # => ["a!", "b!", "c!", "d!"]
361
- #
362
- # @return [Array] returns receiver
363
- def collect!(&block)
364
- `for (var i = 0; i < #{self}.length; i++) {
365
- try {
366
- #{self}[i] = #{block}.apply(#{block}.__self__, [#{self}[i]]);
367
- } catch (e) {
368
- if (e.__keyword__ == 'break') {
369
- return e.opal_value;
370
- }
371
-
372
- throw e;
373
- }
374
- }`
375
- self
376
- end
377
-
378
- alias_method :map!, :collect!
379
-
380
- # Returns a copy of `self` with all `nil` elements removed.
381
- #
382
- # @example
383
- # ["a", nil, "b", nil, "c", nil].compact
384
- # # => ["a", "b", "c"]
385
- #
386
- # @return [Array] new array
387
- def compact
388
- result = []
389
- `for (var i = 0; i < #{self}.length; i++) {
390
- if (#{self}[i] !== #{nil})
391
- #{result}.push(#{self}[i]);
392
- }`
393
- result
394
- end
395
-
396
- # Removes nil elements from the array. Returns nil if no changes were made,
397
- # otherwise returns ary.
398
- #
399
- # @example
400
- # ["a", nil, "b", nil, "c", nil].compact!
401
- # # => ["a", "b", "c"]
402
- #
403
- # ["a", "b", "c"].compact!
404
- # # => nil
405
- #
406
- # @return [Array, nil] returns either the receiver or nil
407
- def compact!
408
- `var size = #{self}.length;
409
- for (var i = 0; i < #{self}.length; i++) {
410
- if (#{self}[i] == #{nil}) {
411
- #{self}.splice(i, 1);
412
- i--;
413
- }
414
- }`
415
- `return size == #{self}.length ? #{nil} : #{self};`
416
- end
417
-
418
- # Appends the elements of `other_ary` to `self`
419
- #
420
- # @example
421
- # ["a", "b"].concat ["c", "d"]
422
- # # => ["a", "b", "c", "d"]
423
- #
424
- # @param [Array] other_ary array to concat
425
- # @return [Array] returns receiver
426
- def concat(other_ary)
427
- `var length = #{other_ary}.length;
428
- for (var i = 0; i < length; i++) {
429
- #{self}.push(#{other_ary}[i]);
430
- }`
431
- self
432
- end
433
-
434
- # Returns the number of elements. If an argument is given, counts the number
435
- # of elements which equals to `obj`. If a block is given, counts the number of
436
- # elements yielding a true value.
437
- #
438
- # @note Block usage not yet implemented
439
- #
440
- # @example
441
- # ary = [1, 2, 4, 2]
442
- # ary.count
443
- # # => 4
444
- # ary.count(2)
445
- # # => 2
446
- #
447
- # @param [Object] obj object to check
448
- # @return [Number] count or count of obj
449
- def count(obj)
450
- `if (obj !== undefined) {
451
- var total = 0;
452
- for (var i = 0; i < #{self}.length; i++) {
453
- if (#{self}[i] === #{obj})
454
- total += 1;
455
- }
456
- return total;
457
- } else {
458
- return #{self}.length;
459
- }`
460
- end
461
-
462
- # Deletes items from `self` that are equal to `obj`. If any items are found,
463
- # returns `obj`. If the itme is not found, returns `nil`. If the optional code
464
- # block is given, returns the result of `block` if the item is not found. (To
465
- # remove nil elements and get an informative return value, use {#compact!})
466
- #
467
- # @todo block is not yet used
468
- #
469
- # @example
470
- # a = ["a", "b", "b", "b", "c"]
471
- # a.delete("b")
472
- # # => "b"
473
- # a
474
- # # => ["a", "c"]
475
- # a.delete("z")
476
- # # => nil
477
- # a.delete("z") { "not found" }
478
- # # => "not found"
479
- #
480
- # @param [Object] obj object to delete
481
- # @return [Object, nil] returns obj or nil
482
- def delete(obj)
483
- `var size = #{self}.length;
484
- for (var i = 0; i < #{self}.length; i++) {
485
- if (#{self}[i]['$=='](#{obj}).r) {
486
- #{self}.splice(i, 1);
487
- i--;
488
- }
489
- }`
490
- `return size == #{self}.length ? #{nil} : #{obj};`
491
- end
492
-
493
- # Deletes the element at the specified index, returning that element, or `nil`
494
- # if the index is out of range. See also Array#slice!.
495
- #
496
- # @example
497
- # a = ["ant", "bat", "cat", "dog"]
498
- # a.delete_at(2)
499
- # # => "cat"
500
- # a
501
- # # => ["ant", "bat", "dog"]
502
- # a.delete_at(99)
503
- # # => 99
504
- #
505
- # @param [Number] index the index to delete
506
- # @return [Object, nil] returns obj at index or nil
507
- def delete_at(index)
508
- `if (#{index} < 0 || #{index} >= #{self}.length) return #{nil};
509
- var res = #{self}[#{index}];
510
- #{self}.splice(#{index}, 1);
511
- return res;`
512
- end
513
-
514
- # Deletes every element of `self` for which `block` evaluates to true. See
515
- # also Array#reject!.
516
- #
517
- # If no block is given, an enumerator is returned instead.
518
- #
519
- # @note no enumerator is currently returned.
520
- #
521
- # @example
522
- # a = [1, 2, 3]
523
- # a.delete_if { |x| x >= 2 }
524
- # # => [1]
525
- #
526
- # @return [Array] returns amended receiver
527
- def delete_if(&block)
528
- `for (var i = 0; i < #{self}.length; i++) {
529
- try {
530
- var res = #{block}.apply(#{block}.__self__, [#{self}[i]]);
531
- if (res.r) {
532
- #{self}.splice(i, 1);
533
- i--;
534
- }
535
- }
536
- catch (e) {
537
- throw "Array#delete_if catch not implemented yet"
538
- }
539
- }`
540
- self
541
- end
542
-
543
- # Drop first `n` elements from receiver, and returns rest elements in array.
544
- #
545
- # @example
546
- # a = [1, 2, 3, 4, 5, 0]
547
- # a.drop 3
548
- # # => [4, 5, 0]
549
- #
550
- # @param [Number] n number to drop
551
- # @return [Array] returns a new array
552
- def drop(n)
553
- `if (#{n} > #{self}.length) return [];`
554
- `var result = [];`
555
- `for (var i = #{n}; i < (#{self}.length); i++) {
556
- result.push(#{self}[i]);
557
- }`
558
- `return result;`
559
- end
560
-
561
- # Drop elements up to, but not including, the first element for which block
562
- # returns `nil` or `false` and returns an array containing the remaining
563
- # elements.
564
- #
565
- # If no block is given, an enumerator is returned instead.
566
- #
567
- # @note returning an enumerator is not yet implemented
568
- #
569
- # @example
570
- # a = [1, 2, 3, 4, 5, 0]
571
- # a.drop_while { |i| i < 3 }
572
- # # => [3, 4, 5, 0]
573
- #
574
- # @return [Array] returns new array
575
- def drop_while(&block)
576
- `var result = []
577
- for (var i = 0; i < #{self}.length; i++) {
578
- try {
579
- var res = #{block}.apply(#{block}.__self__, [#{self}[i]]);
580
- if (!res.r) {
581
- result = #{self}.slice(i);
582
- break;
583
- }
584
- }
585
- catch (e) {
586
- throw "Array#delete_if catch not implemented yet"
587
- }
588
- }
589
- return result;`
590
- end
591
-
592
- # Calls block once for each element in `self`, passing that element as a
593
- # parameter.
594
- #
595
- # If no block is given, an enumerator is returned instead.
596
- #
597
- # @note enumerator functionality not yet implemented
598
- #
599
- # @example
600
- # a = ["a", "b", "c"]
601
- # a.each { |x| puts x }
602
- # # => "a"
603
- # # => "b"
604
- # # => "c"
605
- #
606
- # @return [Array] returns the receiver
607
- def each(&block)
608
- `for (var i = 0; i < #{self}.length; i++) {
609
- try {
610
- #{block}.apply(#{block}.__self__, [#{self}[i]]);
611
- } catch (e) {
612
- if (e.__keyword__ == 'redo') {
613
- i--;
614
- }
615
- else if (e.__keyword__ == 'break') {
616
- return e.opal_value;
617
- }
618
- else {
619
- throw e;
620
- }
621
- }
622
- }`
623
- self
624
- end
625
-
626
- # Same as {Array#each}, but passes the index of the element instead of the
627
- # element itself.
628
- #
629
- # If no block given, an enumerator is returned instead.
630
- #
631
- # @note enumerator functionality not yet implemented.
632
- #
633
- # @example
634
- # a = ["a", "b", "c"]
635
- # a.each_index { |x| puts x }
636
- # # => 0
637
- # # => 1
638
- # # => 2
639
- #
640
- # @return [Array] returns receiver
641
- def each_index(&block)
642
- `for (var i = 0; i < #{self}.length; i++) {
643
- try {
644
- #{block}.apply(#{block}.__self__, [i]);
645
- } catch (e) {
646
- if (e.__keyword__ == 'redo') {
647
- i--;
648
- }
649
- else if (e.__keyword__ == 'break') {
650
- return e.opal_value;
651
- }
652
- else {
653
- throw e;
654
- }
655
- }
656
- }`
657
- self
658
- end
659
-
660
- # Returns `true` if `self` contains no elements, `false` otherwise
661
- #
662
- # @example
663
- # [].empty?
664
- # # => true
665
- #
666
- # @return [Boolean] empty or not
667
- def empty?
668
- `return #{self}.length == 0 ? #{true} : #{false};`
669
- end
670
-
671
- alias_method :eql?, :==
672
-
673
- # Tries to return the element at position `index`. If the index lies outside
674
- # the array, the first form throws an `IndexError` exception, the second form
675
- # returns `default`, and the third form returns the value of invoking the
676
- # block, passing in the index. Negative values of `index` count from the end
677
- # of the array.
678
- #
679
- # @example First form
680
- # a = [11, 22, 33, 44]
681
- # a.fetch(1)
682
- # # => 22
683
- # a.fetch(-1)
684
- # # => 44
685
- #
686
- # @example Second form
687
- # a.fetch(4, 'cat')
688
- # # => "cat"
689
- #
690
- # @example Third form
691
- # a.fetch(4) { |i| i * i }
692
- # # => 16
693
- #
694
- # @param [Number] index
695
- # @param [Object] defaults
696
- # @return [Object] returns result
697
- def fetch(index, defaults, &block)
698
- `var idx = #{index};
699
- if (#{index} < 0) #{index} += #{self}.length;
700
- if (#{index} < 0 || #{index} >= #{self}.length) {
701
- if (#{defaults} === undefined) {
702
- throw "IndexError.."
703
- }
704
- else if (#{defaults}.info & #{self}.TP) {
705
- return #{defaults}.apply(#{defaults}.__self__, [idx]);
706
- }
707
- else {
708
- return #{defaults};
709
- }
710
- }
711
- return #{self}[#{index}];`
712
- end
713
-
714
- # Returns the index of the first object in `self` such that it is `==` to
715
- # `obj`. If a block is given instead of an argument, returns first object for
716
- # which `block` is true. Returns `nil` if no match is found. See also
717
- # Array#rindex.
718
- #
719
- # If neither a block nor an argument is given, an enumerator is returned
720
- # instead.
721
- #
722
- # @note enumerator functionality not yet implemented.
723
- #
724
- # @example
725
- # a = ["a", "b", "c"]
726
- # a.index("b")
727
- # # => 1
728
- # a.index("z")
729
- # # => nil
730
- # a.index { |x| x == "b" }
731
- # # => 1
732
- #
733
- # @param [Object] object to look for
734
- # @return [Number, nil] result
735
- def index(object)
736
- `if (#{object} === undefined) {
737
- throw "need to return enumerator"
738
- } else if (#{object}.info & #{self}.TP) {
739
- for (var i = 0; i < #{self}.length; i++) {
740
- if (#{object}.apply(#{object}.__self__, [#{self}[i]]).r) {
741
- return i;
742
- }
743
- }
744
- } else {
745
- for (var i = 0; i < #{self}.length; i++) {
746
- if (#{self}[i]['$=='](#{object}).r) {
747
- return i;
748
- }
749
- }
750
- }
751
- return #{nil};`
752
- end
753
-
754
- # Returns the first element, or the first `n` elements, of the array. If the
755
- # array is empty, the first form returns `nil`, and the second form returns an
756
- # empty array.
757
- #
758
- # @example
759
- # a = ["q", "r", "s", "t"]
760
- # a.first
761
- # # => "q"
762
- # a.first(2)
763
- # # => ["q", "r"]
764
- #
765
- # @param [Number] n number of elements
766
- # @return [Object, Array] object or array of objects
767
- def first(count=nil)
768
- if count
769
- `return #{self}.slice(0, #{count});`
770
- else
771
- `if (#{self}.length == 0) {
772
- return #{nil};
773
- }
774
- return #{self}[0];`
775
- end
776
- end
777
-
778
- # Returns a new array that is a one-dimensional flattening of this array
779
- # (recursively). That is, for every element that is an array, extract its
780
- # elements info the new array. If the optional `level` argument determines the
781
- # level of recursion to flatten.
782
- #
783
- # @example
784
- # s = [1, 2, 3]
785
- # # => [1, 2, 3]
786
- # t = [4, 5, 6, [7, 8]]
787
- # # => [4, 5, 6, [7, 8]]
788
- # a = [s, t, 9, 10]
789
- # # => [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
790
- # a.flatten
791
- # # => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
792
- # a = [1, 2, [3, [4, 5]]]
793
- # a.flatten(1)
794
- # # => [1, 2, 3, [4, 5]]
795
- #
796
- # @param [Number] level the level to flatten
797
- # @return [Array] returns new array
798
- def flatten(level = nil)
799
- `var result = [];
800
- for (var i = 0; i < #{self}.length; i++) {
801
- var item = #{self}[i];
802
- if (item.info & #{self}.TA) {
803
- if (#{level} == #{nil}) {
804
- result = result.concat(item.$flatten());
805
- }
806
- else if (#{level} == 0) {
807
- result.push(item);
808
- }
809
- else {
810
- result = result.concat(item.$flatten(#{level} - 1));
811
- }
812
- }
813
- else {
814
- result.push(item);
815
- }
816
- }
817
- return result;`
818
- end
819
-
820
- # Flattens `self` in place. Returns `nil` if no modifications were made (i.e.,
821
- # `ary` contains no subarrays.) If the optional `level` argument determines
822
- # the level of recursion to flatten.
823
- #
824
- # @todo current implementation is probably not all that ideal.. (efficiency)
825
- #
826
- # @example
827
- # a = [1, 2, [3, [4, 5]]]
828
- # a.flatten!
829
- # # => [1, 2, 3, 4, 5]
830
- # a.flatten!
831
- # # => nil
832
- # a
833
- # # => [1, 2, 3, 4, 5]
834
- #
835
- # @param [Number] level to flatten to
836
- # @return [Array] returns receiver
837
- def flatten!(level)
838
- length = `#{self}.length`
839
- result = flatten level
840
- clear
841
- concat result
842
- `if (#{self}.length == #{length}) {
843
- return #{nil};
844
- }`
845
- self
846
- end
847
-
848
- # Returns `true` if the given object is present in `self`, `false` otherwise.
849
- #
850
- # @example
851
- # a = ["a", "b", "c"]
852
- # a.include? "b"
853
- # # => true
854
- # a.include? "z"
855
- # # => false
856
- def include?(member)
857
- `for (var i = 0; i < #{self}.length; i++) {
858
- if (#{member}['$=='](#{self}[i]).r) {
859
- return #{true};
860
- }
861
- }`
862
- false
863
- end
864
-
865
- # Replaces the contents of `self` with the contents of `other_ary`, truncating
866
- # or expanding if necessary.
867
- #
868
- # @example
869
- # a = ["a", "b", "c", "d", "e"]
870
- # a.replace ["x", "y", "z"]
871
- # # => ["x", "y", "z"]
872
- # a
873
- # # => ["x", "y", "z"]
874
- #
875
- # @param [Array] other_ary array to replace with
876
- # @return [Array] returns receiver
877
- def replace(other_ary)
878
- `#{self}.splice(0, #{self}.length);
879
- for (var i = 0; i < #{other_ary}.length; i++) {
880
- #{self}.push(#{other_ary}[i]);
881
- }`
882
- self
883
- end
884
-
885
- # Inserts the given values before the element with the given index (which may
886
- # be negative).
887
- #
888
- # @example
889
- # a = ["a", "b", "c", "d"]
890
- # a.insert(2, 99)
891
- # # => ["a", "b", 99, "c", "d"]
892
- # a.insert(-2, 1, 2, 3)
893
- # # => ["a", "b", 99, "c", 1, 2, 3, "d"]
894
- #
895
- # @param [Number] index index for insertion
896
- # @param [Object] obj objects to insert
897
- # @return [Array] returns the receiver
898
- def insert(index, *obj)
899
- # should we add 1??? dont know?
900
- `if (#{index} < 0) #{index} += (#{self}.length + 1);
901
- if (#{index} < 0 || #{index} >= #{self}.length) {
902
- throw "IndexError... out of range"
903
- }
904
- #{self}.splice.apply(#{self}, [#{index}, 0].concat(#{obj}));`
905
- self
906
- end
907
-
908
- # Returns a string created by converting each element of the array to a string
909
- # separated by `sep`.
910
- #
911
- # @example
912
- # ["a", "b", "c"].join
913
- # # => "abc"
914
- # ["a", "b", "c"].join '-'
915
- # "a-b-c"
916
- #
917
- # @param [String] sep the separator
918
- # @return [String] joined string
919
- def join(sep = "")
920
- `var result = [];
921
- for (var i = 0; i < #{self}.length; i++) {
922
- result.push(#{self}[i].$to_s());
923
- }
924
- return result.join(#{sep});`
925
- end
926
-
927
- # Deletes every element of `self` for which `block` evaluates to false. See
928
- # also Array#select!
929
- #
930
- # If no block given, an enumerator is returned instead.
931
- #
932
- # @todo No enumerator currently returned.
933
- #
934
- # @example
935
- # a = [1, 2, 3, 4, 5, 6]
936
- # a.keep_if { |x| x < 4 }
937
- # # => [1, 2, 3]
938
- #
939
- # @return [Array] receiver
940
- def keep_if(&block)
941
- `for (var i = 0; i < #{self}.length; i++) {
942
- try {
943
- var res = #{block}.apply(#{block}.__self__, [#{self}[i]]);
944
- if (!res.r) {
945
- #{self}.splice(i, 1);
946
- i--;
947
- }
948
- }
949
- catch (e) {
950
- throw "Array#keep_if catch not implemented yet"
951
- }
952
- }`
953
- self
954
- end
955
-
956
- # Return the last element(s) of `self`. If the array is empty, the first form
957
- # returns `nil`.
958
- #
959
- # @example
960
- # a = ["w", "x", "y", "z"]
961
- # a.last
962
- # # => "z"
963
- # a.last(2)
964
- # # => ["y", "z"]
965
- #
966
- # @param [Number] n number of items to get
967
- # @return [Object, Array] result
968
- def last(n = nil)
969
- if n
970
- `return #{self}.slice(#{self}.length - #{n}, #{self}.length);`
971
- else
972
- `if (#{self}.length == 0) {
973
- return #{nil};
974
- }
975
- return #{self}[#{self}.length - 1];`
976
- end
977
- end
978
-
979
- # Returns the number of elements in `self`. May be zero.
980
- #
981
- # @example
982
- # [1, 2, 3, 4, 5].length
983
- # # => 5
984
- #
985
- # @return [Number] length
986
- def length
987
- `return #{self}.length;`
988
- end
989
-
990
- alias_method :size, :length
991
-
992
- # Removes the last element from `self` and returns it, or `nil` if array is
993
- # empty.
994
- #
995
- # If a number `n` is given, returns an array of the last n elements (or less)
996
- # just like `array.slice!(-n, n) does.
997
- #
998
- # @example
999
- # a = ["a", "b", "c", "d"]
1000
- # a.pop
1001
- # # => "d"
1002
- # a.pop(2)
1003
- # # => ["b", "c"]
1004
- # a
1005
- # # => ["a"]
1006
- #
1007
- # @param [Number] n number to pop
1008
- # @return [Array] returns receiver
1009
- def pop(n = nil)
1010
- if n
1011
- `return #{self}.splice(#{self}.length - #{n}, #{self}.length);`
1012
- else
1013
- `if (#{self}.length) {
1014
- return #{self}.pop();
1015
- }
1016
- return #{nil};`
1017
- end
1018
- end
1019
-
1020
- # Append - Pushes the given object(s) on to the end of this array. This
1021
- # expression returns the array itself, so several appends may be chained
1022
- # together.
1023
- #
1024
- # @example
1025
- # a = ["a", "b", "c"]
1026
- # a.push("d", "e", "f")
1027
- # # => ["a", "b", "c", "d", "e", "f"]
1028
- #
1029
- # @param [Object] obj object(s) to push
1030
- # @retrun [Array] returns receiver
1031
- def push(*obj)
1032
- `for (var i = 0; i < #{obj}.length; i++) {
1033
- #{self}.push(#{obj}[i]);
1034
- }`
1035
- self
1036
- end
1037
-
1038
- # Searches tthrough the array whose elements are also arrays. Comapres `obj`
1039
- # with the second element of each contained array using `==`. Returns the
1040
- # first contained array that matches. See also {Array#assoc}.
1041
- #
1042
- # @example
1043
- # a = [[1, "one"], [2, "two"], [3, "three"], ["ii", "two"]]
1044
- # a.rassoc("two")
1045
- # # => [2, "two"]
1046
- # a.rassoc("four")
1047
- # # => nil
1048
- #
1049
- # @param [Object] obj object to search for
1050
- # @return [Object, nil] result or nil
1051
- def rassoc(obj)
1052
- `for (var i = 0; i < #{self}.length; i++) {
1053
- var test = #{self}[i];
1054
- if (test.info & #{self}.TA && test[1] !== undefined && test[1]===#{obj}) {
1055
- return test;
1056
- }
1057
- }`
1058
- nil
1059
- end
1060
-
1061
- # Returns a new array containing the items in `self` for which the block is
1062
- # not true. See also {Array#delete_if}.
1063
- #
1064
- # If no block is given, an enumerator is returned instead.
1065
- #
1066
- # @note Enumerator functionality not yet implemented.
1067
- #
1068
- # @example
1069
- # a = [1, 2, 3, 4, 5, 6]
1070
- # a.reject { |x| x > 3 }
1071
- # # => [1, 2, 3]
1072
- # a
1073
- # # => [1, 2, 3, 4, 5, 6]
1074
- #
1075
- # @return [Array] returns array
1076
- def reject(&block)
1077
- `var result = [];
1078
- for (var i = 0; i < #{self}.length; i++) {
1079
- try {
1080
- var res = #{block}.apply(#{block}.__self__, [#{self}[i]]);
1081
- if (!res.r) {
1082
- result.push(#{self}[i]);
1083
- }
1084
- }
1085
- catch (e) {
1086
- throw "Array#reject catch not implemented yet"
1087
- }
1088
- }
1089
- return result;`
1090
- end
1091
-
1092
- # Equivalent to {Array#delete_if}, deleting elements from `self` for which the
1093
- # block evaluates to true, but returns `nil` if no changes were made. See also
1094
- # {Array#delete_if}.
1095
- #
1096
- # If no block is given, an enumerator is returned instead.
1097
- #
1098
- # @note Enumerator functionality is not yet implemented.
1099
- #
1100
- # @example
1101
- # a = [1, 2, 3, 4, 5, 6]
1102
- # a.reject! { |x| x > 3 }
1103
- # # => [1, 2, 3]
1104
- # a.reject! { |x| x > 3 }
1105
- # # => nil
1106
- # a
1107
- # # => [1, 2, 3]
1108
- #
1109
- # @return [Array] returns receiver
1110
- def reject!(&block)
1111
- `var length = #{self}.length;
1112
- for (var i = 0; i < #{self}.length; i++) {
1113
- try {
1114
- var res = #{block}.apply(#{block}.__self__, [#{self}[i]]);
1115
- if (res.r) {
1116
- #{self}.splice(i, 1);
1117
- i--;
1118
- }
1119
- }
1120
- catch (e) {
1121
- throw "Array#reject catch not implemented yet"
1122
- }
1123
- }
1124
- return #{self}.length == length ? #{nil} : #{self};`
1125
- end
1126
-
1127
- # Returns a new array containing `self`'s elements in reverse order.
1128
- #
1129
- # @example
1130
- # ["a", "b", "c"].reverse
1131
- # # => ["c", "b", "a"]
1132
- # [1].reverse
1133
- # # => [1]
1134
- #
1135
- # @return [Array] reversed array
1136
- def reverse
1137
- `var result = [];
1138
- for (var i = #{self}.length - 1; i >= 0; i--) {
1139
- result.push(#{self}[i]);
1140
- }
1141
- return result;`
1142
- end
1143
-
1144
- # Reverses `self` in place.
1145
- #
1146
- # @example
1147
- # a = ["a", "b", "c"]
1148
- # a.reverse!
1149
- # # => ["c", "b", "a"]
1150
- # a
1151
- # # => ["c", "b", "a"]
1152
- #
1153
- # @return [Array] returns receiver
1154
- def reverse!
1155
- `return #{self}.reverse();`
1156
- end
1157
-
1158
- # Same as {Array#each}, but traverses `self` in reverse order.
1159
- #
1160
- # @example
1161
- # a = ["a", "b", "c"]
1162
- # a.reverse_each { |x| puts x }
1163
- # # => "c"
1164
- # # => "b"
1165
- # # => "a"
1166
- #
1167
- # @return [Array] returns receiver
1168
- def reverse_each(&block)
1169
- `for (var i = #{self}.length - 1; i >= 0; i--) {
1170
- try {
1171
- #{block}.apply(#{block}.__self__, [#{self}[i]]);
1172
- } catch (e) {
1173
- if (e.__keyword__ == 'redo') {
1174
- i++;
1175
- }
1176
- else if (e.__keyword__ == 'break') {
1177
- return e.opal_value;
1178
- }
1179
- else {
1180
- throw e;
1181
- }
1182
- }
1183
- }`
1184
- self
1185
- end
1186
-
1187
- # Returns the index of the last object in `self` == to `object`. If a block is
1188
- # given instead of an argument, returns the first object for which `block` is
1189
- # true, starting from the last object. Returns `nil` if no match is found. See
1190
- # also {Array#index}.
1191
- #
1192
- # @example
1193
- # a = ["a", "b", "b", "b", "c"]
1194
- # a.rindex("b")
1195
- # # => 3
1196
- # a.rindex("z")
1197
- # # => nil
1198
- # a.rindex { |x| x == "b" }
1199
- # # => 3
1200
- #
1201
- # @return [Object, nil] returns result or nil
1202
- def rindex(object)
1203
- `if (#{object} === undefined) {
1204
- throw "need to return enumerator"
1205
- } else if (#{object}.info & #{self}.TP) {
1206
- for (var i = #{self}.length - 1; i > 0; i--) {
1207
- if (#{object}.apply(#{object}.__self__, [#{self}[i]]).r) {
1208
- return i;
1209
- }
1210
- }
1211
- } else {
1212
- for (var i = #{self}.length - 1; i > 0; i--) {
1213
- if (#{self}[i]['$=='](#{object}).r) {
1214
- return i;
1215
- }
1216
- }
1217
- }
1218
- return #{nil};`
1219
- end
1220
-
1221
- # Invokes the block passing in successive elements from `self`, returning an
1222
- # array containing those elements for which the block returns a true value.
1223
- #
1224
- # @note enumerator functionality is not yet implemented.
1225
- #
1226
- # @example
1227
- # a = [1, 2, 3, 4, 5, 6]
1228
- # a.select { |x| x > 4 }
1229
- # # => [5, 6]
1230
- #
1231
- # @return [Array] returns array
1232
- def select(&block)
1233
- `var result = [];
1234
- for (var i = 0; i < #{self}.length; i++) {
1235
- try {
1236
- var res = #{block}.apply(#{block}.__self__, [#{self}[i]]);
1237
- if (res.r) {
1238
- result.push(#{self}[i]);
1239
- }
1240
- }
1241
- catch (e) {
1242
- throw "Array#select catch not implemented yet"
1243
- }
1244
- }
1245
- return result;`
1246
- end
1247
-
1248
- # Invokes the block passing in successive elements from `self`, deleting the
1249
- # elements for which the block returns a false value. It returns `self` if
1250
- # changes were made, otherwise it returns `nil`. See also {Array#keep_if}.
1251
- #
1252
- # If no block is given, an enumerator is returned instead.
1253
- #
1254
- # @note Enumerator functionality not yet implemented.
1255
- #
1256
- # @example
1257
- # a = [1, 2, 3, 4, 5, 6]
1258
- # a.select! { |x| x > 4 }
1259
- # # => [5, 6]
1260
- # a.select! { |x| x > 4 }
1261
- # # => nil
1262
- # a
1263
- # # => [5, 6]
1264
- #
1265
- # @return [Array] returns receiver
1266
- def select!(&block)
1267
- `var length = #{self}.length;
1268
- for (var i = 0; i < #{self}.length; i++) {
1269
- try {
1270
- var res = #{block}.apply(#{block}.__self__, [#{self}[i]]);
1271
- if (!res.r) {
1272
- #{self}.splice(i, 1);
1273
- i--;
1274
- }
1275
- }
1276
- catch (e) {
1277
- throw "Array#select! catch not implemented yet"
1278
- }
1279
- }
1280
- return #{self}.length == length ? #{nil} : #{self};`
1281
- end
1282
-
1283
- # Returns the first element of `self` and removes it (shifting all other
1284
- # elements down by one). Returns `nil` if the array is empty.
1285
- #
1286
- # If a number `n` is given, returns an array of the first n elements (or less)
1287
- # just like array.slice!(0, n) does.
1288
- #
1289
- # @example
1290
- # a = ["a", "b", "c"]
1291
- # a.shift
1292
- # # => "a"
1293
- # a
1294
- # # => ["b", "c"]
1295
- # a = ["a", "b", "c"]
1296
- # a.shift(2)
1297
- # # => ["b", "c"]
1298
- # a
1299
- # # => ["c"]
1300
- #
1301
- # @param [Number] n elements to shift
1302
- # @return [Array] result
1303
- def shift(n = nil)
1304
- if n
1305
- `return #{self}.splice(0, #{n});`
1306
- else
1307
- `if (#{self}.length) {
1308
- return #{self}.shift();
1309
- }
1310
- return #{nil};`
1311
- end
1312
- end
1313
-
1314
- # Deletes the element(s) given by an `index` (optionally with a length) or by
1315
- # a range. Returns the deleted object(s), or `nil` if the index is out of
1316
- # range.
1317
- #
1318
- # @example
1319
- # a = ["a", "b", "c"]
1320
- # a.slice!(1)
1321
- # # => "b"
1322
- # a
1323
- # # => ["a", "c"]
1324
- # a.slice!(-1)
1325
- # # => "c"
1326
- # a
1327
- # # => ["a"]
1328
- # a.slice!(100)
1329
- # # => nil
1330
- # a
1331
- # # => ["a"]
1332
- #
1333
- # @todo Does not yet work with ranges.
1334
- #
1335
- # @param [Range, Number] index to begin with
1336
- # @param [Number] length last index
1337
- # @return [Array, nil] result
1338
- def slice!(index, length = nil)
1339
- size = `#{self}.length;`
1340
-
1341
- if index.is_a? Range
1342
- raise "need to implement range"
1343
- else
1344
- `if (#{index} < 0) #{index} += #{size};`
1345
- end
1346
- `if (#{index} >= #{size} || #{index} < 0) return #{nil};`
1347
-
1348
- if length
1349
- `if (#{length} <= 0 || #{length} > #{self}.length) return #{nil};`
1350
- `return #{self}.splice(#{index}, #{index} + #{length});`
1351
- else
1352
- `return #{self}.splice(#{index}, 1)[0];`
1353
- end
1354
- end
1355
-
1356
- # Returns first `n` elements from ary.
1357
- #
1358
- # @example
1359
- # a = [1, 2, 3, 4, 5, 0]
1360
- # a.take(3)
1361
- # # => [1, 2, 3]
1362
- #
1363
- # @return [Array] array of elements
1364
- def take(n)
1365
- `return #{self}.slice(0, #{n});`
1366
- end
1367
-
1368
- # Passes elements to the block until the block returns `nil` or `false`, then
1369
- # stops iterating and returns an array of all prior elements.
1370
- #
1371
- # If no block given, an enumerator is returned instead.
1372
- #
1373
- # @todo Enumerator functionality not yet implemented.
1374
- #
1375
- # @example
1376
- # a = [1, 2, 3, 4, 5, 6]
1377
- # a.take_while { |i| i < 3 }
1378
- # # => [1, 2]
1379
- #
1380
- # @return [Array] array with elements
1381
- def take_while(&block)
1382
- `var result = [];
1383
- for (var i = 0; i < #{self}.length; i++) {
1384
- try {
1385
- var res = #{block}.apply(#{block}.__self__, [#{self}[i]]);
1386
- if (res.r) {
1387
- result.push(#{self}[i]);
1388
- } else {
1389
- break;
1390
- }
1391
- }
1392
- catch (e) {
1393
- throw "Array#take_while catch not implemented yet"
1394
- }
1395
- }
1396
- return result;`
1397
- end
1398
-
1399
- # Returns self.
1400
- #
1401
- # @example
1402
- # a = [1, 2, 3]
1403
- # a.to_a
1404
- # # => [1, 2, 3]
1405
- #
1406
- # @return [Array] returns the receiver
1407
- def to_a
1408
- self
1409
- end
1410
-
1411
- # Returns self.
1412
- #
1413
- # @example
1414
- # a = [1, 2, 3]
1415
- # a.to_ary
1416
- # # => [1, 2, 3]
1417
- #
1418
- # @return [Array] returns the receiver
1419
- def to_ary
1420
- self
1421
- end
1422
-
1423
- # Returns a new array by removing duplicate values in `self`.
1424
- #
1425
- # @example
1426
- # a = ["a", "a", "b", "b", "c"]
1427
- # a.uniq
1428
- # # => ["a", "b", "c"]
1429
- # a
1430
- # # => ["a", "a", "b", "b", "c"]
1431
- #
1432
- # @return [Array]
1433
- def uniq
1434
- `var result = [], seen = [];
1435
- for (var i = 0; i < #{self}.length; i++) {
1436
- var test = #{self}[i], hash = test.hash().toString();
1437
- if (seen.indexOf(hash) == -1) {
1438
- seen.push(hash);
1439
- result.push(test);
1440
- }
1441
- }
1442
- return result;`
1443
- end
1444
-
1445
- # Removes duplicate elements from `self`. Returns `nil` if no changes are
1446
- # made (that is, no duplicates are found).
1447
- #
1448
- # @example
1449
- # a = ["a", "a", "b", "b", "c"]
1450
- # a.uniq!
1451
- # # => ["a", "b", "c"]
1452
- # a.uniq!
1453
- # # => nil
1454
- #
1455
- # @return [Array] returns receiver
1456
- def uniq!
1457
- `var seen = [], length = #{self}.length;
1458
- for (var i = 0; i < #{self}.length; i++) {
1459
- var test = #{self}[i], hash = test.hash().toString();
1460
- if (seen.indexOf(hash) == -1) {
1461
- seen.push(hash);
1462
- } else {
1463
- #{self}.splice(i, 1);
1464
- i--;
1465
- }
1466
- }
1467
- return #{self}.length == length ? #{nil} : #{self};`
1468
- end
1469
-
1470
- # Prepends objects to the front of `self`, moving other elements upwards.
1471
- #
1472
- # @example
1473
- # a = ["b", "c", "d"]
1474
- # a.unshift("a")
1475
- # # => ["a", "b", "c", "d"]
1476
- # a.unshift(1, 2)
1477
- # # => [1, 2, "a", "b", "c", "d"]
1478
- #
1479
- # @param [Object] object objects to add
1480
- # @return [Array] returns receiver
1481
- def unshift(*object)
1482
- `for (var i = #{object}.length - 1; i >= 0 ; i--) {
1483
- #{self}.unshift(#{object}[i]);
1484
- }`
1485
- self
1486
- end
1487
-
1488
- def each_with_index(&block)
1489
- `for (var i = 0; i < #{self}.length; i++) {
1490
- try {
1491
- #{block}.apply(#{block}.__self__, [#{self}[i], i]);
1492
- } catch (e) {
1493
- if (e.__keyword__ == 'redo') {
1494
- i--;
1495
- }
1496
- else if (e.__keyword__ == 'break') {
1497
- return e.opal_value;
1498
- }
1499
- else {
1500
- throw e;
1501
- }
1502
- }
1503
- }`
1504
- self
1505
- end
1506
-
1507
- def inspect
1508
- description = ["["]
1509
- self.each_with_index do |item, index|
1510
- description << ", " if index > 0
1511
- description << item.inspect
1512
- end
1513
- description << "]"
1514
- description.join ""
1515
- end
1516
- end