ruby-nuggets 0.3.6.294 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +30 -2
- data/lib/nuggets/all.rb +6 -2
- data/lib/nuggets/array/format.rb +1 -1
- data/lib/nuggets/array/monotone.rb +4 -4
- data/lib/nuggets/array/only.rb +1 -1
- data/lib/nuggets/array/shuffle.rb +2 -2
- data/lib/nuggets/array/to_hash.rb +12 -40
- data/lib/nuggets/enumerable/agrep.rb +14 -4
- data/lib/nuggets/enumerable/all_any_extended.rb +12 -8
- data/lib/nuggets/enumerable/minmax.rb +6 -4
- data/lib/nuggets/file/which.rb +1 -1
- data/lib/nuggets/hash/at.rb +1 -1
- data/lib/nuggets/hash/in_order.rb +1 -1
- data/lib/nuggets/hash/insert.rb +2 -2
- data/lib/nuggets/hash/only.rb +3 -3
- data/lib/nuggets/integer/factorial.rb +2 -2
- data/lib/nuggets/io/agrep.rb +1 -1
- data/lib/nuggets/io/modes.rb +25 -15
- data/lib/nuggets/numeric/between.rb +1 -1
- data/lib/nuggets/object/blank.rb +14 -7
- data/lib/nuggets/object/boolean.rb +69 -0
- data/lib/nuggets/object/eigenclass.rb +1 -1
- data/lib/nuggets/object/ghost_class.rb +1 -1
- data/lib/nuggets/object/metaclass.rb +1 -1
- data/lib/nuggets/object/singleton_class.rb +5 -3
- data/lib/nuggets/object/uniclass.rb +1 -1
- data/lib/nuggets/object/virtual_class.rb +1 -1
- data/lib/nuggets/proc/bind.rb +1 -3
- data/lib/nuggets/string/msub.rb +3 -3
- data/lib/nuggets/string/nsub.rb +14 -16
- data/lib/nuggets/string/sub_with_md.rb +16 -12
- data/lib/nuggets/string/word_wrap.rb +1 -1
- data/lib/nuggets/{tempfile.rb → tempfile/open.rb} +0 -17
- data/lib/nuggets/util/added_methods/init.rb +1 -1
- data/lib/nuggets/util/added_methods.rb +1 -1
- data/lib/nuggets/util/content_type.rb +14 -3
- data/lib/nuggets/util/dotted_decimal.rb +1 -1
- data/lib/nuggets/util/i18n.rb +1 -1
- data/lib/nuggets/version.rb +2 -2
- data/lib/nuggets.rb +16 -2
- 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.
|
5
|
+
This documentation refers to ruby-nuggets version 0.4.0
|
6
6
|
|
7
7
|
|
8
8
|
== DESCRIPTION
|
9
9
|
|
10
|
-
|
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
|
-
|
29
|
-
|
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
|
}
|
data/lib/nuggets/array/format.rb
CHANGED
@@ -28,12 +28,12 @@
|
|
28
28
|
class Array
|
29
29
|
|
30
30
|
# call-seq:
|
31
|
-
# array.monotone?(
|
31
|
+
# array.monotone?(operator) => true or false
|
32
32
|
#
|
33
|
-
# Check whether _array_ is monotone according to operator
|
34
|
-
def monotone?(
|
33
|
+
# Check whether _array_ is monotone according to +operator+.
|
34
|
+
def monotone?(operator)
|
35
35
|
inject { |a, b|
|
36
|
-
return false unless a.send(
|
36
|
+
return false unless a.send(operator, b)
|
37
37
|
b
|
38
38
|
}
|
39
39
|
|
data/lib/nuggets/array/only.rb
CHANGED
@@ -101,8 +101,8 @@ if $0 == __FILE__
|
|
101
101
|
a.shuffle!
|
102
102
|
p a
|
103
103
|
|
104
|
-
require
|
105
|
-
require
|
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
|
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
|
51
|
-
|
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
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
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
|
-
|
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
|
-
|
29
|
-
require '
|
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
|
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
|
-
|
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 =
|
41
|
-
_nuggets_original_all?(&
|
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 =
|
52
|
-
_nuggets_original_any?(&
|
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
|
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
|
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
|
-
|
64
|
-
|
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
|
86
|
-
what ? minmax(:max, what) :
|
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
|
95
|
-
what ? minmax(:min, what) :
|
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
|
data/lib/nuggets/file/which.rb
CHANGED
data/lib/nuggets/hash/at.rb
CHANGED
data/lib/nuggets/hash/insert.rb
CHANGED
@@ -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
|
51
|
-
replace insert(other
|
50
|
+
def insert!(other)
|
51
|
+
replace block_given? ? insert(other) { |*a| yield(*a) } : insert(other)
|
52
52
|
end
|
53
53
|
|
54
54
|
end
|
data/lib/nuggets/hash/only.rb
CHANGED
@@ -25,12 +25,12 @@
|
|
25
25
|
###############################################################################
|
26
26
|
#++
|
27
27
|
|
28
|
-
require
|
28
|
+
require 'nuggets/hash/at'
|
29
29
|
|
30
30
|
class Hash
|
31
31
|
|
32
32
|
# call-seq:
|
33
|
-
# hash.only(
|
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(
|
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
|
49
|
-
#
|
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
|
data/lib/nuggets/io/agrep.rb
CHANGED
data/lib/nuggets/io/modes.rb
CHANGED
@@ -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
|
43
|
-
return _nuggets_original_read(name, *args) unless
|
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
|
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
|
68
|
-
|
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
|
77
|
-
|
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
|
86
|
-
|
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
|
95
|
-
|
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
|
104
|
-
|
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
|
120
|
+
def open_with_mode(name, mode, binary = false)
|
111
121
|
mode << 'b' if binary
|
112
|
-
open(name, mode,
|
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
|
2
|
+
require 'nuggets/numeric/limit'
|
data/lib/nuggets/object/blank.rb
CHANGED
@@ -42,12 +42,12 @@ class Object
|
|
42
42
|
return true if blank?
|
43
43
|
|
44
44
|
modifiers.each { |modifier|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
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 { |
|
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
|
2
|
+
require 'nuggets/singleton_class'
|
@@ -1,2 +1,2 @@
|
|
1
1
|
# whatever you prefer to call it...
|
2
|
-
require
|
2
|
+
require 'nuggets/singleton_class'
|
@@ -1,2 +1,2 @@
|
|
1
1
|
# whatever you prefer to call it...
|
2
|
-
require
|
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
|
139
|
-
|
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
|
2
|
+
require 'nuggets/singleton_class'
|
@@ -1,2 +1,2 @@
|
|
1
1
|
# whatever you prefer to call it...
|
2
|
-
require
|
2
|
+
require 'nuggets/singleton_class'
|
data/lib/nuggets/proc/bind.rb
CHANGED
@@ -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.
|
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
|
|
data/lib/nuggets/string/msub.rb
CHANGED
@@ -25,7 +25,7 @@
|
|
25
25
|
###############################################################################
|
26
26
|
#++
|
27
27
|
|
28
|
-
require
|
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) ||
|
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
|
}
|
data/lib/nuggets/string/nsub.rb
CHANGED
@@ -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
|
37
|
-
|
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
|
49
|
+
def nsub!(*args)
|
50
|
+
pattern, i = args.first, 0
|
51
|
+
|
47
52
|
case args.size
|
48
53
|
when 2
|
49
|
-
|
54
|
+
# Only +count+ given
|
55
|
+
count = args.last
|
50
56
|
|
51
|
-
|
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 =
|
60
|
-
|
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
|
41
|
-
|
42
|
-
|
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
|
50
|
-
|
51
|
-
|
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
|
60
|
-
|
61
|
-
|
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
|
69
|
-
|
70
|
-
|
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:
|
@@ -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().
|
@@ -25,7 +25,10 @@
|
|
25
25
|
###############################################################################
|
26
26
|
#++
|
27
27
|
|
28
|
-
|
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
|
data/lib/nuggets/util/i18n.rb
CHANGED
data/lib/nuggets/version.rb
CHANGED
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.
|
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-
|
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/
|
29
|
-
- lib/nuggets/
|
30
|
-
- lib/nuggets/
|
31
|
-
- lib/nuggets/
|
32
|
-
- lib/nuggets/
|
33
|
-
- lib/nuggets/
|
34
|
-
- lib/nuggets/
|
35
|
-
- lib/nuggets/
|
36
|
-
- lib/nuggets/
|
37
|
-
- lib/nuggets/
|
38
|
-
- lib/nuggets/
|
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/
|
53
|
+
- lib/nuggets/uri/content_type.rb
|
54
|
+
- lib/nuggets/uri/exist.rb
|
44
55
|
- lib/nuggets/string/msub.rb
|
45
|
-
- lib/nuggets/string/
|
56
|
+
- lib/nuggets/string/capitalize_first.rb
|
46
57
|
- lib/nuggets/string/evaluate.rb
|
47
|
-
- lib/nuggets/string/
|
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/
|
50
|
-
- lib/nuggets/
|
51
|
-
- lib/nuggets/
|
52
|
-
- lib/nuggets/
|
53
|
-
- lib/nuggets/
|
54
|
-
- lib/nuggets/
|
55
|
-
- lib/nuggets/
|
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
|
79
|
-
-
|
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
|
-
-
|
86
|
+
- COPYING
|
86
87
|
- ChangeLog
|
87
|
-
-
|
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.
|
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.
|