opal 0.3.18 → 0.3.19

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 (259) hide show
  1. data/.gitignore +1 -1
  2. data/Gemfile +1 -3
  3. data/README.md +472 -10
  4. data/Rakefile +10 -52
  5. data/core/array.rb +9 -14
  6. data/core/basic_object.rb +7 -10
  7. data/core/boolean.rb +5 -1
  8. data/core/class.rb +15 -38
  9. data/core/dir.rb +89 -0
  10. data/core/enumerable.rb +133 -57
  11. data/core/error.rb +15 -1
  12. data/core/file.rb +85 -0
  13. data/core/hash.rb +186 -32
  14. data/core/kernel.rb +30 -31
  15. data/core/load_order +4 -2
  16. data/core/module.rb +42 -62
  17. data/core/numeric.rb +7 -1
  18. data/core/object.rb +1 -1
  19. data/core/proc.rb +6 -2
  20. data/core/range.rb +16 -28
  21. data/core/regexp.rb +3 -3
  22. data/core/runtime.js +281 -350
  23. data/core/string.rb +100 -110
  24. data/docs/CNAME +1 -0
  25. data/docs/Rakefile +55 -0
  26. data/docs/css/styles.css +50 -0
  27. data/docs/css/syntax.css +63 -0
  28. data/docs/layout/post.html +3 -0
  29. data/docs/layout/pre.html +11 -0
  30. data/examples/dependencies/app.rb +3 -0
  31. data/lib/opal.rb +2 -1
  32. data/lib/opal/builder.rb +36 -10
  33. data/lib/opal/builder_task.rb +51 -24
  34. data/lib/opal/grammar.rb +2509 -2439
  35. data/lib/opal/grammar.y +38 -5
  36. data/lib/opal/lexer.rb +18 -2
  37. data/lib/opal/parser.rb +375 -349
  38. data/lib/opal/scope.rb +24 -2
  39. data/lib/opal/version.rb +1 -1
  40. data/spec/builder/build_order_spec.rb +20 -0
  41. data/spec/builder/lib_name_for_spec.rb +24 -0
  42. data/spec/grammar/call_spec.rb +9 -6
  43. data/spec/grammar/lambda_spec.rb +64 -0
  44. data/spec/grammar/sclass_spec.rb +5 -3
  45. data/{core/spec → test}/core/array/allocate_spec.rb +0 -0
  46. data/{core/spec → test}/core/array/append_spec.rb +0 -0
  47. data/{core/spec → test}/core/array/assoc_spec.rb +0 -0
  48. data/{core/spec → test}/core/array/at_spec.rb +0 -0
  49. data/{core/spec → test}/core/array/clear_spec.rb +0 -0
  50. data/{core/spec → test}/core/array/clone_spec.rb +0 -0
  51. data/{core/spec → test}/core/array/collect_spec.rb +0 -0
  52. data/{core/spec → test}/core/array/compact_spec.rb +0 -0
  53. data/{core/spec → test}/core/array/concat_spec.rb +0 -0
  54. data/{core/spec → test}/core/array/constructor_spec.rb +0 -0
  55. data/{core/spec → test}/core/array/count_spec.rb +0 -0
  56. data/{core/spec → test}/core/array/delete_at_spec.rb +0 -0
  57. data/{core/spec → test}/core/array/delete_if_spec.rb +0 -0
  58. data/{core/spec → test}/core/array/delete_spec.rb +0 -0
  59. data/{core/spec → test}/core/array/each_index_spec.rb +0 -0
  60. data/{core/spec → test}/core/array/each_spec.rb +0 -0
  61. data/{core/spec → test}/core/array/element_reference_spec.rb +0 -0
  62. data/{core/spec → test}/core/array/empty_spec.rb +0 -0
  63. data/{core/spec → test}/core/array/eql_spec.rb +0 -0
  64. data/{core/spec → test}/core/array/fetch_spec.rb +0 -0
  65. data/{core/spec → test}/core/array/first_spec.rb +0 -0
  66. data/{core/spec → test}/core/array/flatten_spec.rb +0 -0
  67. data/{core/spec → test}/core/array/include_spec.rb +0 -0
  68. data/{core/spec → test}/core/array/insert_spec.rb +0 -0
  69. data/{core/spec → test}/core/array/last_spec.rb +0 -0
  70. data/{core/spec → test}/core/array/length_spec.rb +0 -0
  71. data/{core/spec → test}/core/array/map_spec.rb +0 -0
  72. data/{core/spec → test}/core/array/plus_spec.rb +0 -0
  73. data/{core/spec → test}/core/array/pop_spec.rb +0 -0
  74. data/{core/spec → test}/core/array/push_spec.rb +0 -0
  75. data/{core/spec → test}/core/array/rassoc_spec.rb +0 -0
  76. data/{core/spec → test}/core/array/reject_spec.rb +0 -0
  77. data/{core/spec → test}/core/array/replace_spec.rb +0 -0
  78. data/{core/spec → test}/core/array/reverse_each_spec.rb +0 -0
  79. data/{core/spec → test}/core/array/reverse_spec.rb +0 -0
  80. data/{core/spec → test}/core/array/size_spec.rb +0 -0
  81. data/{core/spec → test}/core/array/to_ary_spec.rb +0 -0
  82. data/{core/spec → test}/core/array/uniq_spec.rb +0 -0
  83. data/{core/spec → test}/core/array/zip_spec.rb +0 -0
  84. data/test/core/class/fixtures/classes.rb +9 -0
  85. data/test/core/class/new_spec.rb +108 -0
  86. data/{core/spec → test}/core/enumerable/all_spec.rb +0 -0
  87. data/{core/spec → test}/core/enumerable/any_spec.rb +0 -0
  88. data/{core/spec → test}/core/enumerable/collect_spec.rb +0 -0
  89. data/{core/spec → test}/core/enumerable/count_spec.rb +0 -0
  90. data/test/core/enumerable/detect_spec.rb +48 -0
  91. data/test/core/enumerable/drop_spec.rb +17 -0
  92. data/test/core/enumerable/drop_while_spec.rb +24 -0
  93. data/test/core/enumerable/each_with_index_spec.rb +11 -0
  94. data/test/core/enumerable/each_with_object_spec.rb +17 -0
  95. data/test/core/enumerable/entries_spec.rb +6 -0
  96. data/test/core/enumerable/find_all_spec.rb +13 -0
  97. data/test/core/enumerable/find_index_spec.rb +45 -0
  98. data/test/core/enumerable/find_spec.rb +48 -0
  99. data/test/core/enumerable/first_spec.rb +40 -0
  100. data/{core/spec → test}/core/enumerable/fixtures/classes.rb +19 -0
  101. data/test/core/enumerable/grep_spec.rb +21 -0
  102. data/test/core/enumerable/take_spec.rb +40 -0
  103. data/test/core/enumerable/to_a_spec.rb +6 -0
  104. data/{core/spec → test}/core/false/and_spec.rb +0 -0
  105. data/{core/spec → test}/core/false/inspect_spec.rb +0 -0
  106. data/{core/spec → test}/core/false/or_spec.rb +0 -0
  107. data/{core/spec → test}/core/false/to_s_spec.rb +0 -0
  108. data/{core/spec → test}/core/false/xor_spec.rb +0 -0
  109. data/test/core/file/expand_path_spec.rb +20 -0
  110. data/{core/spec → test}/core/hash/allocate_spec.rb +0 -0
  111. data/{core/spec → test}/core/hash/assoc_spec.rb +0 -0
  112. data/{core/spec → test}/core/hash/clear_spec.rb +0 -0
  113. data/{core/spec → test}/core/hash/clone_spec.rb +0 -0
  114. data/test/core/hash/default_spec.rb +9 -0
  115. data/{core/spec → test}/core/hash/delete_if_spec.rb +0 -0
  116. data/test/core/hash/each_key_spec.rb +15 -0
  117. data/test/core/hash/each_pair_spec.rb +30 -0
  118. data/test/core/hash/each_spec.rb +30 -0
  119. data/test/core/hash/each_value_spec.rb +15 -0
  120. data/{core/spec → test}/core/hash/element_reference_spec.rb +14 -0
  121. data/{core/spec → test}/core/hash/element_set_spec.rb +1 -0
  122. data/test/core/hash/empty_spec.rb +10 -0
  123. data/test/core/hash/fetch_spec.rb +24 -0
  124. data/test/core/hash/flatten_spec.rb +46 -0
  125. data/test/core/hash/has_key_spec.rb +24 -0
  126. data/test/core/hash/has_value_spec.rb +12 -0
  127. data/test/core/hash/include_spec.rb +24 -0
  128. data/test/core/hash/index_spec.rb +13 -0
  129. data/test/core/hash/indexes_spec.rb +9 -0
  130. data/test/core/hash/indices_spec.rb +9 -0
  131. data/test/core/hash/invert_spec.rb +12 -0
  132. data/test/core/hash/keep_if_spec.rb +18 -0
  133. data/test/core/hash/key_spec.rb +24 -0
  134. data/test/core/hash/keys_spec.rb +10 -0
  135. data/test/core/hash/length_spec.rb +10 -0
  136. data/test/core/hash/member_spec.rb +24 -0
  137. data/{core/spec → test}/core/hash/merge_spec.rb +0 -0
  138. data/{core/spec → test}/core/hash/new_spec.rb +0 -0
  139. data/test/core/hash/rassoc_spec.rb +34 -0
  140. data/test/core/hash/replace_spec.rb +7 -0
  141. data/test/core/hash/select_spec.rb +52 -0
  142. data/test/core/hash/shift_spec.rb +19 -0
  143. data/test/core/hash/size_spec.rb +10 -0
  144. data/test/core/hash/update_spec.rb +17 -0
  145. data/test/core/hash/value_spec.rb +12 -0
  146. data/test/core/hash/values_at_spec.rb +9 -0
  147. data/test/core/hash/values_spec.rb +7 -0
  148. data/test/core/kernel/eql_spec.rb +15 -0
  149. data/test/core/kernel/equal_value_spec.rb +12 -0
  150. data/test/core/kernel/loop_spec.rb +23 -0
  151. data/test/core/kernel/nil_spec.rb +7 -0
  152. data/test/core/kernel/proc_spec.rb +9 -0
  153. data/test/core/kernel/rand_spec.rb +14 -0
  154. data/test/core/kernel/respond_to_spec.rb +24 -0
  155. data/test/core/kernel/send_spec.rb +56 -0
  156. data/test/core/kernel/tap_spec.rb +10 -0
  157. data/test/core/kernel/to_s_spec.rb +5 -0
  158. data/{core/spec → test}/core/matchdata/to_a_spec.rb +0 -0
  159. data/{core/spec → test}/core/nil/and_spec.rb +0 -0
  160. data/{core/spec → test}/core/nil/inspect_spec.rb +0 -0
  161. data/{core/spec → test}/core/nil/nil_spec.rb +0 -0
  162. data/{core/spec → test}/core/nil/or_spec.rb +0 -0
  163. data/{core/spec → test}/core/nil/to_a_spec.rb +0 -0
  164. data/{core/spec → test}/core/nil/to_f_spec.rb +0 -0
  165. data/{core/spec → test}/core/nil/to_i_spec.rb +0 -0
  166. data/{core/spec → test}/core/nil/to_s_spec.rb +0 -0
  167. data/{core/spec → test}/core/nil/xor_spec.rb +0 -0
  168. data/{core/spec → test}/core/numeric/equal_value_spec.rb +0 -0
  169. data/test/core/range/begin_spec.rb +9 -0
  170. data/test/core/range/case_compare_spec.rb +16 -0
  171. data/test/core/range/end_spec.rb +9 -0
  172. data/{core/spec → test}/core/regexp/match_spec.rb +0 -0
  173. data/test/core/string/capitalize_spec.rb +10 -0
  174. data/test/core/string/casecmp_spec.rb +16 -0
  175. data/test/core/string/chomp_spec.rb +43 -0
  176. data/test/core/string/chop_spec.rb +10 -0
  177. data/test/core/string/chr_spec.rb +13 -0
  178. data/test/core/string/comparison_spec.rb +13 -0
  179. data/test/core/string/downcase_spec.rb +6 -0
  180. data/test/core/string/element_reference_spec.rb +72 -0
  181. data/test/core/string/empty_spec.rb +8 -0
  182. data/test/core/string/end_with_spec.rb +12 -0
  183. data/test/core/string/fixtures/classes.rb +3 -0
  184. data/test/core/string/gsub_spec.rb +17 -0
  185. data/test/core/string/include_spec.rb +12 -0
  186. data/test/core/string/intern_spec.rb +9 -0
  187. data/test/core/string/length_spec.rb +9 -0
  188. data/test/core/string/lstrip_spec.rb +7 -0
  189. data/test/core/string/match_spec.rb +27 -0
  190. data/test/core/string/next_spec.rb +10 -0
  191. data/test/core/string/ord_spec.rb +9 -0
  192. data/test/core/string/partition_spec.rb +10 -0
  193. data/test/core/string/reverse_spec.rb +7 -0
  194. data/test/core/string/rstrip_spec.rb +7 -0
  195. data/test/core/string/size_spec.rb +9 -0
  196. data/test/core/string/slice_spec.rb +72 -0
  197. data/test/core/string/split_spec.rb +5 -0
  198. data/test/core/string/start_with_spec.rb +12 -0
  199. data/test/core/string/strip_spec.rb +6 -0
  200. data/test/core/string/sub_spec.rb +22 -0
  201. data/test/core/string/succ_spec.rb +10 -0
  202. data/test/core/string/sum_spec.rb +5 -0
  203. data/test/core/string/swapcase_spec.rb +18 -0
  204. data/test/core/string/to_a_spec.rb +9 -0
  205. data/test/core/string/to_f_spec.rb +14 -0
  206. data/test/core/string/to_i_spec.rb +25 -0
  207. data/test/core/string/to_s_spec.rb +13 -0
  208. data/test/core/string/to_str_spec.rb +13 -0
  209. data/test/core/string/to_sym_spec.rb +9 -0
  210. data/test/core/string/upcase_spec.rb +6 -0
  211. data/test/core/symbol/to_proc_spec.rb +12 -0
  212. data/{core/spec → test}/core/true/and_spec.rb +0 -0
  213. data/{core/spec → test}/core/true/inspect_spec.rb +0 -0
  214. data/{core/spec → test}/core/true/or_spec.rb +0 -0
  215. data/{core/spec → test}/core/true/to_s_spec.rb +0 -0
  216. data/{core/spec → test}/core/true/xor_spec.rb +0 -0
  217. data/test/index.html +11 -0
  218. data/{core/spec → test}/language/alias_spec.rb +4 -0
  219. data/{core/spec → test}/language/and_spec.rb +0 -0
  220. data/{core/spec → test}/language/array_spec.rb +0 -0
  221. data/{core/spec → test}/language/block_spec.rb +0 -0
  222. data/{core/spec → test}/language/break_spec.rb +0 -0
  223. data/{core/spec → test}/language/case_spec.rb +0 -0
  224. data/{core/spec → test}/language/defined_spec.rb +0 -0
  225. data/{core/spec → test}/language/ensure_spec.rb +0 -0
  226. data/test/language/fixtures/yield.rb +23 -0
  227. data/{core/spec → test}/language/hash_spec.rb +0 -0
  228. data/{core/spec → test}/language/if_spec.rb +0 -0
  229. data/test/language/literal_lambda_spec.rb +47 -0
  230. data/{core/spec → test}/language/loop_spec.rb +0 -0
  231. data/{core/spec → test}/language/metaclass_spec.rb +0 -0
  232. data/{core/spec → test}/language/next_spec.rb +0 -0
  233. data/{core/spec → test}/language/or_spec.rb +0 -0
  234. data/{core/spec → test}/language/predefined_spec.rb +0 -0
  235. data/{core/spec → test}/language/regexp_spec.rb +0 -0
  236. data/{core/spec → test}/language/send_spec.rb +0 -0
  237. data/{core/spec → test}/language/singleton_class_spec.rb +0 -0
  238. data/{core/spec → test}/language/super_spec.rb +0 -0
  239. data/{core/spec → test}/language/symbol_spec.rb +0 -0
  240. data/{core/spec → test}/language/undef_spec.rb +0 -0
  241. data/{core/spec → test}/language/unless_spec.rb +0 -0
  242. data/{core/spec → test}/language/until_spec.rb +0 -0
  243. data/{core/spec → test}/language/variables_spec.rb +0 -0
  244. data/{core/spec → test}/language/while_spec.rb +0 -0
  245. data/test/language/yield_spec.rb +100 -0
  246. data/test/opal/array/subclassing_spec.rb +32 -0
  247. data/test/opal/class/bridge_class_spec.rb +37 -0
  248. data/test/opal/exception/subclassing_spec.rb +17 -0
  249. data/test/opal/runtime/_methods_spec.rb +48 -0
  250. data/test/opal/runtime/class_hierarchy_spec.rb +22 -0
  251. data/test/opal/runtime/def_spec.rb +23 -0
  252. data/test/opal/string/subclassing_spec.rb +26 -0
  253. data/test/spec_helper.rb +3 -0
  254. metadata +437 -111
  255. data/core/spec/core/class/new_spec.rb +0 -16
  256. data/core/spec/core/hash/default_spec.rb +0 -4
  257. data/core/spec/core/symbol/to_proc_spec.rb +0 -6
  258. data/core/spec/index.html +0 -11
  259. data/spec/builder/build_source_spec.rb +0 -52
@@ -1,14 +1,19 @@
1
- class String
1
+ class String < `String`
2
+ %x{
3
+ def._isString = true;
4
+ var string_class = this;
5
+ }
6
+
2
7
  include Comparable
3
8
 
4
9
  def self.try_convert(what)
5
10
  what.to_str
6
11
  rescue
7
- null
12
+ nil
8
13
  end
9
14
 
10
15
  def self.new(str = '')
11
- str.to_s
16
+ allocate str.to_s
12
17
  end
13
18
 
14
19
  def %(data)
@@ -43,7 +48,7 @@ class String
43
48
  def <=>(other)
44
49
  %x{
45
50
  if (typeof other !== 'string') {
46
- return null;
51
+ return nil;
47
52
  }
48
53
 
49
54
  return this > other ? 1 : (this < other ? -1 : 0);
@@ -75,7 +80,7 @@ class String
75
80
  def =~(other)
76
81
  %x{
77
82
  if (typeof other === 'string') {
78
- throw RubyTypeError.$new(null, 'string given');
83
+ throw RubyTypeError.$new('string given');
79
84
  }
80
85
 
81
86
  return #{other =~ self};
@@ -85,7 +90,29 @@ class String
85
90
  # TODO: implement range based accessors
86
91
  # TODO: implement regex based accessors
87
92
  def [](index, length = undefined)
88
- `this.substr(index, length)`
93
+ %x{
94
+ if (length == null) {
95
+ if (index < 0) {
96
+ index += this.length;
97
+ }
98
+
99
+ if (index >= this.length || index < 0) {
100
+ return nil;
101
+ }
102
+
103
+ return this.substr(index, 1);
104
+ }
105
+
106
+ if (index < 0) {
107
+ index += this.length + 1;
108
+ }
109
+
110
+ if (index > this.length || index < 0) {
111
+ return nil;
112
+ }
113
+
114
+ return this.substr(index, length);
115
+ }
89
116
  end
90
117
 
91
118
  def capitalize
@@ -116,11 +143,15 @@ class String
116
143
  end
117
144
 
118
145
  def chomp(separator = $/)
119
- if separator == "\n"
120
- sub(/(\n|\r|\r\n)$/, '')
121
- else
122
- sub(/#{Regexp.escape(separator)}$/, '')
123
- end
146
+ %x{
147
+ if (separator === "\\n") {
148
+ return this.replace(/(\\n|\\r|\\r\\n)$/, '');
149
+ }
150
+ else if (separator === "") {
151
+ return this.replace(/(\\n|\\r\\n)+$/, '');
152
+ }
153
+ return this.replace(new RegExp(separator + '$'), '');
154
+ }
124
155
  end
125
156
 
126
157
  def chop
@@ -131,18 +162,6 @@ class String
131
162
  `this.charAt(0)`
132
163
  end
133
164
 
134
- def count (*sets)
135
- raise NotImplementedError
136
- end
137
-
138
- def crypt
139
- raise NotImplementedError
140
- end
141
-
142
- def delete (*sets)
143
- raise NotImplementedErrois
144
- end
145
-
146
165
  def downcase
147
166
  `this.toLowerCase()`
148
167
  end
@@ -181,6 +200,10 @@ class String
181
200
 
182
201
  alias eql? ==
183
202
 
203
+ def equal?(val)
204
+ `this.toString() === val.toString()`
205
+ end
206
+
184
207
  def getbyte(index)
185
208
  `this.charCodeAt(index)`
186
209
  end
@@ -246,7 +269,7 @@ class String
246
269
  }
247
270
  }
248
271
 
249
- return result === -1 ? null : result;
272
+ return result === -1 ? nil : result;
250
273
  }
251
274
  end
252
275
 
@@ -289,7 +312,7 @@ class String
289
312
  end
290
313
 
291
314
  def lstrip
292
- `this.replace(/^\s*/, '')`
315
+ `this.replace(/^\\s*/, '')`
293
316
  end
294
317
 
295
318
  def match(pattern, pos = undefined, &block)
@@ -297,22 +320,28 @@ class String
297
320
  end
298
321
 
299
322
  def next
300
- `String.fromCharCode(this.charCodeAt(0) + 1)`
301
- end
323
+ %x{
324
+ if (this.length === 0) {
325
+ return "";
326
+ }
327
+
328
+ var initial = this.substr(0, this.length - 1);
329
+ var last = String.fromCharCode(this.charCodeAt(this.length - 1) + 1);
302
330
 
303
- def oct
304
- to_i 8
331
+ return initial + last;
332
+ }
305
333
  end
306
334
 
307
335
  def ord
308
336
  `this.charCodeAt(0)`
309
337
  end
310
338
 
311
- def partition(what)
339
+ def partition(str)
312
340
  %x{
313
- var result = this.split(what);
341
+ var result = this.split(str);
342
+ var splitter = (result[0].length === this.length ? "" : str);
314
343
 
315
- return [result[0], what.toString(), result.slice(1).join(what.toString())];
344
+ return [result[0], splitter, result.slice(1).join(str.toString())];
316
345
  }
317
346
  end
318
347
 
@@ -320,42 +349,8 @@ class String
320
349
  `this.split('').reverse().join('')`
321
350
  end
322
351
 
323
- def rpartition(what)
324
- raise NotImplementedError
325
- end
326
-
327
352
  def rstrip
328
- `this.replace(/\s*$/, '')`
329
- end
330
-
331
- def scan(pattern)
332
- if pattern.is_a?(String)
333
- pattern = /#{Regexp.escape(pattern)}/
334
- end
335
-
336
- result = []
337
- original = pattern
338
-
339
- %x{
340
- var pattern = pattern.toString(),
341
- options = pattern.substr(pattern.lastIndexOf('/') + 1) + 'g',
342
- regexp = pattern.substr(1, pattern.lastIndexOf('/') - 1);
343
-
344
- var matches = this.match(pattern);
345
-
346
- for (var i = 0, length = matches.length; i < length; i++) {
347
- var current = matches[i].match(/^\\(|[^\\\\]\\(/) ? matches[i] : matches[i].match(original);
348
-
349
- if (#{block_given?}) {
350
- #{yield current};
351
- }
352
- else {
353
- result.push(current);
354
- }
355
- }
356
- }
357
-
358
- block_given? ? self : result
353
+ `this.replace(/\\s*$/, '')`
359
354
  end
360
355
 
361
356
  alias size length
@@ -363,11 +358,7 @@ class String
363
358
  alias slice []
364
359
 
365
360
  def split(pattern = $; || ' ', limit = undefined)
366
- `this.split(pattern === ' ' ? strip : this, limit)`
367
- end
368
-
369
- def squeeze(*sets)
370
- raise NotImplementedError
361
+ `this.split(pattern, limit)`
371
362
  end
372
363
 
373
364
  def start_with?(*prefixes)
@@ -383,19 +374,22 @@ class String
383
374
  end
384
375
 
385
376
  def strip
386
- lstrip.rstrip
377
+ `this.replace(/^\\s*/, '').replace(/\\s*$/, '')`
387
378
  end
388
379
 
389
380
  def sub(pattern, replace = undefined, &block)
390
381
  %x{
391
- if (block !== null) {
382
+ if (typeof(replace) === 'string') {
383
+ return this.replace(pattern, replace);
384
+ }
385
+ if (block !== nil) {
392
386
  return this.replace(pattern, function(str) {
393
- $opal.match_data = arguments
387
+ //$opal.match_data = arguments
394
388
 
395
- return $yielder.call($context, null, str);
389
+ return block.call(__context, str);
396
390
  });
397
391
  }
398
- else if (#{Object === replace}) {
392
+ else if (replace != null) {
399
393
  if (#{replace.is_a?(Hash)}) {
400
394
  return this.replace(pattern, function(str) {
401
395
  var value = #{replace[str]};
@@ -426,7 +420,7 @@ class String
426
420
  var result = 0;
427
421
 
428
422
  for (var i = 0, length = this.length; i < length; i++) {
429
- result += this.charCodeAt(i) % ((1 << n) - 1);
423
+ result += (this.charCodeAt(i) % ((1 << n) - 1));
430
424
  }
431
425
 
432
426
  return result;
@@ -435,14 +429,38 @@ class String
435
429
 
436
430
  def swapcase
437
431
  %x{
438
- return this.replace(/([a-z]+)|([A-Z]+)/g, function($0,$1,$2) {
432
+ var str = this.replace(/([a-z]+)|([A-Z]+)/g, function($0,$1,$2) {
439
433
  return $1 ? $0.toUpperCase() : $0.toLowerCase();
440
434
  });
435
+
436
+ if (this._klass === string_class) {
437
+ return str;
438
+ }
439
+
440
+ return this._klass.$new(str);
441
+ }
442
+ end
443
+
444
+ def to_a
445
+ %x{
446
+ if (this.length === 0) {
447
+ return [];
448
+ }
449
+
450
+ return [this];
441
451
  }
442
452
  end
443
453
 
444
454
  def to_f
445
- `parseFloat(this)`
455
+ %x{
456
+ var result = parseFloat(this);
457
+
458
+ if (isNaN(result)) {
459
+ return 0;
460
+ }
461
+
462
+ return result;
463
+ }
446
464
  end
447
465
 
448
466
  def to_i(base = 10)
@@ -459,9 +477,9 @@ class String
459
477
 
460
478
  def to_proc
461
479
  %x{
462
- var self = this;
480
+ var self = this, jsid = mid_to_jsid(self);
463
481
 
464
- return function(iter, arg) { return arg[mid_to_jsid(self)](); };
482
+ return function(arg) { return arg[jsid](); };
465
483
  }
466
484
  end
467
485
 
@@ -473,37 +491,9 @@ class String
473
491
 
474
492
  alias to_sym intern
475
493
 
476
- def tr(from, to)
477
- raise NotImplementedError
478
- end
479
-
480
- def tr_s(from, to)
481
- raise NotImplementedError
482
- end
483
-
484
- def unpack(format)
485
- raise NotImplementedError
486
- end
487
-
488
494
  def upcase
489
495
  `this.toUpperCase()`
490
496
  end
491
-
492
- def upto(other, exclusive = false)
493
- return enum_for :upto, other, exclusive unless block_given?
494
-
495
- current = self
496
-
497
- until current == other
498
- yield current
499
-
500
- current = current.next
501
- end
502
-
503
- yield current unless exclusive
504
-
505
- self
506
- end
507
497
  end
508
498
 
509
499
  Symbol = String
@@ -0,0 +1 @@
1
+ opalrb.org
@@ -0,0 +1,55 @@
1
+ desc "Clone repo"
2
+ task :clone do
3
+ if File.exists? 'gh-pages'
4
+ Dir.chdir('gh-pages') { sh 'git pull origin gh-pages' }
5
+ else
6
+ FileUtils.mkdir_p 'gh-pages'
7
+ Dir.chdir('gh-pages') do
8
+ sh 'git clone git@github.com:/adambeynon/opal.git .'
9
+ sh 'git checkout gh-pages'
10
+ end
11
+ end
12
+ end
13
+
14
+ desc "Copy files"
15
+ task :build do
16
+ files = {
17
+ '../build/opal.js' => 'gh-pages/opal.js',
18
+ 'CNAME' => 'gh-pages/CNAME',
19
+ 'css/styles.css' => 'gh-pages/css/styles.css',
20
+ 'css/syntax.css' => 'gh-pages/css/syntax.css'
21
+ }
22
+
23
+ files.each do |src, dest|
24
+ puts dest
25
+ FileUtils.mkdir_p File.dirname(dest)
26
+ FileUtils.cp src, dest
27
+ end
28
+
29
+ require 'redcarpet'
30
+ require 'albino'
31
+
32
+ klass = Class.new(Redcarpet::Render::HTML) do
33
+ def block_code(code, language)
34
+ Albino.new(code, language || :text).colorize
35
+ end
36
+ end
37
+
38
+ puts 'gh-pages/index.html'
39
+ markdown = Redcarpet::Markdown.new(klass, :fenced_code_blocks => true)
40
+
41
+ File.open('gh-pages/index.html', 'w+') do |o|
42
+ o.write File.read('layout/pre.html')
43
+ o.write markdown.render(File.read "../README.md")
44
+ o.write File.read('layout/post.html')
45
+ end
46
+ end
47
+
48
+ desc "commit and push"
49
+ task :push do
50
+ Dir.chdir('gh-pages') do
51
+ sh "git add ."
52
+ sh "git commit -a -m \"Documentation update #{Time.new}\""
53
+ sh "git push origin gh-pages"
54
+ end
55
+ end
@@ -0,0 +1,50 @@
1
+ body {
2
+ line-height: 22px;
3
+ font-size: 14px;
4
+ font-family: 'Palatino Linotype', 'Book Antiqua', Palatino, FreeSerif, serif;
5
+ }
6
+
7
+ .content {
8
+ padding: 50px 50px 50px 50px;
9
+ width: 650px;
10
+ }
11
+
12
+ p {
13
+ width: 650px;
14
+ }
15
+
16
+ pre, code {
17
+ font-family: Menlo, Monaco, Consolas, "Lucida Console", monospace;
18
+ font-size: 12px;
19
+ }
20
+
21
+ pre {
22
+ line-height: 17px;
23
+ color: #444444;
24
+ white-space: pre;
25
+ padding: 3px 0px 3px 12px;
26
+ margin: 0px 0px 8px;
27
+
28
+ background: #FAFAFA;
29
+ -webkit-box-shadow: rgba(0,0,0,0.07) 0 1px 2px inset;
30
+ -webkit-border-radius: 3px;
31
+ -moz-border-radius: 3px;
32
+ border-radius: 3px;
33
+ border: 1px solid #DDDDDD;
34
+ }
35
+
36
+ code {
37
+ background-color: #FAFAFA;
38
+ padding: 1px;
39
+ color: #444444;
40
+ border: 1px solid #DEDEDE;
41
+ }
42
+
43
+ pre code {
44
+ border: none;
45
+ padding: 0px;
46
+ }
47
+
48
+ a {
49
+ color: #212121;
50
+ }