facets 3.0.0 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (353) hide show
  1. checksums.yaml +4 -4
  2. data/.index +11 -7
  3. data/CONTRIBUTING.md +112 -0
  4. data/HISTORY.md +38 -0
  5. data/{README.rdoc → README.md} +59 -59
  6. data/demo/core/array/after.md +13 -0
  7. data/demo/core/array/arrange.md +17 -0
  8. data/demo/core/array/before.md +14 -0
  9. data/demo/core/array/collapse.md +15 -0
  10. data/demo/core/array/collisions.md +30 -0
  11. data/demo/core/array/commonality.md +31 -0
  12. data/demo/core/array/conjoin.md +47 -0
  13. data/demo/core/array/contains.md +9 -0
  14. data/demo/core/array/delete_unless.md +13 -0
  15. data/demo/core/array/delete_values.md +10 -0
  16. data/demo/core/array/delete_values_at.md +15 -0
  17. data/demo/core/array/divide.md +14 -0
  18. data/demo/core/array/duplicates.md +14 -0
  19. data/demo/core/array/each_overlap.md +12 -0
  20. data/demo/core/array/each_pair.md +13 -0
  21. data/demo/core/array/each_value.md +12 -0
  22. data/demo/core/array/entropy.md +13 -0
  23. data/demo/core/array/extract_options.md +13 -0
  24. data/demo/core/array/first.md +11 -0
  25. data/demo/core/array/from.md +11 -0
  26. data/demo/core/array/ideal_entropy.md +11 -0
  27. data/demo/core/array/intersection.md +11 -0
  28. data/demo/core/array/last.md +11 -0
  29. data/demo/core/array/median.md +23 -0
  30. data/demo/core/array/merge.md +15 -0
  31. data/demo/core/array/missing.md +10 -0
  32. data/demo/core/array/mode.md +9 -0
  33. data/demo/core/array/nonuniq.md +16 -0
  34. data/demo/core/array/not_empty.md +8 -0
  35. data/demo/core/array/occur.md +18 -0
  36. data/demo/core/array/occurrence.md +13 -0
  37. data/demo/core/array/only.md +9 -0
  38. data/demo/core/array/op_pow.md +10 -0
  39. data/demo/core/array/pad.md +28 -0
  40. data/demo/core/array/peek.md +10 -0
  41. data/demo/core/array/poke.md +15 -0
  42. data/demo/core/array/probability +8 -0
  43. data/demo/core/array/pull.md +10 -0
  44. data/demo/core/array/recurse.md +10 -0
  45. data/demo/core/array/recursively.md +20 -0
  46. data/demo/core/array/reject_values.md +10 -0
  47. data/demo/core/array/splice.md +14 -0
  48. data/demo/core/array/split.md +13 -0
  49. data/demo/core/array/squeeze.md +14 -0
  50. data/demo/core/array/step.md +16 -0
  51. data/demo/core/array/store.md +9 -0
  52. data/demo/core/array/thru.md +12 -0
  53. data/demo/core/array/to_h.md +11 -0
  54. data/demo/core/array/traverse.md +20 -0
  55. data/demo/core/array/uniq_by.md +16 -0
  56. data/demo/core/array/unique_permutation.md +14 -0
  57. data/demo/core/array/zip.md +8 -0
  58. data/demo/core/binding/caller.md +57 -0
  59. data/demo/core/binding/defined.md +10 -0
  60. data/demo/core/binding/opvars.md +16 -0
  61. data/demo/core/binding/self.md +6 -0
  62. data/demo/core/class/descendants.md +13 -0
  63. data/demo/core/class/to_proc.md +11 -0
  64. data/demo/core/comparable/at_least.md +11 -0
  65. data/demo/core/comparable/at_most.md +11 -0
  66. data/demo/core/comparable/bound.md +13 -0
  67. data/demo/core/comparable/cap.md +11 -0
  68. data/demo/core/comparable/clip.md +29 -0
  69. data/demo/core/comparable/cmp.md +18 -0
  70. data/demo/core/comparable/op_get.md +32 -0
  71. data/demo/core/dir/ascend.md +24 -0
  72. data/demo/core/dir/descend.md +16 -0
  73. data/demo/core/dir/multiglob.md +41 -0
  74. data/demo/core/dir/parent.md +6 -0
  75. data/demo/core/dir/recurse.md +41 -0
  76. data/demo/core/exception/detail.md +19 -0
  77. data/demo/core/{facets.rdoc → facets.md} +1 -1
  78. data/demo/core/filetest/root.md +13 -0
  79. data/demo/core/hash/recursively.md +21 -0
  80. data/demo/core/indexable.md +95 -0
  81. data/demo/core/integer/bitmask.md +46 -0
  82. data/demo/core/integer/{factorial.rdoc → factorial.md} +1 -1
  83. data/demo/core/integer/{length.rdoc → length.md} +1 -1
  84. data/demo/core/integer/{multiple.rdoc → multiple.md} +3 -1
  85. data/demo/core/integer/of.md +11 -0
  86. data/demo/core/integer/{ordinal.rdoc → ordinal.md} +3 -1
  87. data/demo/core/integer/{of.rdoc → times_collect.md} +2 -7
  88. data/demo/core/matchdata/match.md +7 -0
  89. data/demo/core/matchdata/matchset.md +7 -0
  90. data/demo/core/matchdata/matchtree.md +10 -0
  91. data/demo/core/module/cattr.md +43 -0
  92. data/demo/core/module/class_extend.md +64 -0
  93. data/demo/core/module/{class_inheritor.rdoc → class_inheritor.md} +2 -2
  94. data/demo/core/module/{copy_inheritor.rdoc → copy_inheritor.md} +4 -4
  95. data/demo/core/module/include_as.md +31 -0
  96. data/demo/core/module/method_space.md +24 -0
  97. data/demo/core/string/acronym.md +9 -0
  98. data/demo/core/string/{align.rdoc → align.md} +10 -10
  99. data/demo/core/string/ascii_only.md +37 -0
  100. data/demo/core/string/{bracket.rdoc → bracket.md} +10 -10
  101. data/demo/core/string/camelcase.md +47 -0
  102. data/demo/core/string/{capitalized.rdoc → capitalized.md} +9 -9
  103. data/demo/core/string/characters.md +11 -0
  104. data/demo/core/string/{cleanlines.rdoc → cleanlines.md} +3 -3
  105. data/demo/core/string/cleave.md +49 -0
  106. data/demo/core/string/compress_lines.md +22 -0
  107. data/demo/core/string/crypt.md +15 -0
  108. data/demo/core/string/divide.md +9 -0
  109. data/demo/core/string/each_word.md +11 -0
  110. data/demo/core/string/edit_distance.md +15 -0
  111. data/demo/core/string/exclude.md +15 -0
  112. data/demo/core/string/expand_tabs.md +69 -0
  113. data/demo/core/string/fold.md +14 -0
  114. data/demo/core/string/indent.md +65 -0
  115. data/demo/core/string/index_all.md +14 -0
  116. data/demo/core/string/interpolate.md +8 -0
  117. data/demo/core/string/lchomp.md +14 -0
  118. data/demo/core/string/line_wrap.md +7 -0
  119. data/demo/core/string/lowercase.md +12 -0
  120. data/demo/core/string/methodize.md +22 -0
  121. data/demo/core/string/modulize.md +22 -0
  122. data/demo/core/string/mscan.md +10 -0
  123. data/demo/core/string/natcmp.md +10 -0
  124. data/demo/core/string/nchar.md +9 -0
  125. data/demo/core/string/{newlines.rdoc → newlines.md} +3 -3
  126. data/demo/core/string/number.md +27 -0
  127. data/demo/core/string/op_div.md +10 -0
  128. data/demo/core/string/op_sub.md +7 -0
  129. data/demo/core/string/pathize.md +22 -0
  130. data/demo/core/string/quote.md +20 -0
  131. data/demo/core/string/random.md +25 -0
  132. data/demo/core/string/range.md +19 -0
  133. data/demo/core/string/remove.md +33 -0
  134. data/demo/core/string/rewrite.md +9 -0
  135. data/demo/core/string/shatter.md +9 -0
  136. data/demo/core/string/similarity.md +12 -0
  137. data/demo/core/string/snakecase.md +14 -0
  138. data/demo/core/string/splice.md +28 -0
  139. data/demo/core/string/squish.md +27 -0
  140. data/demo/core/string/store.md +10 -0
  141. data/demo/core/string/subtract.md +8 -0
  142. data/demo/core/string/titlecase.md +8 -0
  143. data/demo/core/string/to_re.md +22 -0
  144. data/demo/core/string/{margin.rdoc → trim.md} +14 -14
  145. data/demo/core/string/unbracket.md +24 -0
  146. data/demo/core/string/underscore.md +17 -0
  147. data/demo/core/string/unfold.md +9 -0
  148. data/demo/core/string/unindent.md +65 -0
  149. data/demo/core/string/unquote.md +6 -0
  150. data/demo/core/string/uppercase.md +15 -0
  151. data/demo/core/string/variablize.md +7 -0
  152. data/demo/core/string/word_wrap.md +54 -0
  153. data/demo/core/string/words.md +13 -0
  154. data/demo/core/string/xor.md +8 -0
  155. data/lib/core/facets.yml +11 -7
  156. data/lib/core/facets/array.rb +1 -0
  157. data/lib/core/facets/array/average.rb +16 -0
  158. data/lib/core/facets/array/collapse.rb +4 -0
  159. data/lib/core/facets/array/delete_values_at.rb +1 -0
  160. data/lib/core/facets/array/divide.rb +7 -0
  161. data/lib/core/facets/array/duplicates.rb +2 -0
  162. data/lib/core/facets/array/each_pair.rb +1 -4
  163. data/lib/core/facets/array/first.rb +1 -0
  164. data/lib/core/facets/array/from.rb +11 -16
  165. data/lib/core/facets/array/intersection.rb +1 -1
  166. data/lib/core/facets/array/last.rb +1 -0
  167. data/lib/core/facets/array/mode.rb +2 -2
  168. data/lib/core/facets/array/occurrence.rb +4 -3
  169. data/lib/core/facets/array/op_pow.rb +0 -2
  170. data/lib/core/facets/array/probability.rb +1 -1
  171. data/lib/core/facets/array/recursively.rb +2 -2
  172. data/lib/core/facets/array/splice.rb +1 -1
  173. data/lib/core/facets/array/standard_deviation.rb +23 -0
  174. data/lib/core/facets/array/to_h.rb +0 -3
  175. data/lib/core/facets/array/uniq_by.rb +4 -0
  176. data/lib/core/facets/array/variance.rb +20 -0
  177. data/lib/core/facets/binding.rb +0 -1
  178. data/lib/core/facets/enumerable/cluster.rb +6 -4
  179. data/lib/core/facets/enumerable/hashify.rb +27 -31
  180. data/lib/core/facets/enumerable/sum.rb +12 -2
  181. data/lib/core/facets/file/atomic_open.rb +1 -1
  182. data/lib/core/facets/hash/except.rb +13 -1
  183. data/lib/core/facets/indexable.rb +1 -1
  184. data/lib/core/facets/kernel/ergo.rb +3 -3
  185. data/lib/core/facets/kernel/like.rb +1 -1
  186. data/lib/core/facets/matchdata/matchtree.rb +2 -0
  187. data/lib/core/facets/numeric/delimit.rb +20 -0
  188. data/lib/core/facets/range/nudge.rb +29 -0
  189. data/lib/core/facets/string.rb +0 -2
  190. data/lib/core/facets/string/indent.rb +0 -8
  191. data/lib/core/facets/string/margin.rb +0 -4
  192. data/lib/core/facets/string/random.rb +13 -3
  193. data/lib/core/facets/string/similarity.rb +7 -6
  194. data/lib/standard/facets/enumargs.rb +5 -0
  195. data/lib/standard/facets/math/percentile.rb +1 -1
  196. data/lib/standard/facets/net/http.rb +33 -14
  197. data/lib/standard/facets/timer.rb +3 -3
  198. data/test/core/array/test_average.rb +16 -0
  199. data/test/core/array/test_before.rb +21 -0
  200. data/test/core/array/test_collapse.rb +10 -1
  201. data/test/core/array/test_delete_unless.rb +17 -0
  202. data/test/core/array/test_delete_values.rb +31 -0
  203. data/test/core/array/test_divide.rb +10 -0
  204. data/test/core/array/test_each_pair.rb +16 -0
  205. data/test/core/array/test_each_value.rb +16 -0
  206. data/test/core/array/test_from.rb +42 -0
  207. data/test/core/array/test_op_pow.rb +13 -0
  208. data/test/core/array/test_standard_deviation.rb +18 -0
  209. data/test/core/array/test_to_h.rb +14 -0
  210. data/test/core/array/test_uniq_by.rb +7 -0
  211. data/test/core/array/test_variance.rb +17 -0
  212. data/test/core/array/test_zip.rb +13 -0
  213. data/test/core/dir/test_each_child.rb +1 -1
  214. data/test/core/dir/test_multiglob.rb +1 -1
  215. data/test/core/enumerable/test_cluster.rb +36 -0
  216. data/test/core/enumerable/test_hashify.rb +113 -0
  217. data/test/core/hash/test_expect.rb +11 -1
  218. data/test/core/kernel/test_ergo.rb +7 -5
  219. data/test/core/{object/test_replace.rb → kernel/test_instance_replace.rb} +6 -28
  220. data/test/core/numeric/test_delimit.rb +68 -0
  221. data/test/core/numeric/{test_round.rb → test_round_to.rb} +27 -27
  222. data/test/core/range/test_nudge.rb +32 -0
  223. data/test/core/range/{test_to_r.rb → test_to_rng.rb} +3 -3
  224. data/test/core/string/test_acronym.rb +14 -0
  225. data/test/core/string/test_ascii_only.rb +21 -0
  226. data/test/core/string/test_compress_lines.rb +9 -0
  227. data/test/core/string/test_crypt.rb +17 -0
  228. data/test/core/string/test_index_all.rb +4 -0
  229. data/test/core/string/test_random.rb +25 -0
  230. data/test/core/string/test_remove.rb +14 -0
  231. data/test/core/string/test_similarity.rb +9 -1
  232. data/test/core/struct/test_replace.rb +18 -0
  233. data/test/standard/test_enumargs.rb +26 -10
  234. data/test/standard/test_timer.rb +1 -1
  235. metadata +634 -588
  236. data/CONTRIBUTE.md +0 -78
  237. data/demo/core/array/combination.rdoc +0 -19
  238. data/demo/core/array/delete.rdoc +0 -23
  239. data/demo/core/array/index.rdoc +0 -14
  240. data/demo/core/array/merge.rdoc +0 -15
  241. data/demo/core/array/only.rdoc +0 -9
  242. data/demo/core/array/pad.rdoc +0 -28
  243. data/demo/core/array/permutation.rdoc +0 -10
  244. data/demo/core/array/product.rdoc +0 -26
  245. data/demo/core/array/recursively.rdoc +0 -16
  246. data/demo/core/array/rotate.rdoc +0 -30
  247. data/demo/core/array/select.rdoc +0 -8
  248. data/demo/core/array/uniq_by.rdoc +0 -9
  249. data/demo/core/binding/caller.rdoc +0 -57
  250. data/demo/core/binding/defined.rdoc +0 -10
  251. data/demo/core/binding/eval.rdoc +0 -8
  252. data/demo/core/binding/local_variables.rdoc +0 -26
  253. data/demo/core/binding/opvars.rdoc +0 -16
  254. data/demo/core/binding/self.rdoc +0 -6
  255. data/demo/core/class/descendants.rdoc +0 -10
  256. data/demo/core/class/to_proc.rdoc +0 -8
  257. data/demo/core/comparable/cap.rdoc +0 -20
  258. data/demo/core/comparable/clip.rdoc +0 -37
  259. data/demo/core/comparable/cmp.rdoc +0 -18
  260. data/demo/core/comparable/op_get.rdoc +0 -29
  261. data/demo/core/dir/ascend.rdoc +0 -24
  262. data/demo/core/dir/descend.rdoc +0 -14
  263. data/demo/core/dir/multiglob.rdoc +0 -41
  264. data/demo/core/dir/parent.rdoc +0 -6
  265. data/demo/core/dir/recurse.rdoc +0 -41
  266. data/demo/core/exception/detail.rdoc +0 -12
  267. data/demo/core/filetest/root.rdoc +0 -6
  268. data/demo/core/hash/recursively.rdoc +0 -21
  269. data/demo/core/indexable.rdoc +0 -95
  270. data/demo/core/integer/bitmask.rdoc +0 -46
  271. data/demo/core/integer/even.rdoc +0 -12
  272. data/demo/core/integer/odd.rdoc +0 -12
  273. data/demo/core/matchdata/match.rdoc +0 -7
  274. data/demo/core/matchdata/matchset.rdoc +0 -15
  275. data/demo/core/module/cattr.rdoc +0 -43
  276. data/demo/core/module/class_extend.rdoc +0 -64
  277. data/demo/core/module/method_space.rdoc +0 -55
  278. data/demo/core/string/bytes.rdoc +0 -8
  279. data/demo/core/string/camelcase.rdoc +0 -47
  280. data/demo/core/string/characters.rdoc +0 -11
  281. data/demo/core/string/cleave.rdoc +0 -49
  282. data/demo/core/string/divide.rdoc +0 -9
  283. data/demo/core/string/each_char.rdoc +0 -9
  284. data/demo/core/string/each_word.rdoc +0 -9
  285. data/demo/core/string/end_with.rdoc +0 -6
  286. data/demo/core/string/expand_tabs.rdoc +0 -69
  287. data/demo/core/string/fold.rdoc +0 -14
  288. data/demo/core/string/indent.rdoc +0 -65
  289. data/demo/core/string/interpolate.rdoc +0 -7
  290. data/demo/core/string/lchomp.rdoc +0 -14
  291. data/demo/core/string/line_wrap.rdoc +0 -7
  292. data/demo/core/string/lines.rdoc +0 -8
  293. data/demo/core/string/methodize.rdoc +0 -22
  294. data/demo/core/string/modulize.rdoc +0 -22
  295. data/demo/core/string/mscan.rdoc +0 -10
  296. data/demo/core/string/natcmp.rdoc +0 -10
  297. data/demo/core/string/nchar.rdoc +0 -9
  298. data/demo/core/string/op_sub.rdoc +0 -7
  299. data/demo/core/string/pathize.rdoc +0 -22
  300. data/demo/core/string/quote.rdoc +0 -20
  301. data/demo/core/string/range.rdoc +0 -19
  302. data/demo/core/string/rewrite.rdoc +0 -9
  303. data/demo/core/string/shatter.rdoc +0 -9
  304. data/demo/core/string/snakecase.rdoc +0 -10
  305. data/demo/core/string/splice.rdoc +0 -28
  306. data/demo/core/string/start_with.rdoc +0 -7
  307. data/demo/core/string/subtract.rdoc +0 -8
  308. data/demo/core/string/tab.rdoc +0 -16
  309. data/demo/core/string/tabto.rdoc +0 -29
  310. data/demo/core/string/titlecase.rdoc +0 -8
  311. data/demo/core/string/to_re.rdoc +0 -22
  312. data/demo/core/string/unfold.rdoc +0 -9
  313. data/demo/core/string/unindent.rdoc +0 -65
  314. data/demo/core/string/unquote.rdoc +0 -6
  315. data/demo/core/string/variablize.rdoc +0 -7
  316. data/demo/core/string/word_wrap.rdoc +0 -54
  317. data/demo/core/string/words.rdoc +0 -13
  318. data/demo/core/string/xor.rdoc +0 -8
  319. data/lib/core/facets/binding/local_variables.rb +0 -16
  320. data/lib/core/facets/string/tab.rb +0 -2
  321. data/lib/core/facets/string/tabto.rb +0 -2
  322. data/test/core/array/test_after.rb +0 -27
  323. data/test/core/array/test_combination.rb +0 -25
  324. data/test/core/array/test_index.rb +0 -20
  325. data/test/core/array/test_permutation.rb +0 -20
  326. data/test/core/array/test_product.rb +0 -64
  327. data/test/core/array/test_rotate.rb +0 -38
  328. data/test/core/array/test_select.rb +0 -16
  329. data/test/core/binding/test_eval.rb +0 -15
  330. data/test/core/binding/test_local_variables.rb +0 -36
  331. data/test/core/enumerable/test_cluster_by.rb +0 -30
  332. data/test/core/enumerable/test_count.rb +0 -32
  333. data/test/core/enumerable/test_each_with_object.rb +0 -18
  334. data/test/core/enumerable/test_group_by.rb +0 -36
  335. data/test/core/enumerable/test_none.rb +0 -29
  336. data/test/core/enumerable/test_one.rb +0 -34
  337. data/test/core/enumerable/test_take.rb +0 -15
  338. data/test/core/hash/test_select.rb +0 -44
  339. data/test/core/hash/test_url_params.rb +0 -26
  340. data/test/core/integer/test_even.rb +0 -24
  341. data/test/core/integer/test_odd.rb +0 -24
  342. data/test/core/kernel/test_require_relative.rb +0 -12
  343. data/test/core/nilclass/test_to_f.rb +0 -13
  344. data/test/core/proc/test_curry.rb +0 -38
  345. data/test/core/string/test_bytes.rb +0 -13
  346. data/test/core/string/test_each_char.rb +0 -17
  347. data/test/core/string/test_end_with.rb +0 -24
  348. data/test/core/string/test_lines.rb +0 -15
  349. data/test/core/string/test_start_with.rb +0 -24
  350. data/test/core/string/test_tab.rb +0 -16
  351. data/test/core/symbol/test_to_proc.rb +0 -35
  352. data/test/core/to_hash_tc.rb +0 -210
  353. data/test/core/unboundmethod/test_name.rb +0 -19
@@ -7,6 +7,18 @@ class Hash
7
7
  hash
8
8
  end
9
9
 
10
+ # Replaces hash with new hash less the given keys.
11
+ #
12
+ # h = {:a=>1, :b=>2, :c=>3}
13
+ # h.except!(:a) #=> {:b=>2,:c=>3}
14
+ # h #=> {:b=>2,:c=>3}
15
+ #
16
+ # Returns the hash.
17
+ def except!(*rejected)
18
+ rejected.each{ |k| delete(k) }
19
+ self
20
+ end
21
+
10
22
  # Replaces hash with new hash less the given keys.
11
23
  # This returns the hash of keys removed.
12
24
  #
@@ -15,7 +27,7 @@ class Hash
15
27
  # h #=> {:b=>2,:c=>3}
16
28
  #
17
29
  # Returns a Hash of the removed pairs.
18
- def except!(*rejected)
30
+ def remove!(*rejected)
19
31
  removed = {}
20
32
  rejected.each{ |k| removed[k] = delete(k) }
21
33
  removed
@@ -128,7 +128,7 @@ module Indexable
128
128
  # %w{W o r l d}.from(3) #=> %w{l d}
129
129
  #
130
130
  def from(i)
131
- return self if i >= size
131
+ return [] if i >= size
132
132
  slice(i, size - i) #slice(-n..-1)
133
133
  end
134
134
 
@@ -26,7 +26,8 @@ module Kernel
26
26
  end
27
27
 
28
28
  class NilClass
29
-
29
+ FUNCTOR = Functor.new{ nil }
30
+
30
31
  # Compliments Kernel#ergo.
31
32
  #
32
33
  # "a".ergo{ |o| o.upcase } #=> "A"
@@ -35,8 +36,7 @@ class NilClass
35
36
  # CREDIT: Daniel DeLorme
36
37
 
37
38
  def ergo
38
- @_ergo ||= Functor.new{ nil }
39
- @_ergo unless block_given?
39
+ FUNCTOR unless block_given?
40
40
  end
41
41
 
42
42
  end
@@ -5,7 +5,7 @@ module Kernel
5
5
  # #equal?, then #eql?, then #==, and ending with #===.
6
6
  #
7
7
  # 1.like?(1.0) #=> true
8
- # "string".likee?("string") #=> true
8
+ # "string".like?("string") #=> true
9
9
  # String.like?("string") #=> true
10
10
  #
11
11
  def like?(x)
@@ -0,0 +1,2 @@
1
+ # Matchdata#matchtree is defined in
2
+ require 'facets/matchdata/matchset'
@@ -0,0 +1,20 @@
1
+ class Numeric
2
+ # Returns a string representation of the number e.g.
3
+ # 1000.delimit # => "1,000"
4
+ # 1000000.delimit # => "1,000,000"
5
+ # (1000000.1234).delimit # => "1,000,000.1234"
6
+ #
7
+ # Can take a hash of options:
8
+ # * <tt>delimiter</tt> - defaults to "," but can be any string
9
+ # * <tt>separator</tt> - defaults to "." but can be any string
10
+ #
11
+ # 1000.delimit(:delimiter => "_") # => "1_000"
12
+ # (1000.00).delimit(:delimiter => ".", :separator => ",") # => "1.000,00"
13
+ def delimit(options = {})
14
+ opts = { :delimiter => ',', :separator => '.' }.merge(options)
15
+ digits, decimals = self.to_s.split('.')
16
+ digits = digits.reverse.chars.each_slice(3).map(&:join).join(opts[:delimiter]).reverse
17
+ return digits unless decimals
18
+ [digits, decimals].join(opts[:separator])
19
+ end
20
+ end
@@ -0,0 +1,29 @@
1
+ class Range
2
+
3
+ # Nudge range values
4
+ #
5
+ # (1..5).nudge #=> 2..6
6
+ # (1..5).nudge(2) #=> 3..7
7
+ # (1..5).nudge(-2) #=> -1..3
8
+ # (1..5).nudge(min: 1) #=> 2..5
9
+ # (1..5).nudge(max: 1) #=> 1..6
10
+ #
11
+ # CREDIT: Codeindulgence
12
+
13
+ def nudge(options_or_number = 1)
14
+ if options_or_number.instance_of? Fixnum
15
+ {:min => options_or_number, :max => options_or_number}
16
+ min = options_or_number
17
+ max = options_or_number
18
+ else
19
+ min = options_or_number[:min].to_i
20
+ max = options_or_number[:max].to_i
21
+ end
22
+
23
+ if exclude_end?
24
+ (self.min + min)...((self.max + 1) + max)
25
+ else
26
+ (self.min + min)..(self.max + max)
27
+ end
28
+ end
29
+ end
@@ -53,8 +53,6 @@ require_relative 'string/squish.rb'
53
53
  require_relative 'string/starts_with.rb'
54
54
  require_relative 'string/store.rb'
55
55
  require_relative 'string/subtract.rb'
56
- require_relative 'string/tab.rb'
57
- require_relative 'string/tabto.rb'
58
56
  require_relative 'string/titlecase.rb'
59
57
  require_relative 'string/to_re.rb'
60
58
  require_relative 'string/unbracket.rb'
@@ -73,12 +73,4 @@ class String
73
73
  def unindent!
74
74
  replace(unindent)
75
75
  end
76
-
77
- # DEPRECATED: Use String#indent instead.
78
- alias :tab :indent
79
- alias :tab! :indent!
80
-
81
- #alias :backtab :unindent
82
- #alias :backtab! :unindent!
83
-
84
76
  end
@@ -52,8 +52,4 @@ class String
52
52
  str.indent(num - min)
53
53
  end
54
54
  end
55
-
56
- # Original name of #margin.
57
- alias :tabto :margin
58
-
59
55
  end
@@ -2,6 +2,8 @@ class String
2
2
 
3
3
  # Create a random String of given length, using given character set
4
4
  #
5
+ # Character set is an Array which can contain Ranges, Arrays, Characters
6
+ #
5
7
  # Examples
6
8
  #
7
9
  # String.random
@@ -19,6 +21,15 @@ class String
19
21
  # String.random(10, ['0'..'9','A'..'F'] )
20
22
  # => "3EBF48AD3D"
21
23
  #
24
+ # BASE64_CHAR_SET = ["A".."Z", "a".."z", "0".."9", '_', '-']
25
+ # String.random(10, BASE64_CHAR_SET)
26
+ # => "xM_1t3qcNn"
27
+ #
28
+ # SPECIAL_CHARS = ["!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "=", "+", "|", "/", "?", ".", ",", ";", ":", "~", "`", "[", "]", "{", "}", "<", ">"]
29
+ # BASE91_CHAR_SET = ["A".."Z", "a".."z", "0".."9", SPECIAL_CHARS]
30
+ # String.random(10, BASE91_CHAR_SET)
31
+ # => "S(Z]z,J{v;"
32
+ #
22
33
  # CREDIT: Tilo Sloboda
23
34
  #
24
35
  # SEE: https://gist.github.com/tilo/3ee8d94871d30416feba
@@ -26,9 +37,8 @@ class String
26
37
  # TODO: Move to random.rb in standard library?
27
38
 
28
39
  def self.random(len=32, character_set = ["A".."Z", "a".."z", "0".."9"])
29
- characters = character_set.map { |i| i.to_a }.flatten
30
- characters_len = characters.length
31
- (0...len).map{ characters[rand(characters_len)] }.join
40
+ chars = character_set.map{|x| x.is_a?(Range) ? x.to_a : x }.flatten
41
+ Array.new(len){ chars.sample }.join
32
42
  end
33
43
 
34
44
  end
@@ -4,19 +4,19 @@ class String
4
4
  # on the number of shared edges. To be effective, the strings
5
5
  # must be of length 2 or greater.
6
6
  #
7
- # "Alexsander".similarity("Aleksander") #=> 0.9
7
+ # "Alexsander".similarity("Aleksander") #=> 0.9
8
8
  #
9
9
  # The way it works:
10
10
  #
11
11
  # 1. Converts each string into a "graph like" object, with edges ...
12
12
  #
13
- # "alexsander" -> [ alexsander, alexsand, alexsan ... lexsand ... san ... an, etc ]
14
- # "aleksander" -> [ aleksander, aleksand ... etc. ]
13
+ # "alexsander" -> [ alexsander, alexsand, alexsan ... lexsand ... san ... an, etc ]
14
+ # "aleksander" -> [ aleksander, aleksand ... etc. ]
15
15
  #
16
16
  # 2. Perform match, then remove any subsets from this matched set (i.e. a hit
17
17
  # on "san" is a subset of a hit on "sander") ...
18
18
  #
19
- # Above example, once reduced -> [ ale, sander ]
19
+ # Above example, once reduced -> [ ale, sander ]
20
20
  #
21
21
  # 3. See's how many of the matches remain, and calculates a score based
22
22
  # on how many matches, their length, and compare to the length of the
@@ -24,7 +24,8 @@ class String
24
24
  #
25
25
  # Still a bit rough. Any suggestions for improvement are welcome.
26
26
  #
27
- # CREDIT: Derek Lewis.
27
+ # CREDIT: Derek Lewis
28
+
28
29
  def similarity(str_in)
29
30
  return 0 if str_in == nil
30
31
  return 1 if self == str_in
@@ -82,7 +83,7 @@ class String
82
83
  score = 0.0
83
84
  matches.each{ |mm| score += mm.length }
84
85
  self.length > str_in.length ? largest = self.length : largest = str_in.length
85
- return score/largest
86
+ return score/(largest+1)
86
87
  end
87
88
 
88
89
  end
@@ -51,6 +51,11 @@ module Enumerable
51
51
  end
52
52
  }
53
53
  else
54
+ # this branch is used when the method has a variable number of arguments
55
+ # resulting in an arity of -1. Right now this is bugged as it does
56
+ # not pass the argument to the each, and always passes the argument
57
+ # to the method. This makes methods like .min amdn .max to act
58
+ # in an unexpected manner.
54
59
  class_eval %{
55
60
  def #{m}( *args, &yld )
56
61
  enum_for(:each).#{m}( *args, &yld )
@@ -5,7 +5,7 @@ module Math
5
5
  # +pcnt+ should be expressed as an integer, e.g. `percentile(90)` returns
6
6
  # the 90th percentile of the array.
7
7
  #
8
- # Algorithm from NIST[http://www.itl.nist.gov/div898/handbook/prc/section2/prc252.htm]
8
+ # Algorithm from NIST[http://www.itl.nist.gov/div898/handbook/prc/section2/prc262.htm]
9
9
  #
10
10
  # NOTE: This is not a common core extension and is not
11
11
  # loaded automatically when using <code>require 'facets'</code>.
@@ -2,13 +2,17 @@ require 'net/http'
2
2
 
3
3
  module Net
4
4
 
5
+ # Returns a response object for the given URL or raises
6
+ # an exception with the appropriate error status code if unable to
7
+ # complete the request
8
+ #
9
+ # Net.download('http://example.com/')
10
+ # Net.download('http://example.com/', 2) # fail after 2 redirects
11
+ #
5
12
  # CREDIT: Daniel Huckstep
6
- def self.download(url, limit=10)
7
- raise ArgumentError, 'HTTP redirect too deep' if limit == 0
8
- #url = URI.parse(url)
9
- #req = Net::HTTP::Get.new(url.path)
10
- #req['User-Agent'] = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.7) Gecko/2009030422 Ubuntu/8.10 (intrepid) Firefox/3.0.7"
11
- #resp = Net::HTTP.new(url.host, url.port).start { |http| http.request(req) }
13
+ def self.download(url, limit = nil)
14
+ limit ||= 10
15
+ raise ArgumentError, 'HTTP redirect too deep' if limit.zero?
12
16
  resp = Net::HTTP.get_response(URI.parse(url))
13
17
  case resp
14
18
  when Net::HTTPSuccess then resp
@@ -17,16 +21,31 @@ module Net
17
21
  end
18
22
  end
19
23
 
24
+ # Downloads a given URL and saves it to disk at the specified path.
25
+ # If path is not provided or nil, then the last segment in the path of
26
+ # the URL is used as the file name.
27
+ #
28
+ # Takes a third parameters as a hash of options:
29
+ # * <tt>limit</tt> - passed to Net.download to limit number of redirects
30
+ # * <tt>mode</tt>, <tt>:open_args<tt>, etc - passed to File.write to save the file.
31
+ # For details on those options, see File.write
32
+ #
33
+ # # Saves robots.txt to PWD as robots.txt
34
+ # Net.download_and_save('http://example.com/robots.txt')
35
+ #
36
+ # # Saves robots.txt to PWD as example.txt
37
+ # Net.download_and_save('http://example.com/robots.txt', 'example.txt')
38
+ #
39
+ # # Saves robots.txt to PWD as robots.txt, and writes the result as binary
40
+ # Net.download_and_save('http://example.com/robots.txt', nil, mode: 'wb')
41
+ #
20
42
  # CREDIT: Daniel Huckstep
21
- def self.download_and_save(url, path=nil)
22
- if path.nil?
23
- path = File.expand_path(url.split('/').last)
24
- else
25
- path = File.expand_path(path)
26
- end
43
+ def self.download_and_save(url, path = nil, options = {})
44
+ opts = options.dup
45
+ path = File.expand_path(path || url.split('/').last)
27
46
  raise ArgumentError.new('Save path is a directory') if File.directory?(path)
28
- resp = download(url)
29
- open(path,'w') { |file| file.write(resp.body) } if resp.is_a?(Net::HTTPSuccess)
47
+ resp = download(url, opts.delete(:limit))
48
+ File.write(path, resp.body, opts) if resp.is_a?(Net::HTTPSuccess)
30
49
  end
31
50
  end
32
51
 
@@ -1,11 +1,11 @@
1
- require 'timeout' # for TimeoutError
1
+ require 'timeout' # for Timeout::Error
2
2
 
3
3
  # = Timer
4
4
  #
5
5
  # Provides a strightforward means for controlling time critical execution.
6
6
  # Can be used as a "stop watch" timer or as a "time bomb" timer:
7
7
  #
8
- # t = Timer.new(10) { raise TimeoutError, "timeout!" }
8
+ # t = Timer.new(10) { raise Timeout::Error, "timeout!" }
9
9
  # t.start
10
10
  # : # done within 10sec timeout
11
11
  # t.stop
@@ -107,7 +107,7 @@ class Timer
107
107
  if @on_timeout then
108
108
  @on_timeout.call @time_limit
109
109
  else
110
- @current_thread.raise TimeoutError, "#{@time_limit} seconds past"
110
+ @current_thread.raise Timeout::Error, "#{@time_limit} seconds past"
111
111
  end
112
112
  }
113
113
  end
@@ -0,0 +1,16 @@
1
+ require 'facets/array/average'
2
+
3
+ test_case Array do
4
+ method :average do
5
+ test 'Empty array returns nil' do
6
+ [].average.assert == nil
7
+ end
8
+
9
+ test 'Calculate average of array of numerical values' do
10
+ [1, 2, 3].average.assert == 2
11
+ [96, 35, 72, 30, 75, 33, 68, 13, 49, 71].average.assert == 54.2
12
+ [36, -67, -17, 85, -46, -64, -23, -13, 89, -47].average.assert == -6.7
13
+ [60.7829, 31.2622, 20.626, 78.8907, 61.5328].average.assert == 50.61892
14
+ end
15
+ end
16
+ end
@@ -23,5 +23,26 @@ test_case Array do
23
23
 
24
24
  end
25
25
 
26
+ method :after do
27
+
28
+ setup do
29
+ @sequence = ['a', 'b', 'c']
30
+ end
31
+
32
+ test "returns the following element" do
33
+ @sequence.after('a').assert == 'b'
34
+ @sequence.after('b').assert == 'c'
35
+ end
36
+
37
+ test "loops around from back to front" do
38
+ @sequence.after('c').assert == 'a'
39
+ end
40
+
41
+ test "non-member elements return nil" do
42
+ @sequence.after('d').assert == nil
43
+ end
44
+
45
+ end
46
+
26
47
  end
27
48
 
@@ -5,10 +5,19 @@ test_case Array do
5
5
  method :collapse do
6
6
 
7
7
  test do
8
- [1,nil,[1,2],nil,[[3]]].collapse.assert == [1,1,2,3]
8
+ [1, nil, [1, 2], nil, [[3]]].collapse.assert == [1, 1, 2, 3]
9
9
  end
10
10
 
11
11
  end
12
12
 
13
+ method :collapse! do
14
+
15
+ test do
16
+ a = [1, nil, [1, 2], nil, [[3]]]
17
+ a.collapse!
18
+ a.assert == [1, 1, 2, 3]
19
+
20
+ end
21
+ end
13
22
  end
14
23
 
@@ -0,0 +1,17 @@
1
+ covers 'facets/array/delete_unless'
2
+
3
+ test_case Array do
4
+
5
+ method :delete_unless do
6
+
7
+ test 'with an explicit block' do
8
+ [1,2,3].delete_unless{ |x| x < 2 }.assert == [1]
9
+ end
10
+
11
+ test 'with an implicit block' do
12
+ [1,2,3].delete_unless(&:even?).assert == [2]
13
+ end
14
+
15
+ end
16
+
17
+ end
@@ -0,0 +1,31 @@
1
+ covers 'facets/array/delete_values'
2
+
3
+ test_case Array do
4
+
5
+ method :delete_values do
6
+
7
+ test do
8
+ a = [1,2,3,4]
9
+ a.delete_values(1,2).assert == [1,2]
10
+ a.assert == [3,4]
11
+ end
12
+
13
+ end
14
+
15
+ method :delete_values_at do
16
+
17
+ test do
18
+ a = [1,2,3,4]
19
+ a.delete_values_at(1,2).assert == [2,3]
20
+ a.assert == [1,4]
21
+ end
22
+
23
+ test do
24
+ a = [1,2,3,4]
25
+ a.delete_values_at(0..2).assert == [1,2,3]
26
+ a.assert == [4]
27
+ end
28
+
29
+ end
30
+
31
+ end