ruby-nuggets 0.3.6.294 → 0.4.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 (41) hide show
  1. data/README +30 -2
  2. data/lib/nuggets/all.rb +6 -2
  3. data/lib/nuggets/array/format.rb +1 -1
  4. data/lib/nuggets/array/monotone.rb +4 -4
  5. data/lib/nuggets/array/only.rb +1 -1
  6. data/lib/nuggets/array/shuffle.rb +2 -2
  7. data/lib/nuggets/array/to_hash.rb +12 -40
  8. data/lib/nuggets/enumerable/agrep.rb +14 -4
  9. data/lib/nuggets/enumerable/all_any_extended.rb +12 -8
  10. data/lib/nuggets/enumerable/minmax.rb +6 -4
  11. data/lib/nuggets/file/which.rb +1 -1
  12. data/lib/nuggets/hash/at.rb +1 -1
  13. data/lib/nuggets/hash/in_order.rb +1 -1
  14. data/lib/nuggets/hash/insert.rb +2 -2
  15. data/lib/nuggets/hash/only.rb +3 -3
  16. data/lib/nuggets/integer/factorial.rb +2 -2
  17. data/lib/nuggets/io/agrep.rb +1 -1
  18. data/lib/nuggets/io/modes.rb +25 -15
  19. data/lib/nuggets/numeric/between.rb +1 -1
  20. data/lib/nuggets/object/blank.rb +14 -7
  21. data/lib/nuggets/object/boolean.rb +69 -0
  22. data/lib/nuggets/object/eigenclass.rb +1 -1
  23. data/lib/nuggets/object/ghost_class.rb +1 -1
  24. data/lib/nuggets/object/metaclass.rb +1 -1
  25. data/lib/nuggets/object/singleton_class.rb +5 -3
  26. data/lib/nuggets/object/uniclass.rb +1 -1
  27. data/lib/nuggets/object/virtual_class.rb +1 -1
  28. data/lib/nuggets/proc/bind.rb +1 -3
  29. data/lib/nuggets/string/msub.rb +3 -3
  30. data/lib/nuggets/string/nsub.rb +14 -16
  31. data/lib/nuggets/string/sub_with_md.rb +16 -12
  32. data/lib/nuggets/string/word_wrap.rb +1 -1
  33. data/lib/nuggets/{tempfile.rb → tempfile/open.rb} +0 -17
  34. data/lib/nuggets/util/added_methods/init.rb +1 -1
  35. data/lib/nuggets/util/added_methods.rb +1 -1
  36. data/lib/nuggets/util/content_type.rb +14 -3
  37. data/lib/nuggets/util/dotted_decimal.rb +1 -1
  38. data/lib/nuggets/util/i18n.rb +1 -1
  39. data/lib/nuggets/version.rb +2 -2
  40. data/lib/nuggets.rb +16 -2
  41. metadata +55 -54
data/README CHANGED
@@ -2,12 +2,40 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to ruby-nuggets version 0.3.6
5
+ This documentation refers to ruby-nuggets version 0.4.0
6
6
 
7
7
 
8
8
  == DESCRIPTION
9
9
 
10
- TODO: well, the description... ;-)
10
+ ruby-nuggets provides a collection of extensions to Ruby core classes. In
11
+ its effort, it's similar to projects like Facets[http://facets.rubyforge.org]
12
+ and Extensions[http://extensions.rubyforge.org] (or even
13
+ Labrador[http://rubyforge.org/projects/labrador/]), though not as ambitious ;-)
14
+ This is simply where I put stuff that I need on a more or less regular basis
15
+ or that struck me as interesting while surfing the web, reading books, or
16
+ following discussions on ruby-talk.
17
+
18
+ There are several ways to get at the nuggets you're interested in:
19
+
20
+ # The usual way: Just require() what you need
21
+ require 'nuggets/object/singleton_class'
22
+
23
+ # The more fancy way ;-) (see the documentation of Nuggets() for more examples)
24
+ require 'nuggets'
25
+ Nuggets(:object => 'singleton_class', :string => %w[nsub sub_with_md])
26
+
27
+ # Finally, get everything there is...
28
+ require 'nuggets/all'
29
+
30
+ Please be aware that some of the things herein might not work as expected or
31
+ cause other libraries to misbehave. Use at your own risk!
32
+
33
+
34
+ == LINKS
35
+
36
+ * <http://prometheus.rubyforge.org/ruby-nuggets>
37
+ * <http://prometheus.rubyforge.org/svn/scratch/ruby-nuggets>
38
+ * <http://github.com/blackwinter/ruby-nuggets>
11
39
 
12
40
 
13
41
  == AUTHORS
data/lib/nuggets/all.rb CHANGED
@@ -25,6 +25,10 @@
25
25
  ###############################################################################
26
26
  #++
27
27
 
28
- Dir[File.dirname(__FILE__) + '/*/**/*.rb'].sort.each { |rb|
29
- require rb
28
+ base = File.dirname(__FILE__)
29
+ base_re = Regexp.escape(base)
30
+
31
+ Dir[File.join(base, %w[* ** *.rb])].sort.each { |path|
32
+ ext_re = Regexp.escape(File.extname(path))
33
+ require path.sub(/#{base_re}(.*)#{ext_re}/, 'nuggets\1')
30
34
  }
@@ -25,7 +25,7 @@
25
25
  ###############################################################################
26
26
  #++
27
27
 
28
- require File.join(File.dirname(__FILE__), 'combination')
28
+ require 'nuggets/array/combination'
29
29
 
30
30
  class Array
31
31
 
@@ -28,12 +28,12 @@
28
28
  class Array
29
29
 
30
30
  # call-seq:
31
- # array.monotone?(op) => true or false
31
+ # array.monotone?(operator) => true or false
32
32
  #
33
- # Check whether _array_ is monotone according to operator +op+.
34
- def monotone?(op)
33
+ # Check whether _array_ is monotone according to +operator+.
34
+ def monotone?(operator)
35
35
  inject { |a, b|
36
- return false unless a.send(op, b)
36
+ return false unless a.send(operator, b)
37
37
  b
38
38
  }
39
39
 
@@ -28,7 +28,7 @@
28
28
  class Array
29
29
 
30
30
  # call-seq:
31
- # array.only([relax]) => anObject
31
+ # array.only(relax = true or false) => anObject
32
32
  #
33
33
  # Returns the only element of _array_. Raises an IndexError if _array_'s
34
34
  # size is not 1, unless +relax+ is true.
@@ -101,8 +101,8 @@ if $0 == __FILE__
101
101
  a.shuffle!
102
102
  p a
103
103
 
104
- require File.join(File.dirname(__FILE__), '..', 'integer', 'factorial')
105
- require File.join(File.dirname(__FILE__), '..', 'enumerable', 'minmax')
104
+ require 'nuggets/integer/factorial'
105
+ require 'nuggets/enumerable/minmax'
106
106
 
107
107
  a = %w[a b c]
108
108
  n = 100_000
@@ -25,7 +25,7 @@
25
25
  ###############################################################################
26
26
  #++
27
27
 
28
- require File.join(File.dirname(__FILE__), 'flatten_once')
28
+ require 'nuggets/array/flatten_once'
29
29
 
30
30
  class Array
31
31
 
@@ -39,55 +39,27 @@ class Array
39
39
  # <tt>hash.to_a.to_h == hash</tt>). Otherwise, maps each element of
40
40
  # _array_ to +value+ or the result of the block.
41
41
  #
42
- # NOTE: This is the "nice" version. For a more speed-optimized one,
43
- # use #to_hash_opt.
44
- #
45
42
  # Examples:
46
43
  # [[0, 0], [1, [2, 3]]].to_h #=> { 0 => 0, 1 => [2, 3] }
47
44
  # %w[a b c d].to_h #=> { "a" => "b", "c" => "d" }
48
45
  # %w[a b c d].to_h(1) #=> { "a" => 1, "b" => 1, "c" => 1, "d" => 1 }
49
46
  # %w[a b].to_h { |e| e * 2 } #=> { "a" => "aa", "b" => "bb" }
50
- def to_hash(value = default = Object.new, &block)
51
- if block ||= value != default && lambda { value }
52
- inject({}) { |hash, element|
53
- hash.update(element => block[element])
54
- }
55
- else
56
- Hash[*flatten_once]
57
- end
58
- end
59
- alias_method :to_h, :to_hash
47
+ def to_hash(value = default = Object.new)
48
+ hash = {}
60
49
 
61
- # call-seq:
62
- # array.to_hash_opt => aHash
63
- # array.to_hash_opt(value) => aHash
64
- # array.to_hash_opt { |element| ... } => aHash
65
- #
66
- # Same as #to_hash, but slightly optimized for speed. To use this one instead
67
- # of #to_hash: <tt>Array.send(:alias_method, :to_h, :to_hash_opt)</tt>.
68
- #
69
- # Benchmark (array = (1..20).to_a, N = 100_000):
70
- # user system total real
71
- # to_hash: plain 4.820000 0.560000 5.380000 ( 5.600390)
72
- # to_hash: value 12.910000 0.930000 13.840000 ( 13.938352)
73
- # to_hash: block 13.590000 1.180000 14.770000 ( 14.810804)
74
- # to_hash_opt: plain 4.910000 0.470000 5.380000 ( 5.416949)
75
- # to_hash_opt: value 2.170000 0.390000 2.560000 ( 2.609034)
76
- # to_hash_opt: block 7.090000 0.880000 7.970000 ( 8.109180)
77
- def to_hash_opt(value = default = Object.new, &block)
78
- if block
79
- hash = {}
80
- each { |element| hash[element] = block[element] }
81
- hash
82
- elsif value != default
83
- hash = {}
50
+ if block_given?
51
+ raise ArgumentError, "both block and value argument given" if default.nil?
52
+
53
+ each { |element| hash[element] = yield element }
54
+ elsif default.nil?
84
55
  each { |element| hash[element] = value }
85
- hash
86
56
  else
87
- Hash[*flatten_once]
57
+ return Hash[*flatten_once]
88
58
  end
59
+
60
+ hash
89
61
  end
90
- #alias_method :to_h, :to_hash_opt
62
+ alias_method :to_h, :to_hash
91
63
 
92
64
  end
93
65
 
@@ -25,8 +25,16 @@
25
25
  ###############################################################################
26
26
  #++
27
27
 
28
- require 'rubygems'
29
- require 'amatch'
28
+ begin
29
+ require 'rubygems'
30
+ rescue LoadError
31
+ end
32
+
33
+ begin
34
+ require 'amatch'
35
+ rescue LoadError
36
+ warn "Couldn't load amatch..." if $VERBOSE
37
+ end
30
38
 
31
39
  module Enumerable
32
40
 
@@ -46,13 +54,15 @@ module Enumerable
46
54
  # - Only works with string elements in _enum_. (Calls +to_s+ on each element)
47
55
  # - The cost for individual error types (substitution, insertion, deletion)
48
56
  # cannot be adjusted.
49
- def agrep(pattern, distance = 0, &block)
57
+ def agrep(pattern, distance = 0)
58
+ raise 'Amatch not available!' unless defined?(Amatch)
59
+
50
60
  pattern = pattern.source if pattern.is_a?(Regexp)
51
61
 
52
62
  amatch = Amatch::Levenshtein.new(pattern)
53
63
  matches = select { |obj| amatch.search(obj.to_s) <= distance }
54
64
 
55
- block ? matches.map(&block) : matches
65
+ block_given? ? matches.map { |match| yield match } : matches
56
66
  end
57
67
 
58
68
  end
@@ -37,8 +37,10 @@ module Enumerable
37
37
  # Adds the ability to pass an +object+ instead of a block, which will then
38
38
  # be tested against each item in _enum_ according to +operator+, defaulting
39
39
  # to :===.
40
- def all?(object = default = Object.new, operator = :===, &block)
41
- _nuggets_original_all?(&_block_for_all_any_extended(object, default, operator, block))
40
+ def all?(object = default = Object.new, operator = :===)
41
+ _nuggets_original_all?(&block_given? ?
42
+ _block_for_all_any_extended(object, default, operator) { |*a| yield(*a) } :
43
+ _block_for_all_any_extended(object, default, operator))
42
44
  end
43
45
 
44
46
  # call-seq:
@@ -48,20 +50,22 @@ module Enumerable
48
50
  # Adds the ability to pass an +object+ instead of a block, which will then
49
51
  # be tested against each item in _enum_ according to +operator+, defaulting
50
52
  # to :===.
51
- def any?(object = default = Object.new, operator = :===, &block)
52
- _nuggets_original_any?(&_block_for_all_any_extended(object, default, operator, block))
53
+ def any?(object = default = Object.new, operator = :===)
54
+ _nuggets_original_any?(&block_given? ?
55
+ _block_for_all_any_extended(object, default, operator) { |*a| yield(*a) } :
56
+ _block_for_all_any_extended(object, default, operator))
53
57
  end
54
58
 
55
59
  private
56
60
 
57
61
  # Common argument processing for extended versions of #all? and #any?.
58
- def _block_for_all_any_extended(object, default, operator, block)
62
+ def _block_for_all_any_extended(object, default, operator)
59
63
  if default.nil?
60
- raise ArgumentError, "both block and object argument given", caller(1) if block
64
+ raise ArgumentError, "both block and object argument given", caller(1) if block_given?
61
65
 
62
66
  lambda { |item| object.send(operator, item) }
63
- else
64
- block
67
+ elsif block_given?
68
+ lambda { |item| yield item }
65
69
  end
66
70
  end
67
71
 
@@ -82,8 +82,9 @@ module Enumerable
82
82
  #
83
83
  # Maximum #minmax. If +what+ is omitted, or nil, the original Enumerable#max
84
84
  # is called.
85
- def max(what = nil, &block)
86
- what ? minmax(:max, what) : _nuggets_original_max(&block)
85
+ def max(what = nil)
86
+ what ? minmax(:max, what) : block_given? ?
87
+ _nuggets_original_max { |*a| yield(*a) } : _nuggets_original_max
87
88
  end
88
89
 
89
90
  # call-seq:
@@ -91,8 +92,9 @@ module Enumerable
91
92
  #
92
93
  # Minimum #minmax. If +what+ is omitted, or nil, the original Enumerable#min
93
94
  # is called.
94
- def min(what = nil, &block)
95
- what ? minmax(:min, what) : _nuggets_original_min(&block)
95
+ def min(what = nil)
96
+ what ? minmax(:min, what) : block_given? ?
97
+ _nuggets_original_min { |*a| yield(*a) } : _nuggets_original_min
96
98
  end
97
99
 
98
100
  end
@@ -44,7 +44,7 @@ class File
44
44
  }
45
45
  end
46
46
 
47
- return nil
47
+ nil
48
48
  end
49
49
 
50
50
  # call-seq:
@@ -25,7 +25,7 @@
25
25
  ###############################################################################
26
26
  #++
27
27
 
28
- require File.join(File.dirname(__FILE__), '..', 'array', 'rand')
28
+ require 'nuggets/array/rand'
29
29
 
30
30
  class Hash
31
31
 
@@ -25,7 +25,7 @@
25
25
  ###############################################################################
26
26
  #++
27
27
 
28
- require File.join(File.dirname(__FILE__), '..', 'array', 'in_order')
28
+ require 'nuggets/array/in_order'
29
29
 
30
30
  class Hash
31
31
 
@@ -47,8 +47,8 @@ class Hash
47
47
  # hash.insert!(other) { |key, old_value, new_value| ... } => hash
48
48
  #
49
49
  # Destructive version of #insert.
50
- def insert!(other, &block)
51
- replace insert(other, &block)
50
+ def insert!(other)
51
+ replace block_given? ? insert(other) { |*a| yield(*a) } : insert(other)
52
52
  end
53
53
 
54
54
  end
@@ -25,12 +25,12 @@
25
25
  ###############################################################################
26
26
  #++
27
27
 
28
- require File.join(File.dirname(__FILE__), 'at')
28
+ require 'nuggets/hash/at'
29
29
 
30
30
  class Hash
31
31
 
32
32
  # call-seq:
33
- # hash.only([relax]) => aHash
33
+ # hash.only(relax = true or false) => aHash
34
34
  #
35
35
  # Returns the only key/value pair of _hash_. Raises an IndexError if _hash_'s
36
36
  # size is not 1, unless +relax+ is true.
@@ -42,7 +42,7 @@ class Hash
42
42
  end
43
43
 
44
44
  # call-seq:
45
- # hash.only_pair([relax]) => anArray
45
+ # hash.only_pair(relax = true or false) => anArray
46
46
  #
47
47
  # Returns the only key/value pair of _hash_ as an array. Raises an IndexError
48
48
  # if _hash_'s size is not 1, unless +relax+ is true.
@@ -45,8 +45,8 @@ class Integer
45
45
  # Calculate the factorial of _int_ with the help of memoization (Which gives
46
46
  # a considerable speedup for repeated calculations -- at the cost of memory).
47
47
  #
48
- # WARNING: Don't try to calculate the factorial this way for a too large
49
- # integer! This might well bring your system down to its knees... ;-)
48
+ # WARNING: Don't try to calculate the factorial this way for "large"
49
+ # integers! This might well bring your system down to its knees... ;-)
50
50
  def factorial_memoized
51
51
  FACTORIAL[self] ||= (1..self).inject { |f, i| FACTORIAL[i] ||= f * i }
52
52
  end
@@ -25,7 +25,7 @@
25
25
  ###############################################################################
26
26
  #++
27
27
 
28
- require File.join(File.dirname(__FILE__), '..', 'enumerable', 'agrep')
28
+ require 'nuggets/enumerable/agrep'
29
29
 
30
30
  class IO
31
31
 
@@ -39,8 +39,8 @@ class IO
39
39
  #
40
40
  # Opens +name+ with mode +r+. NOTE: With no associated block,
41
41
  # acts like the original IO::read, not like IO::new.
42
- def read(name, *args, &block)
43
- return _nuggets_original_read(name, *args) unless block
42
+ def read(name, *args)
43
+ return _nuggets_original_read(name, *args) unless block_given?
44
44
 
45
45
  case args.size
46
46
  when 0
@@ -56,7 +56,7 @@ class IO
56
56
  raise ArgumentError, "wrong number of arguments (#{args.size + 1} for 1-2)"
57
57
  end
58
58
 
59
- open_with_mode(name, 'r', binary, &block)
59
+ open_with_mode(name, 'r', binary) { |*a| yield(*a) }
60
60
  end
61
61
 
62
62
  # call-seq:
@@ -64,8 +64,10 @@ class IO
64
64
  # IO.write(name, binary = false) { |io| ... } => anObject
65
65
  #
66
66
  # Opens +name+ with mode +w+.
67
- def write(name, binary = false, &block)
68
- open_with_mode(name, 'w', binary, &block)
67
+ def write(name, binary = false)
68
+ block_given? ?
69
+ open_with_mode(name, 'w', binary) { |*a| yield(*a) } :
70
+ open_with_mode(name, 'w', binary)
69
71
  end
70
72
 
71
73
  # call-seq:
@@ -73,8 +75,10 @@ class IO
73
75
  # IO.append(name, binary = false) { |io| ... } => anObject
74
76
  #
75
77
  # Opens +name+ with mode +a+.
76
- def append(name, binary = false, &block)
77
- open_with_mode(name, 'a', binary, &block)
78
+ def append(name, binary = false)
79
+ block_given? ?
80
+ open_with_mode(name, 'a', binary) { |*a| yield(*a) } :
81
+ open_with_mode(name, 'a', binary)
78
82
  end
79
83
 
80
84
  # call-seq:
@@ -82,8 +86,10 @@ class IO
82
86
  # IO.read_write(name, binary = false) { |io| ... } => anObject
83
87
  #
84
88
  # Opens +name+ with mode <tt>r+</tt>.
85
- def read_write(name, binary = false, &block)
86
- open_with_mode(name, 'r+', binary, &block)
89
+ def read_write(name, binary = false)
90
+ block_given? ?
91
+ open_with_mode(name, 'r+', binary) { |*a| yield(*a) } :
92
+ open_with_mode(name, 'r+', binary)
87
93
  end
88
94
 
89
95
  # call-seq:
@@ -91,8 +97,10 @@ class IO
91
97
  # IO.write_read(name, binary = false) { |io| ... } => anObject
92
98
  #
93
99
  # Opens +name+ with mode <tt>w+</tt>.
94
- def write_read(name, binary = false, &block)
95
- open_with_mode(name, 'w+', binary, &block)
100
+ def write_read(name, binary = false)
101
+ block_given? ?
102
+ open_with_mode(name, 'w+', binary) { |*a| yield(*a) } :
103
+ open_with_mode(name, 'w+', binary)
96
104
  end
97
105
 
98
106
  # call-seq:
@@ -100,16 +108,18 @@ class IO
100
108
  # IO.append_read(name, binary = false) { |io| ... } => anObject
101
109
  #
102
110
  # Opens +name+ with mode <tt>a+</tt>.
103
- def append_read(name, binary = false, &block)
104
- open_with_mode(name, 'a+', binary, &block)
111
+ def append_read(name, binary = false)
112
+ block_given? ?
113
+ open_with_mode(name, 'a+', binary) { |*a| yield(*a) } :
114
+ open_with_mode(name, 'a+', binary)
105
115
  end
106
116
 
107
117
  private
108
118
 
109
119
  # Just a helper to DRY things up.
110
- def open_with_mode(name, mode, binary = false, &block)
120
+ def open_with_mode(name, mode, binary = false)
111
121
  mode << 'b' if binary
112
- open(name, mode, &block)
122
+ block_given? ? open(name, mode) { |io| yield io } : open(name, mode)
113
123
  end
114
124
 
115
125
  end
@@ -1,2 +1,2 @@
1
1
  # backwards compatibility
2
- require File.join(File.dirname(__FILE__), 'limit')
2
+ require 'nuggets/numeric/limit'
@@ -42,12 +42,12 @@ class Object
42
42
  return true if blank?
43
43
 
44
44
  modifiers.each { |modifier|
45
- next unless respond_to?(modifier)
46
-
47
- if modifier.to_s[-1] == ??
48
- return true if send(modifier)
49
- else
50
- return true if send(modifier).blank?
45
+ if respond_to?(modifier)
46
+ if modifier.to_s =~ /\?\z/
47
+ return true if send(modifier)
48
+ else
49
+ return true if send(modifier).blank?
50
+ end
51
51
  end
52
52
  }
53
53
 
@@ -86,11 +86,18 @@ class Hash
86
86
  #
87
87
  # Returns true if all of _hash_'s values are themselves vain.
88
88
  def vain?
89
- blank? { |h| h.delete_if { |k, v| v.vain? } }
89
+ blank? { |h| h.delete_if { |_, v| v.vain? } }
90
90
  end
91
91
 
92
92
  end
93
93
 
94
+ class String
95
+ if public_instance_methods(false).include?(method = 'blank?')
96
+ # remove incompatible implementation added by utility_belt/language_greps.rb
97
+ remove_method method
98
+ end
99
+ end
100
+
94
101
  if $0 == __FILE__
95
102
  ['', ' ', 's', 0, 1, nil, true, false, [], [nil], {}].each { |o|
96
103
  p o
@@ -0,0 +1,69 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of ruby-nuggets, some extensions to the Ruby programming #
5
+ # language. #
6
+ # #
7
+ # Copyright (C) 2007-2008 Jens Wille #
8
+ # #
9
+ # Authors: #
10
+ # Jens Wille <jens.wille@uni-koeln.de> #
11
+ # #
12
+ # ruby-nuggets is free software; you can redistribute it and/or modify it #
13
+ # under the terms of the GNU General Public License as published by the Free #
14
+ # Software Foundation; either version 3 of the License, or (at your option) #
15
+ # any later version. #
16
+ # #
17
+ # ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
18
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
19
+ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
20
+ # more details. #
21
+ # #
22
+ # You should have received a copy of the GNU General Public License along #
23
+ # with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
24
+ # #
25
+ ###############################################################################
26
+ #++
27
+
28
+ class Object
29
+
30
+ # call-seq:
31
+ # object.boolean? => true or false
32
+ #
33
+ #
34
+ def boolean?
35
+ is_a?(TrueClass) || is_a?(FalseClass)
36
+ end
37
+
38
+ # call-seq:
39
+ # object.negate => true or false
40
+ #
41
+ #
42
+ def negate
43
+ !self
44
+ end
45
+
46
+ alias_method :false?, :negate
47
+
48
+ # call-seq:
49
+ # object.to_bool => true or false
50
+ #
51
+ #
52
+ def to_bool
53
+ !!self
54
+ end
55
+
56
+ alias_method :true?, :to_bool
57
+
58
+ end
59
+
60
+ if $0 == __FILE__
61
+ [0, 1, nil, '', 'abc', true, false, Class, Object.new].each { |o|
62
+ p o
63
+ p o.boolean?
64
+ p o.negate
65
+ p o.to_bool
66
+ p o.true?
67
+ p o.false?
68
+ }
69
+ end
@@ -1,2 +1,2 @@
1
1
  # whatever you prefer to call it...
2
- require File.join(File.dirname(__FILE__), 'singleton_class')
2
+ require 'nuggets/singleton_class'
@@ -1,2 +1,2 @@
1
1
  # whatever you prefer to call it...
2
- require File.join(File.dirname(__FILE__), 'singleton_class')
2
+ require 'nuggets/singleton_class'
@@ -1,2 +1,2 @@
1
1
  # whatever you prefer to call it...
2
- require File.join(File.dirname(__FILE__), 'singleton_class')
2
+ require 'nuggets/singleton_class'
@@ -56,7 +56,7 @@ class Object
56
56
  return obj if self.equal?(obj.singleton_class)
57
57
  }
58
58
 
59
- # if we got here it can't be a singleton class
59
+ # if we got here, it can't be a singleton class
60
60
  # or its singleton object doesn't exist anymore
61
61
  raise TypeError
62
62
  rescue TypeError
@@ -135,8 +135,10 @@ if $0 == __FILE__
135
135
 
136
136
  ###
137
137
 
138
- class A; end
139
- class B < A; end
138
+ class A # :nodoc:
139
+ end
140
+ class B < A # :nodoc:
141
+ end
140
142
 
141
143
  a = A.singleton_class
142
144
  b = B.singleton_class
@@ -1,2 +1,2 @@
1
1
  # whatever you prefer to call it...
2
- require File.join(File.dirname(__FILE__), 'singleton_class')
2
+ require 'nuggets/singleton_class'
@@ -1,2 +1,2 @@
1
1
  # whatever you prefer to call it...
2
- require File.join(File.dirname(__FILE__), 'singleton_class')
2
+ require 'nuggets/singleton_class'
@@ -25,8 +25,6 @@
25
25
  ###############################################################################
26
26
  #++
27
27
 
28
- require File.join(File.dirname(__FILE__), '..', 'object', 'singleton_class')
29
-
30
28
  class Proc
31
29
 
32
30
  # call-seq:
@@ -36,7 +34,7 @@ class Proc
36
34
  def bind(object)
37
35
  block, time = self, Time.now
38
36
 
39
- object.singleton_class.class_eval {
37
+ (class << object; self; end).class_eval {
40
38
  method_name = "__bind_#{time.to_i}_#{time.usec}"
41
39
  define_method(method_name, &block)
42
40
 
@@ -25,7 +25,7 @@
25
25
  ###############################################################################
26
26
  #++
27
27
 
28
- require File.join(File.dirname(__FILE__), 'evaluate')
28
+ require 'nuggets/string/evaluate'
29
29
 
30
30
  class String
31
31
 
@@ -43,7 +43,7 @@ class String
43
43
  # string or a regexp, +substitution+ may contain string expressions (cf.
44
44
  # #evaluate).
45
45
  def msub(*substitutions)
46
- dup.msub!(*substitutions) || dup
46
+ (_dup = dup).msub!(*substitutions) || _dup
47
47
  end
48
48
 
49
49
  # call-seq:
@@ -62,7 +62,7 @@ class String
62
62
  }
63
63
 
64
64
  gsub!(Regexp.union(*keys)) { |match|
65
- cache[match] ||= subs.find { |key,|
65
+ cache[match] ||= subs.find { |key, _|
66
66
  key =~ match
67
67
  }.last.evaluate(binding)
68
68
  }
@@ -33,8 +33,11 @@ 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, &block)
37
- dup.nsub!(*args, &block) || dup
36
+ def nsub(*args)
37
+ _dup = dup
38
+ (block_given? ?
39
+ _dup.nsub!(*args) { |*a| yield(*a) } :
40
+ _dup.nsub!(*args)) || _dup
38
41
  end
39
42
 
40
43
  # call-seq:
@@ -43,29 +46,24 @@ class String
43
46
  #
44
47
  # Performs the substitutions of #nsub in place, returning _str_, or +nil+ if
45
48
  # no substitutions were performed.
46
- def nsub!(*args, &block)
49
+ def nsub!(*args)
50
+ pattern, i = args.first, 0
51
+
47
52
  case args.size
48
53
  when 2
49
- pattern = args.shift
54
+ # Only +count+ given
55
+ count = args.last
50
56
 
51
- # Only +count+ given; require block
52
- count = *args
53
- raise LocalJumpError, 'no block given' unless block_given?
57
+ gsub!(pattern) { |match| (i += 1) <= count ? yield(match) : match }
54
58
  when 3
55
- pattern = args.shift
56
-
57
59
  # Both +replacement+ and +count+ given;
58
60
  # ignore block (just like String#gsub does)
59
- replacement, count = *args
60
- block = lambda { replacement }
61
+ replacement, count = args.values_at(1, 2)
62
+
63
+ gsub!(pattern) { |match| (i += 1) <= count ? replacement : match }
61
64
  else
62
65
  raise ArgumentError, "wrong number of arguments (#{args.size} for 2-3)"
63
66
  end
64
-
65
- i = 0
66
- gsub!(pattern) { |match|
67
- (i += 1) <= count ? block[match] : match
68
- }
69
67
  end
70
68
 
71
69
  end
@@ -37,18 +37,20 @@ 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, &block)
41
- return sub_without_md(pattern, replacement) if replacement
42
- dup.sub_with_md!(pattern, &block) || dup
40
+ def sub_with_md(pattern, replacement = nil)
41
+ replacement ?
42
+ sub_without_md(pattern, replacement) :
43
+ (_dup = dup).sub_with_md!(pattern) { |*a| yield(*a) } || _dup
43
44
  end
44
45
 
45
46
  # call-seq:
46
47
  # str.sub_with_md!(pattern) { |match_data| ... } => str or nil
47
48
  #
48
49
  # Destructive version of #sub_with_md.
49
- def sub_with_md!(pattern, replacement = nil, &block)
50
- return sub_without_md!(pattern, replacement) if replacement
51
- sub_without_md!(pattern) { |match| block[$~] }
50
+ def sub_with_md!(pattern, replacement = nil)
51
+ replacement ?
52
+ sub_without_md!(pattern, replacement) :
53
+ sub_without_md!(pattern) { |match| yield $~ }
52
54
  end
53
55
 
54
56
  # call-seq:
@@ -56,18 +58,20 @@ class String
56
58
  #
57
59
  # Just like #gsub, but passes the MatchData object instead of the current
58
60
  # match string to the block.
59
- def gsub_with_md(pattern, replacement = nil, &block)
60
- return gsub_without_md(pattern, replacement) if replacement
61
- dup.gsub_with_md!(pattern, &block) || dup
61
+ def gsub_with_md(pattern, replacement = nil)
62
+ replacement ?
63
+ gsub_without_md(pattern, replacement) :
64
+ (_dup = dup).gsub_with_md!(pattern) { |*a| yield(*a) } || _dup
62
65
  end
63
66
 
64
67
  # call-seq:
65
68
  # str.gsub_with_md!(pattern) { |match_data| ... } => str or nil
66
69
  #
67
70
  # Destructive version of #gsub_with_md.
68
- def gsub_with_md!(pattern, replacement = nil, &block)
69
- return gsub_without_md!(pattern, replacement) if replacement
70
- gsub_without_md!(pattern) { |match| block[$~] }
71
+ def gsub_with_md!(pattern, replacement = nil)
72
+ replacement ?
73
+ gsub_without_md!(pattern, replacement) :
74
+ gsub_without_md!(pattern) { |match| yield $~ }
71
75
  end
72
76
 
73
77
  # call-seq:
@@ -60,7 +60,7 @@ class String
60
60
  #
61
61
  # Similar to #word_wrap, but assumes a single paragraph.
62
62
  def word_wrap_paragraph(line_width = 80)
63
- dup.word_wrap_paragraph!(line_width) || dup
63
+ (_dup = dup).word_wrap_paragraph!(line_width) || _dup
64
64
  end
65
65
 
66
66
  # call-seq:
@@ -29,23 +29,6 @@ require 'tempfile'
29
29
 
30
30
  class Tempfile
31
31
 
32
- if RUBY_RELEASE_DATE < '2007-10-05'
33
- alias_method :_nuggets_original_make_tmpname, :make_tmpname
34
-
35
- # Enhanced Tempfile#make_tmpname, as of r13631 (Ruby version prior to
36
- # 2007-10-05)
37
- def make_tmpname(basename, name)
38
- case basename
39
- when Array
40
- prefix, suffix = *basename
41
- else
42
- prefix, suffix = basename, ''
43
- end
44
-
45
- "#{prefix}#{Time.now.strftime('%Y%m%d')}-#{$$}-#{rand(0x100000000).to_s(36)}-#{name}#{suffix}"
46
- end
47
- end
48
-
49
32
  alias_method :_nuggets_original_open, :open
50
33
 
51
34
  # If no block is given, this is a synonym for new().
@@ -1,3 +1,3 @@
1
1
  # just a short-cut
2
- require File.dirname(__FILE__)
2
+ require 'nuggets/util/added_methods'
3
3
  Util::AddedMethods.init
@@ -70,7 +70,7 @@ module Util
70
70
 
71
71
  extend self
72
72
 
73
- HISTFILENAME = '(Readline::HISTORY)'.freeze
73
+ HISTFILENAME = '(Readline::HISTORY)'.freeze unless const_defined?(:HISTFILENAME)
74
74
 
75
75
  class AddedMethod
76
76
 
@@ -25,7 +25,10 @@
25
25
  ###############################################################################
26
26
  #++
27
27
 
28
- require 'rubygems'
28
+ begin
29
+ require 'rubygems'
30
+ rescue LoadError
31
+ end
29
32
 
30
33
  begin
31
34
  require 'filemagic/ext'
@@ -35,6 +38,16 @@ rescue LoadError
35
38
  end
36
39
  end
37
40
 
41
+ begin
42
+ require 'nuggets/uri/content_type'
43
+ rescue LoadError
44
+ module URI
45
+ def self.content_type(path) # :nodoc:
46
+ nil
47
+ end
48
+ end
49
+ end
50
+
38
51
  begin
39
52
  require 'mime/types'
40
53
  rescue LoadError
@@ -47,8 +60,6 @@ rescue LoadError
47
60
  end
48
61
  end
49
62
 
50
- require File.join(File.dirname(__FILE__), '..', 'uri', 'content_type')
51
-
52
63
  module Util
53
64
 
54
65
  module ContentType
@@ -25,7 +25,7 @@
25
25
  ###############################################################################
26
26
  #++
27
27
 
28
- require File.join(File.dirname(__FILE__), '..', 'integer', 'to_binary_s')
28
+ require 'nuggets/integer/to_binary_s'
29
29
 
30
30
  class Integer
31
31
 
@@ -106,7 +106,7 @@ class String
106
106
  # Substitutes any diacritics in _str_ with their replacements as per
107
107
  # Util::I18n::DIACRITICS.
108
108
  def replace_diacritics
109
- dup.replace_diacritics! || dup
109
+ (_dup = dup).replace_diacritics! || _dup
110
110
  end
111
111
 
112
112
  # call-seq:
@@ -3,8 +3,8 @@ module Nuggets
3
3
  module Version
4
4
 
5
5
  MAJOR = 0
6
- MINOR = 3
7
- TINY = 6
6
+ MINOR = 4
7
+ TINY = 0
8
8
 
9
9
  class << self
10
10
 
data/lib/nuggets.rb CHANGED
@@ -24,6 +24,20 @@
24
24
  ###############################################################################
25
25
  #++
26
26
 
27
+ # Load selected +nuggets+.
28
+ #
29
+ # Examples:
30
+ # # All String nuggets
31
+ # Nuggets(:string)
32
+ # Nuggets(String)
33
+ #
34
+ # # Only 'msub' and 'word_wrap' String nuggets
35
+ # Nuggets(:string => %w[msub word_wrap])
36
+ #
37
+ # # Selected String nuggets and all Numeric nuggets
38
+ # Nuggets(:numeric, :string => %w[msub word_wrap])
39
+ #
40
+ # # ...you see the pattern ;-)
27
41
  def Nuggets(*nuggets)
28
42
  loaded_nuggets = []
29
43
 
@@ -32,7 +46,7 @@ def Nuggets(*nuggets)
32
46
 
33
47
  nuggets.each { |nugget|
34
48
  begin
35
- require path = File.join(base.to_s, nugget.to_s)
49
+ require path = File.join(base.to_s, nugget.to_s.downcase)
36
50
  loaded_nuggets << path
37
51
  rescue LoadError
38
52
  # if it's a directory, load anything in it
@@ -49,7 +63,7 @@ def Nuggets(*nuggets)
49
63
 
50
64
  nuggets_by_hierarchy.each { |hierarchy, nuggets|
51
65
  nuggets = [nuggets] if nuggets.is_a?(Hash)
52
- load_nuggets[File.join(base.to_s, hierarchy.to_s), *nuggets]
66
+ load_nuggets[File.join(base.to_s, hierarchy.to_s.downcase), *nuggets]
53
67
  }
54
68
  }
55
69
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-nuggets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6.294
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Wille
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-10-10 00:00:00 +02:00
12
+ date: 2008-12-05 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -24,80 +24,81 @@ extra_rdoc_files:
24
24
  - ChangeLog
25
25
  - README
26
26
  files:
27
- - lib/nuggets.rb
28
- - lib/nuggets/integer/to_binary_s.rb
29
- - lib/nuggets/integer/factorial.rb
30
- - lib/nuggets/version.rb
31
- - lib/nuggets/object/metaclass.rb
32
- - lib/nuggets/object/singleton_class.rb
33
- - lib/nuggets/object/virtual_class.rb
34
- - lib/nuggets/object/ghost_class.rb
35
- - lib/nuggets/object/eigenclass.rb
36
- - lib/nuggets/object/blank.rb
37
- - lib/nuggets/object/msend.rb
38
- - lib/nuggets/object/uniclass.rb
27
+ - lib/nuggets/env/user_home.rb
28
+ - lib/nuggets/env/user_encoding.rb
29
+ - lib/nuggets/numeric/limit.rb
30
+ - lib/nuggets/numeric/to_multiple.rb
31
+ - lib/nuggets/numeric/between.rb
32
+ - lib/nuggets/numeric/duration.rb
33
+ - lib/nuggets/numeric/signum.rb
34
+ - lib/nuggets/array/to_hash.rb
35
+ - lib/nuggets/array/shuffle.rb
36
+ - lib/nuggets/array/format.rb
37
+ - lib/nuggets/array/monotone.rb
38
+ - lib/nuggets/array/only.rb
39
+ - lib/nuggets/array/flatten_once.rb
40
+ - lib/nuggets/array/rand.rb
41
+ - lib/nuggets/array/combination.rb
42
+ - lib/nuggets/array/in_order.rb
43
+ - lib/nuggets/hash/only.rb
44
+ - lib/nuggets/hash/at.rb
45
+ - lib/nuggets/hash/insert.rb
46
+ - lib/nuggets/hash/in_order.rb
39
47
  - lib/nuggets/enumerable/agrep.rb
40
48
  - lib/nuggets/enumerable/all_any_extended.rb
41
49
  - lib/nuggets/enumerable/minmax.rb
50
+ - lib/nuggets/io/agrep.rb
51
+ - lib/nuggets/io/modes.rb
42
52
  - lib/nuggets/all.rb
43
- - lib/nuggets/string/sub_with_md.rb
53
+ - lib/nuggets/uri/content_type.rb
54
+ - lib/nuggets/uri/exist.rb
44
55
  - lib/nuggets/string/msub.rb
45
- - lib/nuggets/string/case.rb
56
+ - lib/nuggets/string/capitalize_first.rb
46
57
  - lib/nuggets/string/evaluate.rb
47
- - lib/nuggets/string/word_wrap.rb
58
+ - lib/nuggets/string/case.rb
59
+ - lib/nuggets/string/sub_with_md.rb
48
60
  - lib/nuggets/string/nsub.rb
49
- - lib/nuggets/string/capitalize_first.rb
50
- - lib/nuggets/env/user_home.rb
51
- - lib/nuggets/env/user_encoding.rb
52
- - lib/nuggets/hash/at.rb
53
- - lib/nuggets/hash/only.rb
54
- - lib/nuggets/hash/in_order.rb
55
- - lib/nuggets/hash/insert.rb
61
+ - lib/nuggets/string/word_wrap.rb
62
+ - lib/nuggets/integer/to_binary_s.rb
63
+ - lib/nuggets/integer/factorial.rb
64
+ - lib/nuggets/object/uniclass.rb
65
+ - lib/nuggets/object/virtual_class.rb
66
+ - lib/nuggets/object/blank.rb
67
+ - lib/nuggets/object/ghost_class.rb
68
+ - lib/nuggets/object/metaclass.rb
69
+ - lib/nuggets/object/singleton_class.rb
70
+ - lib/nuggets/object/eigenclass.rb
71
+ - lib/nuggets/object/msend.rb
72
+ - lib/nuggets/object/boolean.rb
56
73
  - lib/nuggets/proc/bind.rb
57
- - lib/nuggets/array/rand.rb
58
- - lib/nuggets/array/to_hash.rb
59
- - lib/nuggets/array/flatten_once.rb
60
- - lib/nuggets/array/only.rb
61
- - lib/nuggets/array/in_order.rb
62
- - lib/nuggets/array/shuffle.rb
63
- - lib/nuggets/array/monotone.rb
64
- - lib/nuggets/array/format.rb
65
- - lib/nuggets/array/combination.rb
66
- - lib/nuggets/numeric/between.rb
67
- - lib/nuggets/numeric/duration.rb
68
- - lib/nuggets/numeric/signum.rb
69
- - lib/nuggets/numeric/limit.rb
70
- - lib/nuggets/numeric/to_multiple.rb
71
- - lib/nuggets/util/added_methods/init.rb
72
74
  - lib/nuggets/util/added_methods.rb
73
- - lib/nuggets/util/ansicolor2css.rb
74
75
  - lib/nuggets/util/content_type.rb
75
76
  - lib/nuggets/util/dotted_decimal.rb
77
+ - lib/nuggets/util/ansicolor2css.rb
76
78
  - lib/nuggets/util/i18n.rb
79
+ - lib/nuggets/util/added_methods/init.rb
80
+ - lib/nuggets/version.rb
81
+ - lib/nuggets/tempfile/open.rb
77
82
  - lib/nuggets/file/which.rb
78
- - lib/nuggets/tempfile.rb
79
- - lib/nuggets/uri/content_type.rb
80
- - lib/nuggets/uri/exist.rb
81
- - lib/nuggets/io/modes.rb
82
- - lib/nuggets/io/agrep.rb
83
- - COPYING
83
+ - lib/nuggets.rb
84
+ - Rakefile
84
85
  - HEADER
85
- - README
86
+ - COPYING
86
87
  - ChangeLog
87
- - Rakefile
88
+ - README
88
89
  has_rdoc: true
89
90
  homepage: http://prometheus.rubyforge.org/ruby-nuggets
90
91
  post_install_message:
91
92
  rdoc_options:
92
- - --title
93
- - ruby-nuggets Application documentation
94
- - --charset
95
- - UTF-8
96
93
  - --main
97
94
  - README
98
- - --all
99
95
  - --line-numbers
100
96
  - --inline-source
97
+ - --all
98
+ - --title
99
+ - ruby-nuggets Application documentation
100
+ - --charset
101
+ - UTF-8
101
102
  require_paths:
102
103
  - lib
103
104
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -115,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
116
  requirements: []
116
117
 
117
118
  rubyforge_project: prometheus
118
- rubygems_version: 1.3.0
119
+ rubygems_version: 1.3.1
119
120
  signing_key:
120
121
  specification_version: 2
121
122
  summary: Some extensions to the Ruby programming language.