ruby-nuggets 0.9.8.1 → 0.9.9

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 (129) hide show
  1. checksums.yaml +4 -4
  2. data/README +1 -1
  3. data/Rakefile +9 -3
  4. data/lib/nuggets/all.rb +1 -1
  5. data/lib/nuggets/array/combination.rb +2 -25
  6. data/lib/nuggets/array/flatten_once.rb +1 -12
  7. data/lib/nuggets/array/format.rb +2 -60
  8. data/lib/nuggets/array/in_order.rb +1 -12
  9. data/lib/nuggets/array/monotone.rb +10 -27
  10. data/lib/nuggets/array/only.rb +1 -16
  11. data/lib/nuggets/array/shuffle.rb +1 -1
  12. data/lib/nuggets/array/to_hash.rb +1 -22
  13. data/lib/nuggets/cli.rb +2 -2
  14. data/lib/nuggets/content_type.rb +3 -8
  15. data/lib/nuggets/dotted_decimal.rb +0 -18
  16. data/lib/nuggets/enumerable/all_any_extended.rb +0 -27
  17. data/lib/nuggets/enumerable/minmax.rb +4 -25
  18. data/lib/nuggets/hash/at.rb +2 -20
  19. data/lib/nuggets/hash/deep_fetch.rb +5 -0
  20. data/lib/nuggets/hash/deep_fetch_mixin.rb +75 -0
  21. data/lib/nuggets/hash/in_order.rb +0 -8
  22. data/lib/nuggets/hash/insert.rb +2 -13
  23. data/lib/nuggets/hash/only.rb +2 -18
  24. data/lib/nuggets/i18n.rb +1 -10
  25. data/lib/nuggets/integer/factorial.rb +0 -17
  26. data/lib/nuggets/integer/to_binary_s.rb +0 -8
  27. data/lib/nuggets/lsi.rb +2 -2
  28. data/lib/nuggets/midos.rb +2 -2
  29. data/lib/nuggets/mysql.rb +6 -207
  30. data/lib/nuggets/numeric/duration.rb +0 -8
  31. data/lib/nuggets/numeric/limit.rb +1 -8
  32. data/lib/nuggets/numeric/signum.rb +0 -7
  33. data/lib/nuggets/numeric/to_multiple.rb +0 -6
  34. data/lib/nuggets/rdf/compression.rb +3 -89
  35. data/lib/nuggets/rdf/prefix.rb +6 -32
  36. data/lib/nuggets/rdf/turtle/reader.rb +5 -70
  37. data/lib/nuggets/rdf/turtle.rb +6 -122
  38. data/lib/nuggets/rdf/uri.rb +5 -37
  39. data/lib/nuggets/ruby.rb +39 -147
  40. data/lib/nuggets/string/capitalize_first.rb +2 -18
  41. data/lib/nuggets/string/case.rb +0 -22
  42. data/lib/nuggets/string/msub.rb +0 -18
  43. data/lib/nuggets/string/nsub.rb +2 -16
  44. data/lib/nuggets/string/sub_with_md.rb +6 -25
  45. data/lib/nuggets/string/word_wrap.rb +1 -35
  46. data/lib/nuggets/util/ansicolor2css.rb +1 -1
  47. data/lib/nuggets/util/cli.rb +1 -1
  48. data/lib/nuggets/util/content_type.rb +1 -1
  49. data/lib/nuggets/util/dotted_decimal.rb +1 -2
  50. data/lib/nuggets/util/i18n.rb +1 -1
  51. data/lib/nuggets/util/lazy_attr.rb +1 -1
  52. data/lib/nuggets/util/log_parser/apache.rb +1 -1
  53. data/lib/nuggets/util/log_parser/rails.rb +1 -1
  54. data/lib/nuggets/util/log_parser.rb +1 -1
  55. data/lib/nuggets/util/midos.rb +1 -1
  56. data/lib/nuggets/util/mysql.rb +1 -1
  57. data/lib/nuggets/util/pluggable.rb +1 -1
  58. data/lib/nuggets/util/ruby.rb +1 -1
  59. data/lib/nuggets/version.rb +2 -2
  60. data/spec/nuggets/array/boost_spec.rb +1 -3
  61. data/spec/nuggets/array/combination_spec.rb +25 -0
  62. data/spec/nuggets/array/correlation_spec.rb +1 -3
  63. data/spec/nuggets/array/flatten_once_spec.rb +16 -0
  64. data/spec/nuggets/array/flush_spec.rb +1 -3
  65. data/spec/nuggets/array/format_spec.rb +52 -0
  66. data/spec/nuggets/array/hashify_spec.rb +1 -3
  67. data/spec/nuggets/array/histogram_spec.rb +1 -3
  68. data/spec/nuggets/array/in_order_spec.rb +13 -0
  69. data/spec/nuggets/array/limit_spec.rb +1 -3
  70. data/spec/nuggets/array/mean_spec.rb +3 -5
  71. data/spec/nuggets/array/median_spec.rb +3 -5
  72. data/spec/nuggets/array/mode_spec.rb +1 -3
  73. data/spec/nuggets/array/monotone_spec.rb +30 -0
  74. data/spec/nuggets/array/only_spec.rb +26 -0
  75. data/spec/nuggets/array/regression_spec.rb +1 -3
  76. data/spec/nuggets/array/runiq_spec.rb +1 -3
  77. data/spec/nuggets/array/standard_deviation_spec.rb +1 -3
  78. data/spec/nuggets/array/to_hash_spec.rb +28 -0
  79. data/spec/nuggets/array/variance_spec.rb +3 -5
  80. data/spec/nuggets/dotted_decimal_spec.rb +27 -0
  81. data/spec/nuggets/enumerable/all_any_extended_spec.rb +31 -0
  82. data/spec/nuggets/enumerable/minmax_spec.rb +21 -0
  83. data/spec/nuggets/env/set_spec.rb +1 -3
  84. data/spec/nuggets/env/user_encoding_spec.rb +1 -3
  85. data/spec/nuggets/env/user_home_spec.rb +12 -10
  86. data/spec/nuggets/file/ext_spec.rb +1 -3
  87. data/spec/nuggets/file/replace_spec.rb +1 -3
  88. data/spec/nuggets/file/sub_spec.rb +1 -3
  89. data/spec/nuggets/file/which_spec.rb +1 -3
  90. data/spec/nuggets/hash/at_spec.rb +19 -0
  91. data/spec/nuggets/hash/deep_fetch_spec.rb +159 -0
  92. data/spec/nuggets/hash/deep_merge_spec.rb +1 -3
  93. data/spec/nuggets/hash/in_order_spec.rb +12 -0
  94. data/spec/nuggets/hash/insert_spec.rb +13 -0
  95. data/spec/nuggets/hash/nest_spec.rb +1 -3
  96. data/spec/nuggets/hash/only_spec.rb +29 -0
  97. data/spec/nuggets/hash/seen_spec.rb +4 -6
  98. data/spec/nuggets/hash/unroll_spec.rb +2 -4
  99. data/spec/nuggets/i18n_spec.rb +13 -0
  100. data/spec/nuggets/integer/factorial_spec.rb +10 -0
  101. data/spec/nuggets/integer/length_spec.rb +1 -3
  102. data/spec/nuggets/integer/map_spec.rb +1 -3
  103. data/spec/nuggets/integer/to_binary_s_spec.rb +19 -0
  104. data/spec/nuggets/numeric/duration_spec.rb +25 -0
  105. data/spec/nuggets/numeric/limit_spec.rb +16 -0
  106. data/spec/nuggets/numeric/signum_spec.rb +16 -0
  107. data/spec/nuggets/numeric/to_multiple_spec.rb +16 -0
  108. data/spec/nuggets/object/blank_spec.rb +1 -2
  109. data/spec/nuggets/object/boolean_spec.rb +1 -3
  110. data/spec/nuggets/object/msend_spec.rb +1 -3
  111. data/spec/nuggets/object/silence_spec.rb +7 -4
  112. data/spec/nuggets/object/singleton_class_spec.rb +11 -4
  113. data/spec/nuggets/proc/bind_spec.rb +1 -3
  114. data/spec/nuggets/range/quantile_spec.rb +1 -3
  115. data/spec/nuggets/string/camelscore_spec.rb +11 -13
  116. data/spec/nuggets/string/capitalize_first_spec.rb +13 -0
  117. data/spec/nuggets/string/case_spec.rb +31 -0
  118. data/spec/nuggets/string/evaluate_spec.rb +1 -3
  119. data/spec/nuggets/string/msub_spec.rb +20 -0
  120. data/spec/nuggets/string/nsub_spec.rb +13 -0
  121. data/spec/nuggets/string/sub_with_md_spec.rb +25 -0
  122. data/spec/nuggets/string/wc_spec.rb +1 -3
  123. data/spec/nuggets/string/word_wrap_spec.rb +81 -0
  124. data/spec/nuggets/string/xor_spec.rb +1 -3
  125. data/spec/nuggets/uri/content_type_spec.rb +32 -27
  126. data/spec/nuggets/uri/exist_spec.rb +33 -9
  127. data/spec/spec_helper.rb +17 -0
  128. metadata +34 -5
  129. data/.rspec +0 -1
@@ -1,38 +1,6 @@
1
- # encoding: utf-8
2
-
3
- #--
4
- ###############################################################################
5
- # #
6
- # A component of ruby-nuggets, some extensions to the Ruby programming #
7
- # language. #
8
- # #
9
- # Copyright (C) 2007-2014 Jens Wille #
10
- # #
11
- # Authors: #
12
- # Jens Wille <jens.wille@gmail.com> #
13
- # #
14
- # ruby-nuggets is free software; you can redistribute it and/or modify it #
15
- # under the terms of the GNU Affero General Public License as published by #
16
- # the Free Software Foundation; either version 3 of the License, or (at your #
17
- # option) any later version. #
18
- # #
19
- # ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
20
- # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
21
- # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
22
- # for more details. #
23
- # #
24
- # You should have received a copy of the GNU Affero General Public License #
25
- # along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
26
- # #
27
- ###############################################################################
28
- #++
29
-
30
- require 'rdf'
31
-
32
- class RDF::URI
33
-
34
- def basename
35
- File.basename(path).sub(/;[^\/]*$/, '')
36
- end
37
-
1
+ begin
2
+ require 'turtle_reader/rdf/uri'
3
+ warn "#{__FILE__}: 'nuggets/rdf/uri' is deprecated, use 'turtle_reader/rdf/uri' instead." unless ENV['NUGGETS_DEPRECATED_RDF_URI']
4
+ rescue LoadError => err
5
+ warn "#{__FILE__}: 'nuggets/rdf/uri' is no longer available; install `turtle_reader' instead. (#{err})" unless ENV['NUGGETS_DEPRECATED_RDF_URI']
38
6
  end
data/lib/nuggets/ruby.rb CHANGED
@@ -4,7 +4,7 @@
4
4
  # A component of ruby-nuggets, some extensions to the Ruby programming #
5
5
  # language. #
6
6
  # #
7
- # Copyright (C) 2007-2011 Jens Wille #
7
+ # Copyright (C) 2007-2014 Jens Wille #
8
8
  # #
9
9
  # Authors: #
10
10
  # Jens Wille <jens.wille@gmail.com> #
@@ -29,8 +29,9 @@ require 'rbconfig'
29
29
 
30
30
  module Nuggets
31
31
 
32
- # Heavily based on Phusion Passenger's PlatformInfo module; see
33
- # {their code}[https://github.com/FooBarWidget/passenger/blob/release-3.0.2/lib/phusion_passenger/platform_info/ruby.rb].
32
+ # Originally based on Phusion Passenger's
33
+ # {PlatformInfo}[https://github.com/FooBarWidget/passenger/blob/release-3.0.2/lib/phusion_passenger/platform_info/ruby.rb]
34
+ # module.
34
35
  #
35
36
  #--
36
37
  # Phusion Passenger - http://www.modrails.com/
@@ -68,33 +69,9 @@ module Nuggets
68
69
 
69
70
  OSX_RUBY_RE = %r{\A/System/Library/Frameworks/Ruby.framework/Versions/.*?/usr/bin/ruby\Z}
70
71
 
71
- UPDATE_RVM = %q{Please update RVM by running 'rvm update --head && rvm reload && rvm repair all'.} # :nodoc:
72
-
73
72
  # Returns correct command for invoking the current Ruby interpreter.
74
- # In case of RVM this function will return the path to the RVM wrapper script
75
- # that executes the current Ruby interpreter in the currently active gem set.
76
73
  def ruby_command
77
- return @ruby_command if defined?(@ruby_command)
78
-
79
- return @ruby_command = ruby_executable unless rvm?
80
-
81
- if name = rvm_ruby_string and dir = rvm_path
82
- if ::File.exist?(filename = ::File.join(dir, 'wrappers', name, 'ruby'))
83
- # Old wrapper scripts reference $HOME which causes
84
- # things to blow up when run by a different user.
85
- return @ruby_command = filename unless ::File.read(filename).include?('$HOME')
86
- end
87
-
88
- abort 'Your RVM wrapper scripts are too old. ' << UPDATE_RVM
89
- end
90
-
91
- # Something's wrong with the user's RVM installation.
92
- # Raise an error so that the user knows this instead of
93
- # having things fail randomly later on.
94
- # 'name' is guaranteed to be non-nil because rvm_ruby_string
95
- # already raises an exception on error.
96
- abort 'Your RVM installation appears to be broken: the RVM path cannot be found. ' <<
97
- 'Please fix your RVM installation or contact the RVM developers for support.'
74
+ defined?(@ruby_command) ? @ruby_command : @ruby_command = ruby_executable
98
75
  end
99
76
 
100
77
  attr_writer :ruby_command
@@ -111,88 +88,6 @@ module Nuggets
111
88
 
112
89
  attr_writer :ruby_executable
113
90
 
114
- # Returns whether the Ruby interpreter supports process forking.
115
- def ruby_supports_fork?
116
- # MRI >= 1.9.2's respond_to? returns false
117
- # for methods that are not implemented.
118
- ::Process.respond_to?(:fork) &&
119
- RUBY_ENGINE != 'jruby' &&
120
- RUBY_ENGINE != 'macruby' &&
121
- CONFIG['target_os'] !~ /mswin|windows|mingw/
122
- end
123
-
124
- # Returns whether the current Ruby interpreter is managed by RVM.
125
- def rvm?
126
- CONFIG['bindir'] =~ %r{/\.?rvm/}
127
- end
128
-
129
- # If the current Ruby interpreter is managed by RVM, returns the
130
- # directory in which RVM places its working files. Otherwise returns
131
- # +nil+.
132
- def rvm_path
133
- return @rvm_path if defined?(@rvm_path)
134
-
135
- return @rvm_path = nil unless rvm?
136
-
137
- [::ENV['rvm_path'], '~/.rvm', '/usr/local/rvm'].compact.each { |path|
138
- path = ::File.expand_path(path)
139
- return @rvm_path = path if ::File.directory?(path)
140
- }
141
-
142
- # Failure to locate the RVM path is probably caused by the
143
- # user customizing $rvm_path. Older RVM versions don't
144
- # export $rvm_path, making us unable to detect its value.
145
- abort 'Unable to locate the RVM path. Your RVM ' <<
146
- 'installation is probably too old. ' << UPDATE_RVM
147
- end
148
-
149
- attr_writer :rvm_path
150
-
151
- # If the current Ruby interpreter is managed by RVM, returns the
152
- # RVM name which identifies the current Ruby interpreter plus the
153
- # currently active gemset, e.g. something like this:
154
- # "ruby-1.9.2-p0@mygemset"
155
- #
156
- # Returns +nil+ otherwise.
157
- def rvm_ruby_string
158
- return @rvm_ruby_string if defined?(@rvm_ruby_string)
159
-
160
- return @rvm_ruby_string = nil unless rvm?
161
-
162
- # RVM used to export the necessary information through
163
- # environment variables, but doesn't always do that anymore
164
- # in the latest versions in order to fight env var pollution.
165
- # Scanning $LOAD_PATH seems to be the only way to obtain
166
- # the information.
167
-
168
- # Getting the RVM name of the Ruby interpreter ("ruby-1.9.2")
169
- # isn't so hard, we can extract it from the #ruby_executable
170
- # string. Getting the gemset name is a bit harder, so let's
171
- # try various strategies...
172
-
173
- # $GEM_HOME usually contains the gem set name.
174
- return @rvm_ruby_string = ::File.basename(GEM_HOME) if GEM_HOME && GEM_HOME.include?('rvm/gems/')
175
-
176
- # User somehow managed to nuke $GEM_HOME. Extract info from $LOAD_PATH.
177
- $LOAD_PATH.each { |path| return @rvm_ruby_string = $1 if path =~ %r{^.*rvm/gems/([^/]+)} }
178
-
179
- # On Ruby 1.9, $LOAD_PATH does not contain any gem paths until
180
- # at least one gem has been required so the above can fail.
181
- # We're out of options now, we can't detect the gem set.
182
- # Raise an exception so that the user knows what's going on
183
- # instead of having things fail in obscure ways later.
184
- abort 'Unable to autodetect the currently active RVM gem set ' <<
185
- "name. Please contact this program's author for support."
186
- end
187
-
188
- attr_writer :rvm_ruby_string
189
-
190
- # Returns either 'sudo' or 'rvmsudo' depending on whether the current
191
- # Ruby interpreter is managed by RVM.
192
- def ruby_sudo_command
193
- "#{'rvm' if rvm?}sudo"
194
- end
195
-
196
91
  # Locates a Ruby tool command +name+, e.g. 'gem', 'rake', 'bundle', etc. Instead
197
92
  # of naively looking in $PATH, this function uses a variety of search heuristics
198
93
  # to find the command that's really associated with the current Ruby interpreter.
@@ -203,17 +98,14 @@ module Nuggets
203
98
  # Use command_for_ruby_tool for that.
204
99
  #
205
100
  # Returns +nil+ when nothing's found.
206
- def locate_ruby_tool(name)
207
- extensions = ['', CONFIG['EXEEXT']].compact.uniq
208
-
101
+ def locate_ruby_tool(name, extensions = ['', CONFIG['EXEEXT']].compact.uniq)
209
102
  # Deduce Ruby's --program-prefix and --program-suffix from its install name
210
103
  # and transform the given input name accordingly.
211
104
  #
212
- # "rake" => "jrake", "rake1.8", etc
105
+ # "rake" => "jrake", "rake1.8", etc.
213
106
  [name, CONFIG['RUBY_INSTALL_NAME'].sub('ruby', name)].uniq.each { |basename|
214
107
  extensions.each { |ext|
215
- result = locate_ruby_tool_by_basename("#{basename}#{ext}")
216
- return result if result
108
+ result = locate_ruby_tool_by_basename(basename + ext) and return result
217
109
  }
218
110
  }
219
111
 
@@ -228,15 +120,12 @@ module Nuggets
228
120
  # in the correct Ruby interpreter just in case the command doesn't
229
121
  # have the correct shebang line; we don't want a totally different
230
122
  # Ruby than the current one to be invoked.
231
- #
232
- # If it's not a Ruby program then it's probably a wrapper
233
- # script as is the case with e.g. RVM (~/.rvm/wrappers).
234
123
  def command_for_ruby_tool(name)
235
124
  filename = respond_to?(name) ? send(name) : locate_ruby_tool(name)
236
125
  shebang_command(filename) =~ /ruby/ ? "#{ruby_command} #{filename}" : filename
237
126
  end
238
127
 
239
- %w[gem rake rspec].each { |name|
128
+ def self.define_ruby_tool(name)
240
129
  class_eval <<-EOT, __FILE__, __LINE__ + 1
241
130
  def #{name}
242
131
  @#{name} ||= locate_ruby_tool('#{name}')
@@ -250,59 +139,55 @@ module Nuggets
250
139
 
251
140
  attr_writer :#{name}_command
252
141
  EOT
253
- }
142
+ end
143
+
144
+ %w[gem rake rspec].each { |name| define_ruby_tool(name) }
254
145
 
255
146
  def ruby_options_to_argv(args, ruby_command = ruby_command)
256
147
  argv = [ruby_command]
257
148
 
258
- args.pop.each { |key, val|
149
+ ruby_options_from_hash(args.pop, argv) if args.last.is_a?(::Hash)
150
+
151
+ argv.concat(args.map! { |arg| arg.to_s.strip })
152
+ end
153
+
154
+ def ruby_options_from_hash(hash, argv = [])
155
+ hash.each { |key, val|
259
156
  opt = "-#{key.to_s[0, 1]}"
260
157
 
261
158
  if val.is_a?(::Array)
262
- val.each { |wal| argv << opt << wal.to_s }
159
+ val.each { |v| argv << opt << v.to_s }
263
160
  elsif opt == '-e'
264
161
  argv << opt << val.to_s
265
162
  elsif val != false
266
163
  argv << "#{opt}#{val unless val == true}"
267
164
  end
268
- } if args.last.is_a?(::Hash)
165
+ }
269
166
 
270
- argv.concat(args.map! { |arg| arg.to_s.strip })
167
+ argv
271
168
  end
272
169
 
273
170
  private
274
171
 
275
172
  def locate_ruby_tool_by_basename(name)
276
- dir = if ::RUBY_PLATFORM =~ /darwin/ && ruby_command =~ OSX_RUBY_RE
277
- # On OS X we must look for Ruby binaries in /usr/bin.
278
- # RubyGems puts executables (e.g. 'rake') in there, not in
279
- # /System/Libraries/(...)/bin.
280
- '/usr/bin'
281
- else
282
- ::File.dirname(ruby_command)
283
- end
173
+ # On OS X we must look for Ruby binaries in /usr/bin.
174
+ # RubyGems puts executables (e.g. 'rake') in there, not in
175
+ # /System/Libraries/(...)/bin.
176
+ dir = ::RUBY_PLATFORM =~ /darwin/ && ruby_command =~ OSX_RUBY_RE ?
177
+ '/usr/bin' : ::File.dirname(ruby_command)
284
178
 
285
- filename = ::File.join(dir, name)
286
- return filename if ::File.file?(filename) && ::File.executable?(filename)
179
+ filename = executable_filename(dir, name) and return filename
287
180
 
288
181
  # RubyGems might put binaries in a directory other
289
182
  # than Ruby's bindir. Debian packaged RubyGems and
290
183
  # DebGem packaged RubyGems are the prime examples.
291
- begin
292
- require 'rubygems' unless defined?(::Gem)
293
-
294
- filename = ::File.join(::Gem.bindir, name)
295
- return filename if ::File.file?(filename) && ::File.executable?(filename)
296
- rescue ::LoadError
297
- end
184
+ filename = executable_filename(::Gem.bindir, name) and return filename
298
185
 
299
186
  # Looks like it's not in the RubyGems bindir. Search in $PATH, but
300
187
  # be very careful about this because whatever we find might belong
301
188
  # to a different Ruby interpreter than the current one.
302
189
  ::ENV['PATH'].split(::File::PATH_SEPARATOR).each { |path|
303
- filename = ::File.join(path, name)
304
-
305
- if ::File.file?(filename) && ::File.executable?(filename)
190
+ if filename = executable_filename(path, name)
306
191
  return filename if shebang_command(filename) == ruby_command
307
192
  end
308
193
  }
@@ -310,6 +195,11 @@ module Nuggets
310
195
  nil
311
196
  end
312
197
 
198
+ def executable_filename(dir, name)
199
+ filename = ::File.join(dir, name)
200
+ filename if ::File.file?(filename) && ::File.executable?(filename)
201
+ end
202
+
313
203
  def shebang_command(filename)
314
204
  ::File.foreach(filename) { |line|
315
205
  return $1 if line =~ /\A#!\s*(\S*)/
@@ -317,6 +207,8 @@ module Nuggets
317
207
  # Allow one extra line for magic comment.
318
208
  break if $. > 1
319
209
  }
210
+
211
+ nil
320
212
  end
321
213
 
322
214
  end
@@ -328,9 +220,9 @@ def File.ruby; ::Nuggets::Ruby.ruby_command; end
328
220
  begin
329
221
  require 'open4'
330
222
 
331
- def Process.ruby(*args)
223
+ def Process.ruby(*args, &block)
332
224
  argv = ::Nuggets::Ruby.ruby_options_to_argv(args)
333
- ::Open4.popen4(*argv, &block_given? ? ::Proc.new : nil)
225
+ ::Open4.popen4(*argv, &block)
334
226
  end
335
227
 
336
228
  require 'nuggets/io/interact'
@@ -33,8 +33,7 @@ class String
33
33
  # Capitalizes the first character in +str+, but without downcasing the rest
34
34
  # like String#capitalize does.
35
35
  def capitalize_first
36
- return self if empty?
37
- self[0..0].upcase << self[1..-1]
36
+ empty? ? self : self[0..0].upcase << self[1..-1]
38
37
  end
39
38
 
40
39
  # call-seq:
@@ -42,22 +41,7 @@ class String
42
41
  #
43
42
  # Destructive version of #capitalize_first.
44
43
  def capitalize_first!
45
- replace capitalize_first
44
+ replace(capitalize_first)
46
45
  end
47
46
 
48
47
  end
49
-
50
- if $0 == __FILE__
51
- s = 'Some string'
52
- p s
53
- p s.capitalize_first
54
-
55
- s = 'some string'
56
- p s
57
- p s.capitalize_first
58
-
59
- s = 'SOME STRING'
60
- p s
61
- p s.capitalize
62
- p s.capitalize_first
63
- end
@@ -80,25 +80,3 @@ class String
80
80
  end
81
81
 
82
82
  end
83
-
84
- if $0 == __FILE__
85
- s = 'Some string'
86
- puts s
87
- p s.case
88
- puts s.downcase?
89
- puts s.upcase?
90
- puts s.mixed_case?
91
- puts s.capitalized?
92
-
93
- s = 'some string'
94
- puts s
95
- p s.case
96
- puts s.downcase?
97
- puts s.mixed_case?
98
-
99
- s = 'SOME STRING'
100
- puts s
101
- p s.case
102
- puts s.upcase?
103
- puts s.mixed_case?
104
- end
@@ -83,21 +83,3 @@ class String
83
83
  end
84
84
 
85
85
  end
86
-
87
- if $0 == __FILE__
88
- s = 'Foo, Bar - Baz'
89
- p s
90
-
91
- p s.gsub(/a/, 'o').gsub(/o/, 'a')
92
- p s.msub('a' => 'o', 'o' => 'a')
93
-
94
- s.msub!('a' => 'o', 'o' => 'a')
95
- p s
96
-
97
- t = '!!!'
98
- begin; p s.msub('r' => '???', 'z' => '#{t}'); rescue ::NameError => err; warn err; end
99
- p s.msub('r' => '???', 'z' => '#{t}', :__binding__ => binding)
100
-
101
- p s.msub(/[A-Z]/ => '#{__match__.downcase}', :__binding__ => binding)
102
- p s.msub(/[A-Z]/ => lambda { |match| match.downcase })
103
- end
@@ -33,11 +33,8 @@ class String
33
33
  #
34
34
  # Returns a copy of _str_ with the _first_ +count+ occurrences of pattern
35
35
  # replaced with either +replacement+ or the value of the block.
36
- def nsub(*args)
37
- _dup = dup
38
- (block_given? ?
39
- _dup.nsub!(*args) { |*a| yield(*a) } :
40
- _dup.nsub!(*args)) || _dup
36
+ def nsub(*args, &block)
37
+ (_dup = dup).nsub!(*args, &block) || _dup
41
38
  end
42
39
 
43
40
  # call-seq:
@@ -67,14 +64,3 @@ class String
67
64
  end
68
65
 
69
66
  end
70
-
71
- if $0 == __FILE__
72
- s = 'a b c d e f g h i'
73
- puts s
74
-
75
- puts s.nsub(' ', '', 6)
76
- puts s.nsub(' ', 6) { '' }
77
-
78
- s.nsub!(' ', '', 6)
79
- puts s
80
- end
@@ -37,10 +37,10 @@ class String
37
37
  #
38
38
  # Just like #sub, but passes the MatchData object instead of the current
39
39
  # match string to the block.
40
- def sub_with_md(pattern, replacement = nil)
40
+ def sub_with_md(pattern, replacement = nil, &block)
41
41
  replacement ?
42
42
  sub_without_md(pattern, replacement) :
43
- (_dup = dup).sub_with_md!(pattern) { |*a| yield(*a) } || _dup
43
+ (_dup = dup).sub_with_md!(pattern, &block) || _dup
44
44
  end
45
45
 
46
46
  # call-seq:
@@ -58,10 +58,10 @@ class String
58
58
  #
59
59
  # Just like #gsub, but passes the MatchData object instead of the current
60
60
  # match string to the block.
61
- def gsub_with_md(pattern, replacement = nil)
61
+ def gsub_with_md(pattern, replacement = nil, &block)
62
62
  replacement ?
63
63
  gsub_without_md(pattern, replacement) :
64
- (_dup = dup).gsub_with_md!(pattern) { |*a| yield(*a) } || _dup
64
+ (_dup = dup).gsub_with_md!(pattern, &block) || _dup
65
65
  end
66
66
 
67
67
  # call-seq:
@@ -89,43 +89,24 @@ class String
89
89
  end
90
90
 
91
91
  if $0 == __FILE__
92
- # stupid example, but it proves the point ;-)
93
92
  s = 'Foo, Bar - Baz'
94
93
  p s
95
94
 
96
- p s.gsub(/\w(\w+)(\W*)/) { |m|
97
- #p m
98
- "#{$1.gsub(/[ao]/, 'X')}#{$2}"
99
- }
100
-
101
- p s.gsub_with_md(/\w(\w+)(\W*)/) { |md|
102
- #p md[0]
103
- "#{md[1].gsub(/[ao]/, 'X')}#{md[2]}"
104
- }
105
-
106
- p s.gsub_with_md(/\w(\w+)(\W*)/) { |md|
107
- #p md[0]
108
- "#{md[1].gsub_with_md(/[ao]/) { |md2| md2[0].upcase }}#{md[2]}"
109
- }
110
-
111
- ::String.gimme_match_data!
95
+ String.gimme_match_data!
112
96
 
113
97
  p s.gsub(/\w(\w+)(\W*)/) { |m|
114
- #p m
115
98
  begin
116
99
  "#{$1.gsub(/[ao]/, 'X')}#{$2}"
117
- rescue ::NoMethodError => err
100
+ rescue NoMethodError => err
118
101
  warn err
119
102
  end
120
103
  }
121
104
 
122
105
  p s.gsub(/\w(\w+)(\W*)/) { |md|
123
- #p md[0]
124
106
  "#{md[1].gsub(/[ao]/, 'X')}#{md[2]}"
125
107
  }
126
108
 
127
109
  p s.gsub(/\w(\w+)(\W*)/) { |md|
128
- #p md[0]
129
110
  "#{md[1].gsub(/[ao]/) { |md2| md2[0].upcase }}#{md[2]}"
130
111
  }
131
112
  end
@@ -52,7 +52,7 @@ class String
52
52
  #
53
53
  # As with #word_wrap, but modifies the string in place.
54
54
  def word_wrap!(line_width = 80)
55
- replace word_wrap(line_width)
55
+ replace(word_wrap(line_width))
56
56
  end
57
57
 
58
58
  # call-seq:
@@ -75,37 +75,3 @@ class String
75
75
  end
76
76
 
77
77
  end
78
-
79
- if $0 == __FILE__
80
- s = <<EOT
81
- Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nullam nulla arcu,
82
- ullamcorper non, vulputate eget, elementum quis, sapien. Quisque consequat
83
- porta enim. Phasellus porta libero et turpis. Ut felis.
84
-
85
- Phasellus eget est a enim rutrum accumsan. Integer nec purus. Maecenas
86
- facilisis urna sed arcu. Suspendisse potenti.
87
-
88
-
89
- Vestibulum lacus metus, faucibus sit amet, mattis non, mollis sed, pede. Aenean
90
- vitae sem nec sem euismod sollicitudin. Cras rhoncus.
91
-
92
-
93
-
94
- Phasellus condimentum, ante a cursus dictum, lectus ipsum convallis magna, sed
95
- tincidunt massa eros vitae ante. Suspendisse nec sem.
96
- In hac habitasse platea dictumst. Fusce purus leo, ullamcorper sit amet, luctus
97
- in, mollis mollis, enim. In adipiscing erat.
98
- EOT
99
- puts s
100
-
101
- puts '=' * 80
102
-
103
- puts s.word_wrap(60)
104
- puts '=' * 80
105
- puts s.word_wrap(79)
106
-
107
- puts '=' * 80
108
-
109
- s.word_wrap!(60)
110
- puts s
111
- end
@@ -1,4 +1,4 @@
1
1
  require 'nuggets/ansicolor2css'
2
2
  module Util; ANSIColor2CSS = ::Nuggets::ANSIColor2CSS; end
3
3
 
4
- warn "#{__FILE__}: Util::ANSIColor2CSS is deprecated, use Nuggets::ANSIColor2CSS instead."
4
+ warn "#{__FILE__}: Util::ANSIColor2CSS is deprecated, use Nuggets::ANSIColor2CSS instead." unless ENV['NUGGETS_DEPRECATED_UTIL_ANSICOLOR2CSS']
@@ -1,4 +1,4 @@
1
1
  require 'nuggets/cli'
2
2
  module Util; CLI = ::Nuggets::CLI; end
3
3
 
4
- warn "#{__FILE__}: Util::CLI is deprecated, use Nuggets::CLI instead."
4
+ warn "#{__FILE__}: Util::CLI is deprecated, use Nuggets::CLI instead." unless ENV['NUGGETS_DEPRECATED_UTIL_CLI']
@@ -1,4 +1,4 @@
1
1
  require 'nuggets/content_type'
2
2
  module Util; ContentType = ::Nuggets::ContentType; end
3
3
 
4
- warn "#{__FILE__}: Util::ContentType is deprecated, use Nuggets::ContentType instead."
4
+ warn "#{__FILE__}: Util::ContentType is deprecated, use Nuggets::ContentType instead." unless ENV['NUGGETS_DEPRECATED_UTIL_CONTENT_TYPE']
@@ -1,4 +1,3 @@
1
1
  require 'nuggets/dotted_decimal'
2
- module Util; DottedDecimal = ::Nuggets::DottedDecimal; end
3
2
 
4
- warn "#{__FILE__}: Util::DottedDecimal is deprecated, use Nuggets::DottedDecimal instead."
3
+ warn "#{__FILE__}: 'nuggets/util/dotted_decimal' is deprecated, use 'nuggets/dotted_decimal' instead." unless ENV['NUGGETS_DEPRECATED_DOTTED_DECIMAL']
@@ -1,4 +1,4 @@
1
1
  require 'nuggets/i18n'
2
2
  module Util; I18n = ::Nuggets::I18n; end
3
3
 
4
- warn "#{__FILE__}: Util::I18n is deprecated, use Nuggets::I18n instead."
4
+ warn "#{__FILE__}: Util::I18n is deprecated, use Nuggets::I18n instead." unless ENV['NUGGETS_DEPRECATED_UTIL_I18N']
@@ -1,4 +1,4 @@
1
1
  require 'nuggets/lazy_attr'
2
2
  module Util; LazyAttr = ::Nuggets::LazyAttr; end
3
3
 
4
- warn "#{__FILE__}: Util::LazyAttr is deprecated, use Nuggets::LazyAttr instead."
4
+ warn "#{__FILE__}: Util::LazyAttr is deprecated, use Nuggets::LazyAttr instead." unless ENV['NUGGETS_DEPRECATED_UTIL_LAZY_ATTR']
@@ -1,4 +1,4 @@
1
1
  require 'nuggets/log_parser/apache'
2
2
  module Util; LogParser = ::Nuggets::LogParser; end
3
3
 
4
- warn "#{__FILE__}: Util::LogParser::Apache is deprecated, use Nuggets::LogParser::Apache instead."
4
+ warn "#{__FILE__}: Util::LogParser::Apache is deprecated, use Nuggets::LogParser::Apache instead." unless ENV['NUGGETS_DEPRECATED_UTIL_LOG_PARSER_APACHE']
@@ -1,4 +1,4 @@
1
1
  require 'nuggets/log_parser/rails'
2
2
  module Util; LogParser = ::Nuggets::LogParser; end
3
3
 
4
- warn "#{__FILE__}: Util::LogParser::Rails is deprecated, use Nuggets::LogParser::Rails instead."
4
+ warn "#{__FILE__}: Util::LogParser::Rails is deprecated, use Nuggets::LogParser::Rails instead." unless ENV['NUGGETS_DEPRECATED_UTIL_LOG_PARSER_RAILS']
@@ -1,4 +1,4 @@
1
1
  require 'nuggets/log_parser'
2
2
  module Util; LogParser = ::Nuggets::LogParser; end
3
3
 
4
- warn "#{__FILE__}: Util::LogParser is deprecated, use Nuggets::LogParser instead."
4
+ warn "#{__FILE__}: Util::LogParser is deprecated, use Nuggets::LogParser instead." unless ENV['NUGGETS_DEPRECATED_UTIL_LOG_PARSER']
@@ -1,4 +1,4 @@
1
1
  require 'nuggets/midos'
2
2
  module Util; Midos = ::Nuggets::Midos; end
3
3
 
4
- warn "#{__FILE__}: Util::Midos is deprecated, use Nuggets::Midos instead."
4
+ warn "#{__FILE__}: Util::Midos is deprecated, use Nuggets::Midos instead." unless ENV['NUGGETS_DEPRECATED_UTIL_MIDOS']
@@ -1,4 +1,4 @@
1
1
  require 'nuggets/mysql'
2
2
  module Util; MySQL = ::Nuggets::MySQL; end
3
3
 
4
- warn "#{__FILE__}: Util::MySQL is deprecated, use Nuggets::MySQL instead."
4
+ warn "#{__FILE__}: Util::MySQL is deprecated, use Nuggets::MySQL instead." unless ENV['NUGGETS_DEPRECATED_UTIL_MYSQL']
@@ -1,4 +1,4 @@
1
1
  require 'nuggets/pluggable'
2
2
  module Util; Pluggable = ::Nuggets::Pluggable; end
3
3
 
4
- warn "#{__FILE__}: Util::Pluggable is deprecated, use Nuggets::Pluggable instead."
4
+ warn "#{__FILE__}: Util::Pluggable is deprecated, use Nuggets::Pluggable instead." unless ENV['NUGGETS_DEPRECATED_UTIL_PLUGGABLE']
@@ -1,4 +1,4 @@
1
1
  require 'nuggets/ruby'
2
2
  module Util; Ruby = ::Nuggets::Ruby; end
3
3
 
4
- warn "#{__FILE__}: Util::Ruby is deprecated, use Nuggets::Ruby instead."
4
+ warn "#{__FILE__}: Util::Ruby is deprecated, use Nuggets::Ruby instead." unless ENV['NUGGETS_DEPRECATED_UTIL_RUBY']