rmtools 1.0.0 → 1.1.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 (110) hide show
  1. data/History.txt +5 -0
  2. data/Manifest.txt +84 -26
  3. data/Rakefile +7 -4
  4. data/ext/extconf.rb +1 -1
  5. data/ext/rmtools.cpp +27 -12
  6. data/ext/rmtools.h +6 -5
  7. data/lib/rmtools/b.rb +18 -0
  8. data/lib/rmtools/console/coloring.rb +72 -0
  9. data/lib/rmtools/console/highlight.rb +13 -0
  10. data/lib/rmtools/{printing.rb → console/printing.rb} +17 -2
  11. data/lib/rmtools/console.rb +1 -0
  12. data/lib/rmtools/conversions/enum.rb +23 -0
  13. data/lib/rmtools/conversions/int.rb +47 -0
  14. data/lib/rmtools/conversions/string.rb +26 -0
  15. data/lib/rmtools/conversions.rb +1 -0
  16. data/lib/rmtools/core/arguments.rb +52 -0
  17. data/lib/rmtools/{boolean.rb → core/boolean.rb} +0 -13
  18. data/lib/rmtools/core/class.rb +41 -0
  19. data/lib/rmtools/core/js.rb +45 -0
  20. data/lib/rmtools/core/kernel.rb +28 -0
  21. data/lib/rmtools/{module.rb → core/module.rb} +0 -41
  22. data/lib/rmtools/core/numeric.rb +35 -0
  23. data/lib/rmtools/{object.rb → core/object.rb} +2 -23
  24. data/lib/rmtools/core/proc.rb +18 -0
  25. data/lib/rmtools/core/regexp.rb +11 -0
  26. data/lib/rmtools/core/string_compliance.rb +31 -0
  27. data/lib/rmtools/core.rb +1 -0
  28. data/lib/rmtools/db/active_record.rb +54 -0
  29. data/lib/rmtools/db.rb +7 -0
  30. data/lib/rmtools/debug/binding.rb +56 -0
  31. data/lib/rmtools/debug/highlight.rb +23 -0
  32. data/lib/rmtools/debug/logging.rb +176 -0
  33. data/lib/rmtools/debug/present.rb +38 -0
  34. data/lib/rmtools/debug/timer.rb +19 -0
  35. data/lib/rmtools/debug/traceback.rb +92 -0
  36. data/lib/rmtools/debug.rb +1 -0
  37. data/lib/rmtools/debug_notrace.rb +1 -0
  38. data/lib/rmtools/enumerable/array.rb +134 -0
  39. data/lib/rmtools/enumerable/array_iterators.rb +33 -0
  40. data/lib/rmtools/enumerable/common.rb +49 -0
  41. data/lib/rmtools/{hash.rb → enumerable/hash.rb} +8 -8
  42. data/lib/rmtools/enumerable/object_space.rb +19 -0
  43. data/lib/rmtools/enumerable/range.rb +201 -0
  44. data/lib/rmtools/enumerable.rb +1 -0
  45. data/lib/rmtools/experimental/blackhole.rb +12 -0
  46. data/lib/rmtools/experimental/deprecation.rb +36 -0
  47. data/lib/rmtools/experimental/dumps.rb +28 -0
  48. data/lib/rmtools/{numeric.rb → experimental/numeric.rb} +22 -51
  49. data/lib/rmtools/experimental/rails_backtrace.rb +29 -0
  50. data/lib/rmtools/experimental/string.rb +56 -0
  51. data/lib/rmtools/{tree.rb → experimental/tree.rb} +0 -0
  52. data/lib/rmtools/experimental.rb +1 -0
  53. data/lib/rmtools/fs/dir.rb +89 -0
  54. data/lib/rmtools/fs/file.rb +104 -0
  55. data/lib/rmtools/fs/io.rb +58 -0
  56. data/lib/rmtools/fs/tools.rb +49 -0
  57. data/lib/rmtools/fs.rb +1 -0
  58. data/lib/rmtools/functional/fold.rb +32 -0
  59. data/lib/rmtools/{string_to_proc.rb → functional/string_to_proc.rb} +2 -22
  60. data/lib/rmtools/functional/unfold.rb +16 -0
  61. data/lib/rmtools/functional.rb +1 -0
  62. data/lib/rmtools/ip/numeric.rb +35 -0
  63. data/lib/rmtools/ip/string.rb +45 -0
  64. data/lib/rmtools/ip.rb +1 -0
  65. data/lib/rmtools/lang/ansi.rb +17 -0
  66. data/lib/rmtools/lang/cyrillic.rb +106 -0
  67. data/lib/rmtools/lang/regexp.rb +8 -0
  68. data/lib/rmtools/lang/shortcuts.rb +20 -0
  69. data/lib/rmtools/lang.rb +1 -0
  70. data/lib/rmtools/rand/array.rb +39 -0
  71. data/lib/rmtools/rand/enum.rb +26 -0
  72. data/lib/rmtools/rand/range.rb +13 -0
  73. data/lib/rmtools/{random.rb → rand/string.rb} +13 -107
  74. data/lib/rmtools/rand.rb +1 -0
  75. data/lib/rmtools/require.rb +13 -0
  76. data/lib/rmtools/setup.rb +6 -5
  77. data/lib/rmtools/text/string_parse.rb +60 -0
  78. data/lib/rmtools/{stringscanner.rb → text/string_scanner.rb} +3 -2
  79. data/lib/rmtools/text/string_simple.rb +75 -0
  80. data/lib/rmtools/text/string_split.rb +148 -0
  81. data/lib/rmtools/text/textilize.rb +44 -0
  82. data/lib/rmtools/text.rb +1 -0
  83. data/lib/rmtools/time/global.rb +17 -0
  84. data/lib/rmtools/time/russian.rb +47 -0
  85. data/lib/rmtools/time.rb +1 -32
  86. data/lib/rmtools/xml/document.rb +28 -0
  87. data/lib/rmtools/xml/finders.rb +84 -0
  88. data/lib/rmtools/xml/libxml.rb +11 -0
  89. data/lib/rmtools/xml/node.rb +196 -0
  90. data/lib/rmtools/xml/string.rb +43 -0
  91. data/lib/rmtools/xml/xpath.rb +32 -0
  92. data/lib/rmtools/xml.rb +7 -0
  93. data/lib/rmtools.rb +8 -44
  94. metadata +97 -72
  95. data/lib/rmtools/arguments.rb +0 -24
  96. data/lib/rmtools/array.rb +0 -189
  97. data/lib/rmtools/binding.rb +0 -23
  98. data/lib/rmtools/coloring.rb +0 -82
  99. data/lib/rmtools/cyr-time.rb +0 -49
  100. data/lib/rmtools/cyrilic.rb +0 -124
  101. data/lib/rmtools/dumps.rb +0 -192
  102. data/lib/rmtools/enum.rb +0 -90
  103. data/lib/rmtools/io.rb +0 -303
  104. data/lib/rmtools/js.rb +0 -25
  105. data/lib/rmtools/limited_string.rb +0 -17
  106. data/lib/rmtools/logging.rb +0 -158
  107. data/lib/rmtools/proc.rb +0 -25
  108. data/lib/rmtools/range.rb +0 -100
  109. data/lib/rmtools/string.rb +0 -276
  110. data/lib/rmtools/traceback.rb +0 -106
data/History.txt CHANGED
@@ -2,3 +2,8 @@
2
2
 
3
3
  * Divided by classes and packed as gem
4
4
 
5
+ === Version 1.1.0
6
+
7
+ * Fixed some bugs
8
+ * Divided by semantics
9
+ * Compatible with ruby 1.8.7 (2010-08-16 patchlevel 302)
data/Manifest.txt CHANGED
@@ -2,34 +2,92 @@ ext/extconf.rb
2
2
  ext/rmtools.h
3
3
  ext/rmtools.cpp
4
4
  lib/rmtools.rb
5
- lib/rmtools/arguments.rb
6
- lib/rmtools/numeric.rb
7
- lib/rmtools/string.rb
8
- lib/rmtools/io.rb
9
- lib/rmtools/js.rb
10
- lib/rmtools/cyrilic.rb
11
- lib/rmtools/coloring.rb
12
- lib/rmtools/stringscanner.rb
13
- lib/rmtools/enum.rb
14
- lib/rmtools/string_to_proc.rb
15
- lib/rmtools/traceback.rb
5
+ lib/rmtools/db/active_record.rb
6
+ lib/rmtools/fs/io.rb
7
+ lib/rmtools/fs/file.rb
8
+ lib/rmtools/fs/dir.rb
9
+ lib/rmtools/fs/tools.rb
10
+ lib/rmtools/ip/numeric.rb
11
+ lib/rmtools/ip/string.rb
12
+ lib/rmtools/xml/xpath.rb
13
+ lib/rmtools/xml/string.rb
14
+ lib/rmtools/xml/libxml.rb
15
+ lib/rmtools/xml/finders.rb
16
+ lib/rmtools/xml/node.rb
17
+ lib/rmtools/xml/document.rb
18
+ lib/rmtools/b.rb
19
+ lib/rmtools/core/arguments.rb
20
+ lib/rmtools/core/numeric.rb
21
+ lib/rmtools/core/js.rb
22
+ lib/rmtools/core/string_compliance.rb
23
+ lib/rmtools/core/kernel.rb
24
+ lib/rmtools/core/boolean.rb
25
+ lib/rmtools/core/module.rb
26
+ lib/rmtools/core/object.rb
27
+ lib/rmtools/core/regexp.rb
28
+ lib/rmtools/core/class.rb
29
+ lib/rmtools/core/proc.rb
30
+ lib/rmtools/lang/shortcuts.rb
31
+ lib/rmtools/lang/ansi.rb
32
+ lib/rmtools/lang/regexp.rb
33
+ lib/rmtools/lang/cyrillic.rb
34
+ lib/rmtools/rand/string.rb
35
+ lib/rmtools/rand/enum.rb
36
+ lib/rmtools/rand/range.rb
37
+ lib/rmtools/rand/array.rb
38
+ lib/rmtools/text/string_simple.rb
39
+ lib/rmtools/text/string_scanner.rb
40
+ lib/rmtools/text/textilize.rb
41
+ lib/rmtools/text/string_split.rb
42
+ lib/rmtools/text/string_parse.rb
43
+ lib/rmtools/time/russian.rb
44
+ lib/rmtools/time/global.rb
45
+ lib/rmtools/rand.rb
46
+ lib/rmtools/console/coloring.rb
47
+ lib/rmtools/console/printing.rb
48
+ lib/rmtools/console/highlight.rb
49
+ lib/rmtools/core.rb
50
+ lib/rmtools/db.rb
51
+ lib/rmtools/debug/present.rb
52
+ lib/rmtools/debug/traceback.rb
53
+ lib/rmtools/debug/binding.rb
54
+ lib/rmtools/debug/logging.rb
55
+ lib/rmtools/debug/highlight.rb
56
+ lib/rmtools/debug/timer.rb
57
+ lib/rmtools/fs.rb
58
+ lib/rmtools/ip.rb
59
+ lib/rmtools/lang.rb
60
+ lib/rmtools/functional.rb
61
+ lib/rmtools/experimental.rb
62
+ lib/rmtools/functional/string_to_proc.rb
63
+ lib/rmtools/functional/fold.rb
64
+ lib/rmtools/functional/unfold.rb
65
+ lib/rmtools/text.rb
66
+ lib/rmtools/console.rb
16
67
  lib/rmtools/time.rb
17
- lib/rmtools/random.rb
18
- lib/rmtools/binding.rb
19
- lib/rmtools/printing.rb
20
- lib/rmtools/range.rb
21
- lib/rmtools/tree.rb
22
- lib/rmtools/boolean.rb
23
- lib/rmtools/hash.rb
24
- lib/rmtools/logging.rb
25
- lib/rmtools/limited_string.rb
26
- lib/rmtools/module.rb
27
- lib/rmtools/object.rb
28
- lib/rmtools/array.rb
68
+ lib/rmtools/debug_notrace.rb
69
+ lib/rmtools/debug.rb
70
+ lib/rmtools/enumerable/common.rb
71
+ lib/rmtools/enumerable/range.rb
72
+ lib/rmtools/enumerable/hash.rb
73
+ lib/rmtools/enumerable/array.rb
74
+ lib/rmtools/enumerable/object_space.rb
75
+ lib/rmtools/enumerable/array_iterators.rb
76
+ lib/rmtools/require.rb
77
+ lib/rmtools/experimental/numeric.rb
78
+ lib/rmtools/experimental/string.rb
79
+ lib/rmtools/experimental/blackhole.rb
80
+ lib/rmtools/experimental/tree.rb
81
+ lib/rmtools/experimental/deprecation.rb
82
+ lib/rmtools/experimental/dumps.rb
83
+ lib/rmtools/experimental/rails_backtrace.rb
84
+ lib/rmtools/xml.rb
85
+ lib/rmtools/conversions.rb
29
86
  lib/rmtools/setup.rb
30
- lib/rmtools/cyr-time.rb
31
- lib/rmtools/dumps.rb
32
- lib/rmtools/proc.rb
87
+ lib/rmtools/conversions/string.rb
88
+ lib/rmtools/conversions/enum.rb
89
+ lib/rmtools/conversions/int.rb
90
+ lib/rmtools/enumerable.rb
33
91
  ./Rakefile
34
92
  ./Manifest.txt
35
93
  ./License.txt
data/Rakefile CHANGED
@@ -4,7 +4,7 @@ compile_manifest
4
4
 
5
5
  begin
6
6
  require 'hoe'
7
- config = Hoe.new('rmtools', '1.0.0') do |h|
7
+ config = Hoe.new('rmtools', '1.1.0') do |h|
8
8
  h.developer("Shinku Templar", "tinbka@gmail.com")
9
9
 
10
10
  h.summary = 'Yet another Ruby applied framework'
@@ -13,7 +13,7 @@ begin
13
13
  h.url = 'http://github.com/tinbka'
14
14
 
15
15
  h.extra_deps << ['rake','>= 0.8.7']
16
- h.extra_deps << ['activesupport','>= 2.3.5']
16
+ h.extra_deps << ['activesupport','>= 2.3.8']
17
17
  end
18
18
  config.spec.extensions << 'ext/extconf.rb'
19
19
  rescue LoadError
@@ -23,6 +23,8 @@ rescue Exception => e
23
23
  STDERR.puts "error message is: #{e.message}"
24
24
  end
25
25
 
26
+ puts "Installing RMTools..."
27
+
26
28
  ruby = RbConfig::CONFIG['RUBY_INSTALL_NAME']
27
29
  windoze = PLATFORM =~ /mswin32/
28
30
  make = windoze ? 'nmake' : 'make'
@@ -35,5 +37,6 @@ Dir.chdir "ext" do
35
37
  if File.file? 'Makefile'
36
38
  system "#{make} clean" and FileUtils.rm_f "Makefile"
37
39
  end
38
- end unless ext_files_not_modified
39
-
40
+ end unless ext_files_not_modified 'rmtools', '1.1.0'
41
+
42
+ puts "RMTools installed!"
data/ext/extconf.rb CHANGED
@@ -5,7 +5,7 @@ if RUBY_VERSION >= "1.9"
5
5
  $CFLAGS += " -DRUBY_IS_19"
6
6
  end
7
7
 
8
- $LDFLAGS += "-module"
8
+ $LDFLAGS += " -module"
9
9
 
10
10
  dir_config("ruby19")
11
11
  create_makefile("rmtools")
data/ext/rmtools.cpp CHANGED
@@ -225,19 +225,24 @@ static VALUE rb_ary_uniq_by(VALUE ary)
225
225
  /*
226
226
  * Make hash with unique items of +self+ or (when block given)
227
227
  * unique results of items yield for keys and
228
- * count of them in +self, or (with option :fill) arrays of themselves,
229
- * or (with option :indexes) arrays of indexes of them for values
228
+ * count of them in +self,
229
+ * or (with option :fill) arrays of yield results,
230
+ * or (with option :indexes) arrays of indexes of them,
231
+ * or (with option :group) arrays of themselves for values
230
232
  *
231
- * [1, 2, 2, 3, 3, 3].count
232
- * => {1=>1, 2=>2, 3=>3}
233
- * [1, 2, 2, 3, 3, 3].count :fill
234
- * => {1=>[1], 2=>[2, 2], 3=>[3, 3, 3]}
235
- * [1, 2, 2, 3, 3, 3].count :indexes
236
- * => {1=>[0], 2=>[1, 2], 3=>[3, 4, 5]}
237
- * [1, 2, 2, 3, 3, 3].count(:indexes) {|i| i%2}
238
- * => {0=>[1, 2], 1=>[0, 3, 4, 5]}
239
- * [1, 2, 2, 3, 3, 3].count {|i| i%2}
240
- * => {0=>2, 1=>4}
233
+ * [1, 2, 2, 2, 3, 3].count
234
+ * => {1=>1, 2=>3, 3=>2}
235
+ * [1, 2, 2, 2, 3, 3].count {|i| i%2}
236
+ * => {0=>3, 1=>3}
237
+ * [1, 2, 2, 2, 3, 3].count :fill
238
+ * => {1=>[1], 2=>[2, 2, 2], 3=>[3, 3]}
239
+ * [1, 2, 2, 2, 3, 3].count :indexes
240
+ * => {1=>[0], 2=>[1, 2, 3], 3=>[4, 5]}
241
+ * [1, 2, 2, 2, 3, 3].count(:indexes) {|i| i%2}
242
+ * => {0=>[1, 2, 3], 1=>[0, 4, 5]}
243
+ * :group is analogue to rails' group_by but twice faster
244
+ * [1, 2, 2, 2, 3, 3].count(:group) {|i| i%2}
245
+ * => {0=>[2, 2, 2], 1=>[1, 3, 3]}
241
246
  */
242
247
  static VALUE rb_ary_count_items(int argc, VALUE *argv, VALUE ary)
243
248
  {
@@ -276,6 +281,16 @@ static VALUE rb_ary_count_items(int argc, VALUE *argv, VALUE ary)
276
281
  return hash;
277
282
  }
278
283
 
284
+ /*
285
+ * call-seq:
286
+ * ary.partition {| obj | block } => [ true_array, false_array ]
287
+ *
288
+ * Same as Enumerable#partition, but twice faster
289
+ *
290
+ * [5, 6, 1, 2, 4, 3].partition {|i| (i&1).zero?} #=> [[2, 4, 6], [1, 3, 5]]
291
+ *
292
+ */
293
+
279
294
  static VALUE rb_ary_partition(VALUE ary)
280
295
  {
281
296
  VALUE select, reject;
data/ext/rmtools.h CHANGED
@@ -94,12 +94,13 @@ static const size_t SLOT_SIZE = sizeof(RVALUE);
94
94
  #include <re.h>
95
95
 
96
96
  # define STR_SET_LEN(x, i) (RSTRING(x)->len = (i))
97
-
97
+
98
+ #ifndef RFLOAT_VALUE
98
99
  # define RFLOAT_VALUE(f) (RFLOAT(f)->value)
99
-
100
100
  # define RBIGNUM_DIGITS(f) (RBIGNUM(f)->digits)
101
101
  # define RBIGNUM_LEN(f) (RBIGNUM(f)->len)
102
-
102
+ #endif
103
+
103
104
  # define ARY_SET_LEN(x, i) (RARRAY(x)->len = (i))
104
105
 
105
106
  # define BDIGITS(x) ((BDIGIT*)RBIGNUM(x)->digits)
@@ -141,11 +142,11 @@ static const size_t SLOT_SIZE = sizeof(RVALUE);
141
142
 
142
143
 
143
144
  // shortcuts for debug r_r
144
- extern void rb_log_std(const char* str)
145
+ extern void rb_log(const char* str)
145
146
  {
146
147
  rb_funcall(rb_gv_get("$log"), rb_intern("debug"), 1, rb_str_new2(str));
147
148
  }
148
- extern void rb_log_obj_std(VALUE obj)
149
+ extern void rb_log_obj(VALUE obj)
149
150
  {
150
151
  rb_funcall(rb_gv_get("$log"), rb_intern("debug"), 1, obj);
151
152
  }
data/lib/rmtools/b.rb ADDED
@@ -0,0 +1,18 @@
1
+ # {obj.b -> self or false} with python logic
2
+ # Since 2.3.8 active_support has analogue: #presence
3
+
4
+ class Object
5
+ def b; self end
6
+ end
7
+ class Numeric
8
+ def b; !zero? && self end
9
+ end
10
+ class String
11
+ def b; !empty? && self end
12
+ end
13
+ class Proc
14
+ def b; (self != NULL) && self end
15
+ end
16
+ module Enumerable
17
+ def b; !empty? && self end
18
+ end
@@ -0,0 +1,72 @@
1
+ # encoding: utf-8
2
+ module RMTools
3
+
4
+ class Coloring
5
+ __init__
6
+ method_defined? :b and undef_method :b
7
+ method_defined? :p and undef_method :p
8
+
9
+ if !defined? ::BOLD
10
+ BOLD = 1
11
+ UNDERLINE = 4
12
+ GRAYBG = 5
13
+ BOLDBG = 7
14
+
15
+ KEY = BLACK = 30
16
+ RED = 31
17
+ GREEN = 32
18
+ YELLOW = 33
19
+ BLUE = 34
20
+ PURPLE = 35
21
+ CYAN = 36
22
+ GRAY = 37
23
+
24
+ Colors = {:black => 30, :red => 31, :green => 32, :yellow => 33, :blue => 34, :purple => 35, :cyan => 36, :gray => 37,
25
+ :k => 30, :r => 31, :g => 32, :y => 33, :b => 34, :p => 35, :c => 36
26
+ }.unify_keys
27
+ Effects = {:bold => 1, :underline => 4, :graybg => 5, :boldbg => 7,
28
+ :b => 1, :u => 4, :gbg => 5, :bbg => 7
29
+ }.unify_keys
30
+ end
31
+
32
+ def paint(str, num=nil, effect=nil)
33
+ # default cmd.exe cannot into ANSI
34
+ return str if ENV['ComSpec'] =~ /cmd(.exe)?$/
35
+ num = Colors[num] if num.is String
36
+ effect = Effects[effect] if effect.is String
37
+ if num and effect
38
+ "\e[#{effect};#{num}m#{str}\e[m"
39
+ elsif effect
40
+ "\e[#{effect}m#{str}\e[m"
41
+ elsif num
42
+ "\e[#{num}m#{str}\e[m"
43
+ else str
44
+ end
45
+ end
46
+
47
+ def method_missing(m, str)
48
+ paint str, *(m.to_s/"_")
49
+ end
50
+
51
+ def clean str
52
+ str.gsub(/\e\[.*?m/, '')
53
+ end
54
+
55
+ end
56
+
57
+ Painter = Coloring.new
58
+ ['sub', 'gsub', 'sub!', 'gsub!'].each {|m|
59
+ Coloring.module_eval "
60
+ def #{m.sub 'sub', 'hl'} str, pattern, color=:red_bold
61
+ str.#{m}(pattern) {|word| send color, word}
62
+ end
63
+ "
64
+ module_eval "
65
+ def #{m.sub 'sub', 'hl'} str, pattern, color=:red_bold
66
+ str.#{m}(pattern) {|word| Painter.send color, word}
67
+ end
68
+ "
69
+ # module_function m.to_sym
70
+ }
71
+
72
+ end
@@ -0,0 +1,13 @@
1
+ require_with_path 'console/coloring'
2
+
3
+ class String
4
+
5
+ def find_hl(pat, range=1000) idx = case pat
6
+ when String; index pat
7
+ when Regexp; self =~ pat
8
+ else raise TypeError, "pattern must be string or regexp"
9
+ end
10
+ puts RMTools.ghl(self[[idx-range, 0].max, 2*range], pat)
11
+ end
12
+
13
+ end
@@ -1,16 +1,23 @@
1
1
  module RMTools
2
2
 
3
+ # transparent print: print text (or object details) and erase it
3
4
  class TempPrinter
4
5
  __init__
5
6
 
6
7
  def initialize object=nil, format=nil
7
8
  if @object = object
8
9
  @format = '"' + format.gsub(/:([a-z_]\w*)/, '#{@\1.inspect}') + '"'
10
+ else
11
+ @format = format
9
12
  end
10
13
  end
11
14
 
12
- def format_str
13
- @object.instance_eval @format
15
+ def format_str params_hash=nil
16
+ if params_hash
17
+ params_hash.each {|k, v| @format.sub! ":#{k}", v.inspect}
18
+ else
19
+ @object.instance_eval @format
20
+ end
14
21
  end
15
22
 
16
23
  def p(str=format_str)
@@ -38,4 +45,12 @@ module RMTools
38
45
 
39
46
  end
40
47
 
48
+ end
49
+
50
+ module RMTools
51
+
52
+ def tick!
53
+ print %W{|\b /\b -\b \\\b +\b X\b}.rand
54
+ end
55
+
41
56
  end
@@ -0,0 +1 @@
1
+ require_with_path __FILE__, '*'
@@ -0,0 +1,23 @@
1
+ require 'cgi'
2
+
3
+ module Enumerable
4
+
5
+ # Simple http stringifying. Also stringifies multi-value hashes
6
+ # {'a'=>'10&20&30'}.urlencode
7
+ # => "a=10&a=20&a=30"
8
+ def urlencode
9
+ map {|k, v| next if !v
10
+ k, v = k.to_s, v.to_s
11
+ if v =~ /&/
12
+ v = v/'&'
13
+ v.map {|val| "#{CGI.escape(k)}=#{CGI.escape(val)}"} * '&'
14
+ elsif k =~ /&/
15
+ k = k/'&'
16
+ k.map {|key| "#{CGI.escape(key)}=#{CGI.escape(v)}"} * '&'
17
+ else
18
+ "#{CGI.escape(k)}=#{CGI.escape(v)}"
19
+ end
20
+ } * '&'
21
+ end
22
+
23
+ end
@@ -0,0 +1,47 @@
1
+ class Integer
2
+
3
+ def kb; self*1024 end
4
+ def mb; self*1048576 end
5
+ def gb; self*1073741824 end
6
+
7
+ def to_timestr(t=nil)
8
+ if t
9
+ if t.in [:minutes, :min, :m]
10
+ "#{self/60} minutes"
11
+ elsif t.in [:hours, :h]
12
+ "#{self/3600} hours"
13
+ elsif t.in [:days, :d]
14
+ "#{self/86400} days"
15
+ end
16
+ elsif self < 60
17
+ "#{self} seconds"
18
+ elsif self < 3600
19
+ "#{self/60} minutes"
20
+ elsif self < 86400
21
+ "#{self/3600} hours"
22
+ else
23
+ "#{self/86400} days"
24
+ end
25
+ end
26
+
27
+ def bytes(t=nil)
28
+ if t
29
+ if :kb == t
30
+ "%.2fKB"%[to_f/1024]
31
+ elsif :mb == t
32
+ "%.2fMB"%[to_f/1048576]
33
+ elsif :gb == t
34
+ "%.2fGB"%[to_f/1073741824]
35
+ end
36
+ elsif self < 1024
37
+ "#{self}B"
38
+ elsif self < 1048576
39
+ "%.2fKB"%[to_f/1024]
40
+ elsif self < 1073741824
41
+ "%.2fMB"%[to_f/1048576]
42
+ else
43
+ "%.2fGB"%[to_f/1073741824]
44
+ end
45
+ end
46
+
47
+ end
@@ -0,0 +1,26 @@
1
+ require 'cgi'
2
+
3
+ class String
4
+
5
+ # with default delimiters - inversion of #urlencode
6
+ def to_hash(unscp=true, params_delim='&', k_v_delim='=')
7
+ params = split(params_delim)
8
+ h = {}
9
+ params.each {|par|
10
+ str = par.split(k_v_delim, 2)
11
+ if unscp
12
+ h[CGI.unescape(str[0]) || ''] = CGI.unescape(str[1] || '')
13
+ else
14
+ h[str[0]] = str[1]
15
+ end
16
+ }
17
+ h
18
+ end
19
+
20
+ # inversion of #to_json
21
+ # active support activesupport
22
+ def from_json
23
+ ActiveSupport::JSON.decode self
24
+ end
25
+
26
+ end
@@ -0,0 +1 @@
1
+ require_with_path __FILE__, '*'
@@ -0,0 +1,52 @@
1
+ require 'active_support/core_ext/hash'
2
+
3
+ class Array
4
+
5
+ # a, b, opts = [<hash1>].fetch_opts([<hash2>, <object1>]) may work unintuitive:
6
+ # you'll get <hash1> as `a' and not as `opts'
7
+ # So if function is not implying that some argument other than `opts' definetly
8
+ # must be a hash, don't make hash default.
9
+ # Better put hashie argument into opts like
10
+ # b, opts = [<hash1>].fetch([<object1>], :a => <hash2>)
11
+ # and get `a' from `'opts' hash
12
+ def fetch_opts(defaults=[], opts={})
13
+ if Hash === defaults
14
+ opts = defaults
15
+ defaults = []
16
+ $log.warn "fetch_opts(<hash>) now changed"
17
+ end
18
+ opts &&= if self[-1].kind_of?(Hash) and !defaults[size-1].kind_of?(Hash)
19
+ opts.merge pop
20
+ else
21
+ opts.dup
22
+ end
23
+ if defaults == :flags
24
+ defauls = [:flags]
25
+ end
26
+ if defaults.last == :flags
27
+ defaults.pop
28
+ flags = defaults.size..-1
29
+ if defaults.size < size
30
+ self[flags].each {|flag| opts[flag] = true}
31
+ self[flags] = []
32
+ end
33
+ end
34
+ each_index {|i| import(defaults, i) if self[i] == :def}
35
+ defaults.slice! 0, size
36
+ concat defaults << opts
37
+ end
38
+ alias :fetch_options :fetch_opts
39
+ alias :get_opts :fetch_opts
40
+
41
+ def valid_types(pattern_ary)
42
+ each_with_index.find {|var, i|
43
+ pattern = pattern_ary[i]
44
+ if pattern.is Array
45
+ pattern.find {|j| !(pattern[j] === var[i])}
46
+ else !(pattern === var[i])
47
+ end
48
+ }
49
+ end
50
+
51
+ end
52
+
@@ -1,17 +1,4 @@
1
1
  # encoding: utf-8
2
- class Numeric
3
- def b; (self != 0) && self end
4
- end
5
- class String
6
- def b; !empty? && self end
7
- end
8
- class Proc
9
- def b; (self != NULL) && self end
10
- end
11
- module Enumerable
12
- def b; !empty? && self end
13
- end
14
-
15
2
  class TrueClass
16
3
  def to_i; 1; end
17
4
  def <=>(obj)
@@ -0,0 +1,41 @@
1
+ class Class
2
+
3
+ # define python-style initializer
4
+ def __init__
5
+ modname, classname = name.match(/^(?:(.+)::)?([^:]+)$/)[1..2]
6
+ classname ||= modname
7
+ mod = '::'.in(name) ? eval(modname) : RMTools
8
+ mod.module_eval "def #{classname} *args; #{name}.new *args end
9
+ module_function :#{classname}"
10
+ if mod != RMTools
11
+ mod.each_child {|c| c.class_eval "include #{mod}; extend #{mod}" if !c.in c.children}
12
+ end
13
+ end
14
+
15
+ def method_proxy *vars
16
+ buffered_missing = instance_methods.grep(/method_missing/).sort.last || 'method_missing'
17
+ # next arg overrides previous
18
+ vars.each {|v|
19
+ class_eval "
20
+ alias #{buffered_missing.bump! '_'} method_missing
21
+ def method_missing *args, &block
22
+ #{v}.send *args, &block
23
+ rescue NoMethodError
24
+ #{buffered_missing} *args, &block
25
+ end"
26
+ }
27
+ end
28
+
29
+ def personal_methods filter=//
30
+ (self.singleton_methods - self.superclass.singleton_methods).sort!.grep(filter)
31
+ end
32
+
33
+ def my_instance_methods filter=//
34
+ (self.public_instance_methods - Object.public_instance_methods).sort!.grep(filter)
35
+ end
36
+
37
+ def personal_instance_methods filter=//
38
+ (self.public_instance_methods - self.superclass.public_instance_methods).sort!.grep(filter)
39
+ end
40
+
41
+ end
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+ # js hash getter/setter and string concat logic
3
+ class Hash
4
+
5
+ # hash = {}
6
+ # hash.abc = 123
7
+ # hash # => {"abc"=>123}
8
+ # hash.abc # => 123
9
+ # hash.unknown_function(321) # => raise NoMethodError
10
+ # hash.unknown_function # => nil
11
+ # hash[:def] = 456
12
+ # hash.def # => 456
13
+ def method_missing(met, *args)
14
+ str = met.id2name
15
+ if str[/=$/]
16
+ self[str[0..-2]] = args[0]
17
+ else
18
+ raise NoMethodError, "undefined method `#{str}' for #{self}:#{(self.class)}" if !args.empty?
19
+ a = self[str]
20
+ (a == default) ? self[met] : a
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+ class String
27
+ if !method_defined? :plus
28
+ alias :plus :+
29
+ end
30
+
31
+ # immutable:
32
+ # '123' + 95 # => '12395'
33
+ # '123'.plus 95 # => raise TypeError
34
+ # mutable:
35
+ # '123' << 95 # => '12395'
36
+ # '123'.concat 95 # => '123_'
37
+ def +(str)
38
+ plus str.to_s
39
+ end
40
+
41
+ def <<(str)
42
+ concat str.to_s
43
+ end
44
+
45
+ end