nuggets 0.9.9 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (222) hide show
  1. checksums.yaml +4 -4
  2. data/COPYING +663 -0
  3. data/ChangeLog +7 -0
  4. data/README +74 -0
  5. data/Rakefile +50 -0
  6. data/lib/nuggets.rb +73 -0
  7. data/lib/nuggets/all.rb +35 -0
  8. data/lib/nuggets/all_mixins.rb +33 -0
  9. data/lib/nuggets/ansicolor2css.rb +124 -0
  10. data/lib/nuggets/argv/option.rb +3 -0
  11. data/lib/nuggets/argv/option_mixin.rb +84 -0
  12. data/lib/nuggets/array/boost.rb +5 -0
  13. data/lib/nuggets/array/boost_mixin.rb +77 -0
  14. data/lib/nuggets/array/combination.rb +62 -0
  15. data/lib/nuggets/array/correlation.rb +5 -0
  16. data/lib/nuggets/array/correlation_mixin.rb +63 -0
  17. data/lib/nuggets/array/flatten_once.rb +56 -0
  18. data/lib/nuggets/array/flush.rb +5 -0
  19. data/lib/nuggets/array/flush_mixin.rb +46 -0
  20. data/lib/nuggets/array/format.rb +65 -0
  21. data/lib/nuggets/array/hashify.rb +5 -0
  22. data/lib/nuggets/array/hashify_mixin.rb +47 -0
  23. data/lib/nuggets/array/histogram.rb +5 -0
  24. data/lib/nuggets/array/histogram_mixin.rb +169 -0
  25. data/lib/nuggets/array/in_order.rb +50 -0
  26. data/lib/nuggets/array/limit.rb +5 -0
  27. data/lib/nuggets/array/limit_mixin.rb +56 -0
  28. data/lib/nuggets/array/mean.rb +5 -0
  29. data/lib/nuggets/array/mean_mixin.rb +202 -0
  30. data/lib/nuggets/array/median.rb +5 -0
  31. data/lib/nuggets/array/median_mixin.rb +73 -0
  32. data/lib/nuggets/array/mode.rb +5 -0
  33. data/lib/nuggets/array/mode_mixin.rb +69 -0
  34. data/lib/nuggets/array/monotone.rb +83 -0
  35. data/lib/nuggets/array/only.rb +41 -0
  36. data/lib/nuggets/array/rand.rb +44 -0
  37. data/lib/nuggets/array/regression.rb +5 -0
  38. data/lib/nuggets/array/regression_mixin.rb +149 -0
  39. data/lib/nuggets/array/runiq.rb +5 -0
  40. data/lib/nuggets/array/runiq_mixin.rb +52 -0
  41. data/lib/nuggets/array/shuffle.rb +132 -0
  42. data/lib/nuggets/array/standard_deviation.rb +5 -0
  43. data/lib/nuggets/array/standard_deviation_mixin.rb +50 -0
  44. data/lib/nuggets/array/to_hash.rb +64 -0
  45. data/lib/nuggets/array/variance.rb +5 -0
  46. data/lib/nuggets/array/variance_mixin.rb +81 -0
  47. data/lib/nuggets/content_type.rb +97 -0
  48. data/lib/nuggets/dotted_decimal.rb +59 -0
  49. data/lib/nuggets/enumerable/agrep.rb +79 -0
  50. data/lib/nuggets/enumerable/all_any_extended.rb +66 -0
  51. data/lib/nuggets/enumerable/minmax.rb +102 -0
  52. data/lib/nuggets/env/set.rb +3 -0
  53. data/lib/nuggets/env/set_mixin.rb +67 -0
  54. data/lib/nuggets/env/user_encoding.rb +3 -0
  55. data/lib/nuggets/env/user_encoding_mixin.rb +54 -0
  56. data/lib/nuggets/env/user_home.rb +3 -0
  57. data/lib/nuggets/env/user_home_mixin.rb +56 -0
  58. data/lib/nuggets/file/ext.rb +5 -0
  59. data/lib/nuggets/file/ext_mixin.rb +87 -0
  60. data/lib/nuggets/file/replace.rb +5 -0
  61. data/lib/nuggets/file/replace_mixin.rb +55 -0
  62. data/lib/nuggets/file/sub.rb +5 -0
  63. data/lib/nuggets/file/sub_mixin.rb +95 -0
  64. data/lib/nuggets/file/which.rb +5 -0
  65. data/lib/nuggets/file/which_mixin.rb +72 -0
  66. data/lib/nuggets/hash/at.rb +68 -0
  67. data/lib/nuggets/hash/deep_fetch.rb +5 -0
  68. data/lib/nuggets/hash/deep_fetch_mixin.rb +74 -0
  69. data/lib/nuggets/hash/deep_merge.rb +5 -0
  70. data/lib/nuggets/hash/deep_merge_mixin.rb +53 -0
  71. data/lib/nuggets/hash/idmap.rb +5 -0
  72. data/lib/nuggets/hash/idmap_mixin.rb +41 -0
  73. data/lib/nuggets/hash/in_order.rb +43 -0
  74. data/lib/nuggets/hash/insert.rb +53 -0
  75. data/lib/nuggets/hash/nest.rb +5 -0
  76. data/lib/nuggets/hash/nest_mixin.rb +78 -0
  77. data/lib/nuggets/hash/only.rb +52 -0
  78. data/lib/nuggets/hash/seen.rb +5 -0
  79. data/lib/nuggets/hash/seen_mixin.rb +58 -0
  80. data/lib/nuggets/hash/unroll.rb +5 -0
  81. data/lib/nuggets/hash/unroll_mixin.rb +88 -0
  82. data/lib/nuggets/hash/zip.rb +5 -0
  83. data/lib/nuggets/hash/zip_mixin.rb +159 -0
  84. data/lib/nuggets/i18n.rb +155 -0
  85. data/lib/nuggets/integer/factorial.rb +56 -0
  86. data/lib/nuggets/integer/length.rb +5 -0
  87. data/lib/nuggets/integer/length_mixin.rb +49 -0
  88. data/lib/nuggets/integer/map.rb +5 -0
  89. data/lib/nuggets/integer/map_mixin.rb +42 -0
  90. data/lib/nuggets/integer/to_binary_s.rb +38 -0
  91. data/lib/nuggets/io/agrep.rb +43 -0
  92. data/lib/nuggets/io/interact.rb +5 -0
  93. data/lib/nuggets/io/interact_mixin.rb +159 -0
  94. data/lib/nuggets/io/modes.rb +121 -0
  95. data/lib/nuggets/io/null.rb +5 -0
  96. data/lib/nuggets/io/null_mixin.rb +40 -0
  97. data/lib/nuggets/io/redirect.rb +5 -0
  98. data/lib/nuggets/io/redirect_mixin.rb +50 -0
  99. data/lib/nuggets/lazy_attr.rb +44 -0
  100. data/lib/nuggets/log_parser.rb +70 -0
  101. data/lib/nuggets/log_parser/apache.rb +101 -0
  102. data/lib/nuggets/log_parser/rails.rb +219 -0
  103. data/lib/nuggets/net/success.rb +59 -0
  104. data/lib/nuggets/numeric/between.rb +2 -0
  105. data/lib/nuggets/numeric/duration.rb +100 -0
  106. data/lib/nuggets/numeric/limit.rb +62 -0
  107. data/lib/nuggets/numeric/signum.rb +52 -0
  108. data/lib/nuggets/numeric/to_multiple.rb +61 -0
  109. data/lib/nuggets/object/blank.rb +20 -0
  110. data/lib/nuggets/object/blank_mixin.rb +99 -0
  111. data/lib/nuggets/object/boolean.rb +5 -0
  112. data/lib/nuggets/object/boolean_mixin.rb +61 -0
  113. data/lib/nuggets/object/eigenclass.rb +2 -0
  114. data/lib/nuggets/object/ghost_class.rb +2 -0
  115. data/lib/nuggets/object/metaclass.rb +2 -0
  116. data/lib/nuggets/object/msend.rb +5 -0
  117. data/lib/nuggets/object/msend_mixin.rb +43 -0
  118. data/lib/nuggets/object/silence.rb +5 -0
  119. data/lib/nuggets/object/silence_mixin.rb +44 -0
  120. data/lib/nuggets/object/singleton_class.rb +5 -0
  121. data/lib/nuggets/object/singleton_class_mixin.rb +95 -0
  122. data/lib/nuggets/object/uniclass.rb +2 -0
  123. data/lib/nuggets/object/virtual_class.rb +2 -0
  124. data/lib/nuggets/pluggable.rb +91 -0
  125. data/lib/nuggets/proc/bind.rb +5 -0
  126. data/lib/nuggets/proc/bind_mixin.rb +51 -0
  127. data/lib/nuggets/range/quantile.rb +5 -0
  128. data/lib/nuggets/range/quantile_mixin.rb +42 -0
  129. data/lib/nuggets/ruby.rb +235 -0
  130. data/lib/nuggets/statistics.rb +12 -0
  131. data/lib/nuggets/statistics_mixins.rb +12 -0
  132. data/lib/nuggets/string/camelscore.rb +5 -0
  133. data/lib/nuggets/string/camelscore_mixin.rb +116 -0
  134. data/lib/nuggets/string/capitalize_first.rb +46 -0
  135. data/lib/nuggets/string/case.rb +81 -0
  136. data/lib/nuggets/string/evaluate.rb +5 -0
  137. data/lib/nuggets/string/evaluate_mixin.rb +47 -0
  138. data/lib/nuggets/string/msub.rb +84 -0
  139. data/lib/nuggets/string/nsub.rb +65 -0
  140. data/lib/nuggets/string/sub_with_md.rb +111 -0
  141. data/lib/nuggets/string/wc.rb +5 -0
  142. data/lib/nuggets/string/wc_mixin.rb +95 -0
  143. data/lib/nuggets/string/word_wrap.rb +76 -0
  144. data/lib/nuggets/string/xor.rb +5 -0
  145. data/lib/nuggets/string/xor_mixin.rb +59 -0
  146. data/lib/nuggets/tempfile/open.rb +57 -0
  147. data/lib/nuggets/uri/content_type.rb +5 -0
  148. data/lib/nuggets/uri/content_type_mixin.rb +47 -0
  149. data/lib/nuggets/uri/exist.rb +5 -0
  150. data/lib/nuggets/uri/exist_mixin.rb +56 -0
  151. data/lib/nuggets/uri/redirect.rb +5 -0
  152. data/lib/nuggets/uri/redirect_mixin.rb +101 -0
  153. data/lib/nuggets/version.rb +27 -0
  154. data/spec/nuggets/array/boost_spec.rb +50 -0
  155. data/spec/nuggets/array/combination_spec.rb +25 -0
  156. data/spec/nuggets/array/correlation_spec.rb +81 -0
  157. data/spec/nuggets/array/flatten_once_spec.rb +16 -0
  158. data/spec/nuggets/array/flush_spec.rb +43 -0
  159. data/spec/nuggets/array/format_spec.rb +52 -0
  160. data/spec/nuggets/array/hashify_spec.rb +41 -0
  161. data/spec/nuggets/array/histogram_spec.rb +87 -0
  162. data/spec/nuggets/array/in_order_spec.rb +13 -0
  163. data/spec/nuggets/array/limit_spec.rb +62 -0
  164. data/spec/nuggets/array/mean_spec.rb +203 -0
  165. data/spec/nuggets/array/median_spec.rb +77 -0
  166. data/spec/nuggets/array/mode_spec.rb +57 -0
  167. data/spec/nuggets/array/monotone_spec.rb +30 -0
  168. data/spec/nuggets/array/only_spec.rb +26 -0
  169. data/spec/nuggets/array/regression_spec.rb +54 -0
  170. data/spec/nuggets/array/runiq_spec.rb +25 -0
  171. data/spec/nuggets/array/standard_deviation_spec.rb +33 -0
  172. data/spec/nuggets/array/to_hash_spec.rb +28 -0
  173. data/spec/nuggets/array/variance_spec.rb +106 -0
  174. data/spec/nuggets/dotted_decimal_spec.rb +27 -0
  175. data/spec/nuggets/enumerable/all_any_extended_spec.rb +31 -0
  176. data/spec/nuggets/enumerable/minmax_spec.rb +21 -0
  177. data/spec/nuggets/env/set_spec.rb +29 -0
  178. data/spec/nuggets/env/user_encoding_spec.rb +38 -0
  179. data/spec/nuggets/env/user_home_spec.rb +42 -0
  180. data/spec/nuggets/file/ext_spec.rb +38 -0
  181. data/spec/nuggets/file/replace_spec.rb +95 -0
  182. data/spec/nuggets/file/sub_spec.rb +149 -0
  183. data/spec/nuggets/file/which_spec.rb +22 -0
  184. data/spec/nuggets/hash/at_spec.rb +19 -0
  185. data/spec/nuggets/hash/deep_fetch_spec.rb +159 -0
  186. data/spec/nuggets/hash/deep_merge_spec.rb +78 -0
  187. data/spec/nuggets/hash/in_order_spec.rb +12 -0
  188. data/spec/nuggets/hash/insert_spec.rb +13 -0
  189. data/spec/nuggets/hash/nest_spec.rb +102 -0
  190. data/spec/nuggets/hash/only_spec.rb +29 -0
  191. data/spec/nuggets/hash/seen_spec.rb +36 -0
  192. data/spec/nuggets/hash/unroll_spec.rb +68 -0
  193. data/spec/nuggets/i18n_spec.rb +13 -0
  194. data/spec/nuggets/integer/factorial_spec.rb +10 -0
  195. data/spec/nuggets/integer/length_spec.rb +18 -0
  196. data/spec/nuggets/integer/map_spec.rb +19 -0
  197. data/spec/nuggets/integer/to_binary_s_spec.rb +19 -0
  198. data/spec/nuggets/numeric/duration_spec.rb +25 -0
  199. data/spec/nuggets/numeric/limit_spec.rb +16 -0
  200. data/spec/nuggets/numeric/signum_spec.rb +16 -0
  201. data/spec/nuggets/numeric/to_multiple_spec.rb +16 -0
  202. data/spec/nuggets/object/blank_spec.rb +34 -0
  203. data/spec/nuggets/object/boolean_spec.rb +23 -0
  204. data/spec/nuggets/object/msend_spec.rb +25 -0
  205. data/spec/nuggets/object/silence_spec.rb +36 -0
  206. data/spec/nuggets/object/singleton_class_spec.rb +51 -0
  207. data/spec/nuggets/proc/bind_spec.rb +28 -0
  208. data/spec/nuggets/range/quantile_spec.rb +33 -0
  209. data/spec/nuggets/string/camelscore_spec.rb +114 -0
  210. data/spec/nuggets/string/capitalize_first_spec.rb +13 -0
  211. data/spec/nuggets/string/case_spec.rb +31 -0
  212. data/spec/nuggets/string/evaluate_spec.rb +24 -0
  213. data/spec/nuggets/string/msub_spec.rb +20 -0
  214. data/spec/nuggets/string/nsub_spec.rb +13 -0
  215. data/spec/nuggets/string/sub_with_md_spec.rb +25 -0
  216. data/spec/nuggets/string/wc_spec.rb +73 -0
  217. data/spec/nuggets/string/word_wrap_spec.rb +81 -0
  218. data/spec/nuggets/string/xor_spec.rb +57 -0
  219. data/spec/nuggets/uri/content_type_spec.rb +42 -0
  220. data/spec/nuggets/uri/exist_spec.rb +49 -0
  221. data/spec/spec_helper.rb +36 -0
  222. metadata +309 -17
@@ -0,0 +1,5 @@
1
+ require 'nuggets/range/quantile_mixin'
2
+
3
+ class Range
4
+ include Nuggets::Range::QuantileMixin
5
+ end
@@ -0,0 +1,42 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2011 Jens Wille #
7
+ # #
8
+ # Authors: #
9
+ # Jens Wille <jens.wille@gmail.com> #
10
+ # #
11
+ # nuggets is free software; you can redistribute it and/or modify it under #
12
+ # the terms of the GNU Affero General Public License as published by the Free #
13
+ # Software Foundation; either version 3 of the License, or (at your option) #
14
+ # any later version. #
15
+ # #
16
+ # nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
17
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
18
+ # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
19
+ # more details. #
20
+ # #
21
+ # You should have received a copy of the GNU Affero General Public License #
22
+ # along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
23
+ # #
24
+ ###############################################################################
25
+ #++
26
+
27
+ module Nuggets
28
+ class Range
29
+ module QuantileMixin
30
+
31
+ # call-seq:
32
+ # range.quantile(value[, order]) => anInteger
33
+ #
34
+ # Determines the <tt>order</tt>-quantile of +value+ in _range_.
35
+ def quantile(value, order = 100)
36
+ value < first ? 1 : value >= last ? order :
37
+ ((value - first) / ((last - first) / order.to_f)).to_i + 1
38
+ end
39
+
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,235 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2014 Jens Wille #
7
+ # #
8
+ # Authors: #
9
+ # Jens Wille <jens.wille@gmail.com> #
10
+ # #
11
+ # nuggets is free software; you can redistribute it and/or modify it under #
12
+ # the terms of the GNU Affero General Public License as published by the Free #
13
+ # Software Foundation; either version 3 of the License, or (at your option) #
14
+ # any later version. #
15
+ # #
16
+ # nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
17
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
18
+ # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
19
+ # more details. #
20
+ # #
21
+ # You should have received a copy of the GNU Affero General Public License #
22
+ # along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
23
+ # #
24
+ ###############################################################################
25
+ #++
26
+
27
+ require 'rbconfig'
28
+
29
+ module Nuggets
30
+
31
+ # Originally based on Phusion Passenger's
32
+ # {PlatformInfo}[https://github.com/FooBarWidget/passenger/blob/release-3.0.2/lib/phusion_passenger/platform_info/ruby.rb]
33
+ # module.
34
+ #
35
+ #--
36
+ # Phusion Passenger - http://www.modrails.com/
37
+ # Copyright (c) 2010 Phusion
38
+ #
39
+ # "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
40
+ #
41
+ # Permission is hereby granted, free of charge, to any person obtaining a
42
+ # copy of this software and associated documentation files (the "Software"),
43
+ # to deal in the Software without restriction, including without limitation
44
+ # the rights to use, copy, modify, merge, publish, distribute, sublicense,
45
+ # and/or sell copies of the Software, and to permit persons to whom the
46
+ # Software is furnished to do so, subject to the following conditions:
47
+ #
48
+ # The above copyright notice and this permission notice shall be included in
49
+ # all copies or substantial portions of the Software.
50
+ #++
51
+
52
+ module Ruby
53
+
54
+ extend self
55
+
56
+ CONFIG = ::RbConfig::CONFIG
57
+
58
+ # Store original $GEM_HOME value so that even if the app customizes
59
+ # $GEM_HOME we can still work with the original value.
60
+ if gem_home = ::ENV['GEM_HOME']
61
+ gem_home = gem_home.strip.freeze
62
+ gem_home = nil if gem_home.empty?
63
+ end
64
+
65
+ GEM_HOME = gem_home
66
+
67
+ RUBY_ENGINE = defined?(::RUBY_ENGINE) ? ::RUBY_ENGINE : 'ruby'
68
+
69
+ OSX_RUBY_RE = %r{\A/System/Library/Frameworks/Ruby.framework/Versions/.*?/usr/bin/ruby\Z}
70
+
71
+ # Returns correct command for invoking the current Ruby interpreter.
72
+ def ruby_command
73
+ defined?(@ruby_command) ? @ruby_command : @ruby_command = ruby_executable
74
+ end
75
+
76
+ attr_writer :ruby_command
77
+
78
+ # Returns the full path to the current Ruby interpreter's executable file.
79
+ # This might not be the actual correct command to use for invoking the Ruby
80
+ # interpreter; use ruby_command instead.
81
+ def ruby_executable
82
+ @ruby_executable ||= begin
83
+ dir, name, ext = CONFIG.values_at(*%w[bindir RUBY_INSTALL_NAME EXEEXT])
84
+ ::File.join(dir, name + ext).sub(/.*\s.*/m, '"\&"')
85
+ end
86
+ end
87
+
88
+ attr_writer :ruby_executable
89
+
90
+ # Locates a Ruby tool command +name+, e.g. 'gem', 'rake', 'bundle', etc. Instead
91
+ # of naively looking in $PATH, this function uses a variety of search heuristics
92
+ # to find the command that's really associated with the current Ruby interpreter.
93
+ # It should never locate a command that's actually associated with a different
94
+ # Ruby interpreter.
95
+ #
96
+ # NOTE: The return value may not be the actual correct invocation for the tool.
97
+ # Use command_for_ruby_tool for that.
98
+ #
99
+ # Returns +nil+ when nothing's found.
100
+ def locate_ruby_tool(name, extensions = ['', CONFIG['EXEEXT']].compact.uniq)
101
+ # Deduce Ruby's --program-prefix and --program-suffix from its install name
102
+ # and transform the given input name accordingly.
103
+ #
104
+ # "rake" => "jrake", "rake1.8", etc.
105
+ [name, CONFIG['RUBY_INSTALL_NAME'].sub('ruby', name)].uniq.each { |basename|
106
+ extensions.each { |ext|
107
+ result = locate_ruby_tool_by_basename(basename + ext) and return result
108
+ }
109
+ }
110
+
111
+ nil
112
+ end
113
+
114
+ # Returns the correct command string for invoking the +name+ executable
115
+ # that belongs to the current Ruby interpreter. Returns +nil+ if the
116
+ # command is not found.
117
+ #
118
+ # If the command executable is a Ruby program, then we need to run it
119
+ # in the correct Ruby interpreter just in case the command doesn't
120
+ # have the correct shebang line; we don't want a totally different
121
+ # Ruby than the current one to be invoked.
122
+ def command_for_ruby_tool(name)
123
+ filename = respond_to?(name) ? send(name) : locate_ruby_tool(name)
124
+ shebang_command(filename) =~ /ruby/ ? "#{ruby_command} #{filename}" : filename
125
+ end
126
+
127
+ def self.define_ruby_tool(name)
128
+ class_eval <<-EOT, __FILE__, __LINE__ + 1
129
+ def #{name}
130
+ @#{name} ||= locate_ruby_tool('#{name}')
131
+ end
132
+
133
+ attr_writer :#{name}
134
+
135
+ def #{name}_command
136
+ @#{name}_command ||= command_for_ruby_tool('#{name}')
137
+ end
138
+
139
+ attr_writer :#{name}_command
140
+ EOT
141
+ end
142
+
143
+ %w[gem rake rspec].each { |name| define_ruby_tool(name) }
144
+
145
+ def ruby_options_to_argv(args, ruby_command = ruby_command)
146
+ argv = [ruby_command]
147
+
148
+ ruby_options_from_hash(args.pop, argv) if args.last.is_a?(::Hash)
149
+
150
+ argv.concat(args.map! { |arg| arg.to_s.strip })
151
+ end
152
+
153
+ def ruby_options_from_hash(hash, argv = [])
154
+ hash.each { |key, val|
155
+ opt = "-#{key.to_s[0, 1]}"
156
+
157
+ if val.is_a?(::Array)
158
+ val.each { |v| argv << opt << v.to_s }
159
+ elsif opt == '-e'
160
+ argv << opt << val.to_s
161
+ elsif val != false
162
+ argv << "#{opt}#{val unless val == true}"
163
+ end
164
+ }
165
+
166
+ argv
167
+ end
168
+
169
+ private
170
+
171
+ def locate_ruby_tool_by_basename(name)
172
+ # On OS X we must look for Ruby binaries in /usr/bin.
173
+ # RubyGems puts executables (e.g. 'rake') in there, not in
174
+ # /System/Libraries/(...)/bin.
175
+ dir = ::RUBY_PLATFORM =~ /darwin/ && ruby_command =~ OSX_RUBY_RE ?
176
+ '/usr/bin' : ::File.dirname(ruby_command)
177
+
178
+ filename = executable_filename(dir, name) and return filename
179
+
180
+ # RubyGems might put binaries in a directory other
181
+ # than Ruby's bindir. Debian packaged RubyGems and
182
+ # DebGem packaged RubyGems are the prime examples.
183
+ filename = executable_filename(::Gem.bindir, name) and return filename
184
+
185
+ # Looks like it's not in the RubyGems bindir. Search in $PATH, but
186
+ # be very careful about this because whatever we find might belong
187
+ # to a different Ruby interpreter than the current one.
188
+ ::ENV['PATH'].split(::File::PATH_SEPARATOR).each { |path|
189
+ if filename = executable_filename(path, name)
190
+ return filename if shebang_command(filename) == ruby_command
191
+ end
192
+ }
193
+
194
+ nil
195
+ end
196
+
197
+ def executable_filename(dir, name)
198
+ filename = ::File.join(dir, name)
199
+ filename if ::File.file?(filename) && ::File.executable?(filename)
200
+ end
201
+
202
+ def shebang_command(filename)
203
+ ::File.foreach(filename) { |line|
204
+ return $1 if line =~ /\A#!\s*(\S*)/
205
+
206
+ # Allow one extra line for magic comment.
207
+ break if $. > 1
208
+ }
209
+
210
+ nil
211
+ end
212
+
213
+ end
214
+
215
+ end
216
+
217
+ def File.ruby; ::Nuggets::Ruby.ruby_command; end
218
+
219
+ begin
220
+ require 'open4'
221
+
222
+ def Process.ruby(*args, &block)
223
+ argv = ::Nuggets::Ruby.ruby_options_to_argv(args)
224
+ ::Open4.popen4(*argv, &block)
225
+ end
226
+
227
+ require 'nuggets/io/interact'
228
+
229
+ def Process.interact_ruby(input, *args)
230
+ ruby(*args) { |_, i, o, e|
231
+ ::IO.interact({ input => i }, { o => $stdout, e => $stderr })
232
+ }
233
+ end
234
+ rescue ::LoadError
235
+ end
@@ -0,0 +1,12 @@
1
+ # load statistics related nuggets
2
+
3
+ require 'nuggets/array/boost'
4
+ require 'nuggets/array/correlation'
5
+ require 'nuggets/array/histogram'
6
+ require 'nuggets/array/mean'
7
+ require 'nuggets/array/median'
8
+ require 'nuggets/array/mode'
9
+ require 'nuggets/array/regression'
10
+ require 'nuggets/array/standard_deviation'
11
+ require 'nuggets/array/variance'
12
+ require 'nuggets/range/quantile'
@@ -0,0 +1,12 @@
1
+ # load statistics related nuggets (mixins)
2
+
3
+ require 'nuggets/array/boost_mixin'
4
+ require 'nuggets/array/correlation_mixin'
5
+ require 'nuggets/array/histogram_mixin'
6
+ require 'nuggets/array/mean_mixin'
7
+ require 'nuggets/array/median_mixin'
8
+ require 'nuggets/array/mode_mixin'
9
+ require 'nuggets/array/regression_mixin'
10
+ require 'nuggets/array/standard_deviation_mixin'
11
+ require 'nuggets/array/variance_mixin'
12
+ require 'nuggets/range/quantile_mixin'
@@ -0,0 +1,5 @@
1
+ require 'nuggets/string/camelscore_mixin'
2
+
3
+ class String
4
+ include Nuggets::String::CamelscoreMixin
5
+ end
@@ -0,0 +1,116 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2012 Jens Wille #
7
+ # #
8
+ # Authors: #
9
+ # Jens Wille <jens.wille@gmail.com> #
10
+ # #
11
+ # nuggets is free software; you can redistribute it and/or modify it under #
12
+ # the terms of the GNU Affero General Public License as published by the Free #
13
+ # Software Foundation; either version 3 of the License, or (at your option) #
14
+ # any later version. #
15
+ # #
16
+ # nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
17
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
18
+ # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
19
+ # more details. #
20
+ # #
21
+ # You should have received a copy of the GNU Affero General Public License #
22
+ # along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
23
+ # #
24
+ ###############################################################################
25
+ #++
26
+
27
+ module Nuggets
28
+ class String
29
+ module CamelscoreMixin
30
+
31
+ # List of acronyms to treat specially
32
+ # in #camelcase and #underscore.
33
+ CAMELSCORE_ACRONYMS = {
34
+ 'html' => 'HTML',
35
+ 'rss' => 'RSS',
36
+ 'sql' => 'SQL',
37
+ 'ssl' => 'SSL',
38
+ 'xml' => 'XML'
39
+ }
40
+
41
+ # call-seq:
42
+ # str.camelcase => new_string
43
+ #
44
+ # Returns the CamelCase form of _str_.
45
+ def camelcase
46
+ dup.camelcase!
47
+ end
48
+
49
+ alias_method :camelize, :camelcase
50
+
51
+ # call-seq:
52
+ # str.camelcase! => str
53
+ #
54
+ # Replaces _str_ with its CamelCase form and returns _str_.
55
+ def camelcase!
56
+ sub!(/^[a-z]+/) {
57
+ CAMELSCORE_ACRONYMS[$&] || $&.capitalize
58
+ }
59
+
60
+ gsub!(/(?:_|([\/\d]))([a-z]+)/i) {
61
+ "#{$1}#{CAMELSCORE_ACRONYMS[$2] || $2.capitalize}"
62
+ }
63
+
64
+ gsub!('/', '::')
65
+
66
+ self
67
+ end
68
+
69
+ alias_method :camelize!, :camelcase!
70
+
71
+ # call-seq:
72
+ # str.underscore => new_string
73
+ #
74
+ # Returns the under_score form of _str_.
75
+ def underscore
76
+ dup.underscore!
77
+ end
78
+
79
+ # call-seq:
80
+ # str.underscore! => str
81
+ #
82
+ # Replaces _str_ with its under_score form and returns _str_.
83
+ def underscore!
84
+ gsub!(/::/, '/')
85
+
86
+ a = CAMELSCORE_ACRONYMS.values
87
+ r = a.empty? ? /(?=a)b/ : ::Regexp.union(*a.sort_by { |v| v.length })
88
+
89
+ gsub!(/(?:([A-Za-z])|(\d)|^)(#{r})(?=\b|[^a-z])/) {
90
+ "#{$1 || $2}#{'_' if $1}#{$3.downcase}"
91
+ }
92
+
93
+ gsub!(/([A-Z])(?=[A-Z])/, '\1_')
94
+ gsub!(/([a-z\d])([A-Z])/, '\1_\2')
95
+
96
+ downcase!
97
+
98
+ self
99
+ end
100
+
101
+ # call-seq:
102
+ # str.constantize(base = Object) => anObject
103
+ #
104
+ # Returns the constant pointed to by _str_, relative to +base+.
105
+ def constantize(base = ::Object)
106
+ names = split('::')
107
+ return if names.empty?
108
+
109
+ const = names.first.empty? ? (names.shift; ::Object) : base
110
+ names.each { |name| const = const.const_get(name) }
111
+ const
112
+ end
113
+
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,46 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2011 Jens Wille #
7
+ # #
8
+ # Authors: #
9
+ # Jens Wille <jens.wille@gmail.com> #
10
+ # #
11
+ # nuggets is free software; you can redistribute it and/or modify it under #
12
+ # the terms of the GNU Affero General Public License as published by the Free #
13
+ # Software Foundation; either version 3 of the License, or (at your option) #
14
+ # any later version. #
15
+ # #
16
+ # nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
17
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
18
+ # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
19
+ # more details. #
20
+ # #
21
+ # You should have received a copy of the GNU Affero General Public License #
22
+ # along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
23
+ # #
24
+ ###############################################################################
25
+ #++
26
+
27
+ class String
28
+
29
+ # call-seq:
30
+ # str.capitalize_first => new_string
31
+ #
32
+ # Capitalizes the first character in +str+, but without downcasing the rest
33
+ # like String#capitalize does.
34
+ def capitalize_first
35
+ empty? ? self : self[0..0].upcase << self[1..-1]
36
+ end
37
+
38
+ # call-seq:
39
+ # str.capitalize_first! => str
40
+ #
41
+ # Destructive version of #capitalize_first.
42
+ def capitalize_first!
43
+ replace(capitalize_first)
44
+ end
45
+
46
+ end