ruby-nuggets 0.7.4 → 0.7.5
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.
- data/README +1 -1
- data/lib/nuggets/all.rb +4 -4
- data/lib/nuggets/all_mixins.rb +4 -4
- data/lib/nuggets/array/flatten_once.rb +1 -1
- data/lib/nuggets/array/format.rb +3 -3
- data/lib/nuggets/array/only.rb +2 -2
- data/lib/nuggets/array/rand.rb +1 -1
- data/lib/nuggets/array/shuffle.rb +2 -2
- data/lib/nuggets/array/standard_deviation_mixin.rb +1 -1
- data/lib/nuggets/array/to_hash.rb +4 -4
- data/lib/nuggets/enumerable/agrep.rb +5 -5
- data/lib/nuggets/enumerable/all_any_extended.rb +10 -10
- data/lib/nuggets/enumerable/minmax.rb +2 -2
- data/lib/nuggets/env/set_mixin.rb +2 -2
- data/lib/nuggets/env/user_encoding_mixin.rb +3 -3
- data/lib/nuggets/env/user_home_mixin.rb +1 -1
- data/lib/nuggets/file/which_mixin.rb +2 -2
- data/lib/nuggets/hash/at.rb +1 -1
- data/lib/nuggets/hash/insert.rb +1 -1
- data/lib/nuggets/hash/nest_mixin.rb +1 -1
- data/lib/nuggets/hash/only.rb +2 -2
- data/lib/nuggets/integer/factorial.rb +1 -1
- data/lib/nuggets/io/agrep.rb +1 -1
- data/lib/nuggets/io/modes.rb +12 -12
- data/lib/nuggets/io/null_mixin.rb +1 -1
- data/lib/nuggets/net/success.rb +60 -0
- data/lib/nuggets/numeric/duration.rb +2 -2
- data/lib/nuggets/object/singleton_class_mixin.rb +5 -5
- data/lib/nuggets/string/evaluate_mixin.rb +1 -1
- data/lib/nuggets/string/msub.rb +5 -5
- data/lib/nuggets/string/nsub.rb +1 -1
- data/lib/nuggets/string/sub_with_md.rb +2 -2
- data/lib/nuggets/string/xor_mixin.rb +1 -1
- data/lib/nuggets/uri/content_type_mixin.rb +6 -5
- data/lib/nuggets/uri/exist_mixin.rb +12 -5
- data/lib/nuggets/util/ansicolor2css.rb +2 -2
- data/lib/nuggets/util/cli.rb +12 -12
- data/lib/nuggets/util/content_type.rb +9 -9
- data/lib/nuggets/util/i18n.rb +2 -2
- data/lib/nuggets/util/ruby.rb +29 -29
- data/lib/nuggets/version.rb +1 -1
- data/lib/nuggets.rb +8 -8
- metadata +133 -132
data/README
CHANGED
data/lib/nuggets/all.rb
CHANGED
@@ -25,12 +25,12 @@
|
|
25
25
|
###############################################################################
|
26
26
|
#++
|
27
27
|
|
28
|
-
base = File.dirname(__FILE__)
|
29
|
-
base_re = Regexp.escape(base)
|
28
|
+
base = ::File.dirname(__FILE__)
|
29
|
+
base_re = ::Regexp.escape(base)
|
30
30
|
|
31
|
-
Dir[File.join(base, %w[* ** *.rb])].sort.each { |path|
|
31
|
+
::Dir[::File.join(base, %w[* ** *.rb])].sort.each { |path|
|
32
32
|
next if path =~ /_mixin\.rb\z/
|
33
33
|
|
34
|
-
ext_re = Regexp.escape(File.extname(path))
|
34
|
+
ext_re = ::Regexp.escape(::File.extname(path))
|
35
35
|
require path.sub(/#{base_re}(.*)#{ext_re}/, 'nuggets\1')
|
36
36
|
}
|
data/lib/nuggets/all_mixins.rb
CHANGED
@@ -25,10 +25,10 @@
|
|
25
25
|
###############################################################################
|
26
26
|
#++
|
27
27
|
|
28
|
-
base = File.dirname(__FILE__)
|
29
|
-
base_re = Regexp.escape(base)
|
28
|
+
base = ::File.dirname(__FILE__)
|
29
|
+
base_re = ::Regexp.escape(base)
|
30
30
|
|
31
|
-
Dir[File.join(base, %w[* ** *_mixin.rb])].sort.each { |path|
|
32
|
-
ext_re = Regexp.escape(File.extname(path))
|
31
|
+
::Dir[::File.join(base, %w[* ** *_mixin.rb])].sort.each { |path|
|
32
|
+
ext_re = ::Regexp.escape(::File.extname(path))
|
33
33
|
require path.sub(/#{base_re}(.*)#{ext_re}/, 'nuggets\1')
|
34
34
|
}
|
data/lib/nuggets/array/format.rb
CHANGED
@@ -42,17 +42,17 @@ class Array
|
|
42
42
|
# _str_; empty string if _str_ is empty.
|
43
43
|
def %(args)
|
44
44
|
opts = { :sep => ', ' }
|
45
|
-
opts.update(pop) if last.is_a?(Hash)
|
45
|
+
opts.update(pop) if last.is_a?(::Hash)
|
46
46
|
|
47
47
|
default = lambda { |n|
|
48
48
|
['%s'] * n * opts[:sep]
|
49
49
|
}
|
50
50
|
|
51
51
|
case args
|
52
|
-
when String
|
52
|
+
when ::String
|
53
53
|
return (first || default[1]) % args unless
|
54
54
|
args.nil? || args.empty?
|
55
|
-
when Array
|
55
|
+
when ::Array
|
56
56
|
i = 0
|
57
57
|
[*args].comb { |x|
|
58
58
|
return (self[i] || default[x.size]) % x unless
|
data/lib/nuggets/array/only.rb
CHANGED
@@ -36,7 +36,7 @@ class Array
|
|
36
36
|
#
|
37
37
|
# Idea stolen from Gavin Sinclair's Ruby Extensions Project.
|
38
38
|
def only(relax = size == 1)
|
39
|
-
raise IndexError, 'not a single-element array' unless relax
|
39
|
+
raise ::IndexError, 'not a single-element array' unless relax
|
40
40
|
first
|
41
41
|
end
|
42
42
|
|
@@ -48,7 +48,7 @@ if $0 == __FILE__
|
|
48
48
|
|
49
49
|
begin
|
50
50
|
p a.only
|
51
|
-
rescue IndexError => err
|
51
|
+
rescue ::IndexError => err
|
52
52
|
warn err
|
53
53
|
end
|
54
54
|
|
data/lib/nuggets/array/rand.rb
CHANGED
@@ -33,7 +33,7 @@ class Array
|
|
33
33
|
# Shuffles _array_ in random order. Select a different shuffling algorithm:
|
34
34
|
# <tt>Array.send(:alias_method, :shuffle, :shuffle_kfy)</tt>.
|
35
35
|
def shuffle
|
36
|
-
sort_by { Kernel.rand }
|
36
|
+
sort_by { ::Kernel.rand }
|
37
37
|
end
|
38
38
|
|
39
39
|
# call-seq:
|
@@ -114,7 +114,7 @@ if $0 == __FILE__
|
|
114
114
|
max = algorithms.max(:length)
|
115
115
|
|
116
116
|
algorithms.each { |algorithm|
|
117
|
-
score = Hash.new { |h, k| h[k] = 0 }
|
117
|
+
score = ::Hash.new { |h, k| h[k] = 0 }
|
118
118
|
|
119
119
|
n.times {
|
120
120
|
score[a.send(algorithm)] += 1
|
@@ -41,7 +41,7 @@ module Nuggets
|
|
41
41
|
# Calculates the {standard deviation}[http://en.wikipedia.org/wiki/Standard_deviation]
|
42
42
|
# of the values in _array_.
|
43
43
|
def standard_deviation(&block)
|
44
|
-
Math.sqrt(variance(&block))
|
44
|
+
::Math.sqrt(variance(&block))
|
45
45
|
end
|
46
46
|
|
47
47
|
alias_method :std, :standard_deviation
|
@@ -44,17 +44,17 @@ class Array
|
|
44
44
|
# %w[a b c d].to_h #=> { "a" => "b", "c" => "d" }
|
45
45
|
# %w[a b c d].to_h(1) #=> { "a" => 1, "b" => 1, "c" => 1, "d" => 1 }
|
46
46
|
# %w[a b].to_h { |e| e * 2 } #=> { "a" => "aa", "b" => "bb" }
|
47
|
-
def to_hash(value = default =
|
47
|
+
def to_hash(value = default = true)
|
48
48
|
hash = {}
|
49
49
|
|
50
50
|
if block_given?
|
51
|
-
raise ArgumentError,
|
51
|
+
raise ::ArgumentError, 'both block and value argument given' unless default
|
52
52
|
|
53
53
|
each { |element| hash[element] = yield element }
|
54
|
-
elsif default
|
54
|
+
elsif !default
|
55
55
|
each { |element| hash[element] = value }
|
56
56
|
else
|
57
|
-
return Hash[*flatten_once]
|
57
|
+
return ::Hash[*flatten_once]
|
58
58
|
end
|
59
59
|
|
60
60
|
hash
|
@@ -27,12 +27,12 @@
|
|
27
27
|
|
28
28
|
begin
|
29
29
|
require 'rubygems'
|
30
|
-
rescue LoadError
|
30
|
+
rescue ::LoadError
|
31
31
|
end
|
32
32
|
|
33
33
|
begin
|
34
34
|
require 'amatch'
|
35
|
-
rescue LoadError
|
35
|
+
rescue ::LoadError
|
36
36
|
warn "Couldn't load amatch..." if $VERBOSE
|
37
37
|
end
|
38
38
|
|
@@ -55,11 +55,11 @@ module Enumerable
|
|
55
55
|
# - The cost for individual error types (substitution, insertion, deletion)
|
56
56
|
# cannot be adjusted.
|
57
57
|
def agrep(pattern, distance = 0)
|
58
|
-
raise 'Amatch not available!' unless defined?(Amatch)
|
58
|
+
raise 'Amatch not available!' unless defined?(::Amatch)
|
59
59
|
|
60
|
-
pattern = pattern.source if pattern.is_a?(Regexp)
|
60
|
+
pattern = pattern.source if pattern.is_a?(::Regexp)
|
61
61
|
|
62
|
-
amatch = Amatch::Levenshtein.new(pattern)
|
62
|
+
amatch = ::Amatch::Levenshtein.new(pattern)
|
63
63
|
matches = select { |obj| amatch.search(obj.to_s) <= distance }
|
64
64
|
|
65
65
|
block_given? ? matches.map { |match| yield match } : matches
|
@@ -37,7 +37,7 @@ 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 <tt>:===</tt>.
|
40
|
-
def all?(object = default =
|
40
|
+
def all?(object = default = true, operator = :===, &block)
|
41
41
|
_nuggets_original_all?(&_block_for_all_any_extended(object, default, operator, &block))
|
42
42
|
end
|
43
43
|
|
@@ -48,7 +48,7 @@ module Enumerable
|
|
48
48
|
# Adds the ability to pass an +object+ instead of a block, which will then
|
49
49
|
# be tested against each item in _enum_ according to +operator+, defaulting
|
50
50
|
# to <tt>:===</tt>.
|
51
|
-
def any?(object = default =
|
51
|
+
def any?(object = default = true, operator = :===, &block)
|
52
52
|
_nuggets_original_any?(&_block_for_all_any_extended(object, default, operator, &block))
|
53
53
|
end
|
54
54
|
|
@@ -56,8 +56,8 @@ module Enumerable
|
|
56
56
|
|
57
57
|
# Common argument processing for extended versions of #all? and #any?.
|
58
58
|
def _block_for_all_any_extended(object, default, operator, &block)
|
59
|
-
|
60
|
-
raise ArgumentError,
|
59
|
+
unless default
|
60
|
+
raise ::ArgumentError, 'both block and object argument given', caller(1) if block
|
61
61
|
lambda { |*a| object.send(operator, *a) }
|
62
62
|
else
|
63
63
|
block
|
@@ -70,18 +70,18 @@ if $0 == __FILE__
|
|
70
70
|
e = %w[quux quuux quix]
|
71
71
|
p e
|
72
72
|
|
73
|
-
p e.all?(String)
|
74
|
-
p e.any?(Numeric)
|
73
|
+
p e.all?(::String)
|
74
|
+
p e.any?(::Numeric)
|
75
75
|
|
76
76
|
e = [:one, 'c', nil, 88]
|
77
77
|
p e
|
78
78
|
|
79
|
-
p e.all?(Object)
|
80
|
-
p e.any?(NilClass)
|
79
|
+
p e.all?(::Object)
|
80
|
+
p e.any?(::NilClass)
|
81
81
|
|
82
82
|
begin
|
83
|
-
e.any?(NilClass) { |i| i.nil? }
|
84
|
-
rescue ArgumentError => err
|
83
|
+
e.any?(::NilClass) { |i| i.nil? }
|
84
|
+
rescue ::ArgumentError => err
|
85
85
|
puts "#{err.backtrace.first}: #{err} (#{err.class})"
|
86
86
|
end
|
87
87
|
|
@@ -42,7 +42,7 @@ module Enumerable
|
|
42
42
|
# to +by+ (which may be a symbol/string that is sent to each value, or a proc
|
43
43
|
# that receives each value as parameter).
|
44
44
|
def minmax_by(meth, by)
|
45
|
-
_by = by.is_a?(Proc) ? by : lambda { |i| i.send(by) }
|
45
|
+
_by = by.is_a?(::Proc) ? by : lambda { |i| i.send(by) }
|
46
46
|
send(meth) { |a, b| _by[a] <=> _by[b] }
|
47
47
|
end
|
48
48
|
|
@@ -73,7 +73,7 @@ module Enumerable
|
|
73
73
|
#m = minmax_by(meth, what)
|
74
74
|
#what.is_a?(Proc) ? what[m] : m.send(what)
|
75
75
|
|
76
|
-
_what = what.is_a?(Proc) ? what : lambda { |i| i.send(what) }
|
76
|
+
_what = what.is_a?(::Proc) ? what : lambda { |i| i.send(what) }
|
77
77
|
map { |i| _what[i] }.send(meth)
|
78
78
|
|
79
79
|
# Benchmark (:max, :length; enum.size = 20, N = 100_000):
|
@@ -42,8 +42,8 @@ module Nuggets
|
|
42
42
|
self.clear if clear
|
43
43
|
|
44
44
|
env.each { |key, value|
|
45
|
-
key = key.to_s.upcase unless key.is_a?(String)
|
46
|
-
value = value.to_s unless value.is_a?(String)
|
45
|
+
key = key.to_s.upcase unless key.is_a?(::String)
|
46
|
+
value = value.to_s unless value.is_a?(::String)
|
47
47
|
|
48
48
|
self[key] = value
|
49
49
|
}
|
@@ -27,7 +27,7 @@
|
|
27
27
|
|
28
28
|
begin
|
29
29
|
require 'win32console'
|
30
|
-
rescue LoadError
|
30
|
+
rescue ::LoadError
|
31
31
|
end
|
32
32
|
|
33
33
|
module Nuggets
|
@@ -42,8 +42,8 @@ module Nuggets
|
|
42
42
|
self['ENCODING'] || begin
|
43
43
|
lang = self['LANG']
|
44
44
|
lang[/\.(.*)/, 1] if lang
|
45
|
-
end || if defined?(Win32::Console)
|
46
|
-
"CP#{Win32::Console.InputCP}"
|
45
|
+
end || if defined?(::Win32::Console)
|
46
|
+
"CP#{::Win32::Console.InputCP}"
|
47
47
|
elsif ::File::ALT_SEPARATOR
|
48
48
|
cp = %x{chcp}[/:\s*(.*?)\./, 1]
|
49
49
|
"CP#{cp}" if cp
|
@@ -31,7 +31,7 @@ module Nuggets
|
|
31
31
|
class File
|
32
32
|
module WhichMixin
|
33
33
|
|
34
|
-
DEFAULT_EXTENSIONS = [RbConfig::CONFIG['EXEEXT']]
|
34
|
+
DEFAULT_EXTENSIONS = [::RbConfig::CONFIG['EXEEXT']]
|
35
35
|
|
36
36
|
# call-seq:
|
37
37
|
# File.which(executable[, extensions]) => aString or +nil+
|
@@ -44,7 +44,7 @@ module Nuggets
|
|
44
44
|
def which(executable, extensions = DEFAULT_EXTENSIONS)
|
45
45
|
extensions |= ['']
|
46
46
|
|
47
|
-
if env = ENV['PATH']
|
47
|
+
if env = ::ENV['PATH']
|
48
48
|
dirs = env.split(self::PATH_SEPARATOR)
|
49
49
|
dirs.map! { |dir| expand_path(dir) }
|
50
50
|
end
|
data/lib/nuggets/hash/at.rb
CHANGED
data/lib/nuggets/hash/insert.rb
CHANGED
@@ -35,7 +35,7 @@ class Hash
|
|
35
35
|
# overwriting. Uses default Hash#merge or block for merging.
|
36
36
|
def insert(other, &block)
|
37
37
|
block ||= lambda { |key, old_val, new_val|
|
38
|
-
old_val.is_a?(Hash) && new_val.is_a?(Hash) ?
|
38
|
+
old_val.is_a?(::Hash) && new_val.is_a?(::Hash) ?
|
39
39
|
old_val.merge(new_val, &block) : new_val
|
40
40
|
}
|
41
41
|
|
@@ -50,7 +50,7 @@ module Nuggets
|
|
50
50
|
# hash[:foo][:bar][:b] = { :x => 0, :y => 3 }
|
51
51
|
# hash
|
52
52
|
# #=> {:foo=>{:bar=>{:b=>{:y=>3, :x=>0}, :a=>{:y=>2, :x=>1}}}}
|
53
|
-
def nest(depth = 0, value = default =
|
53
|
+
def nest(depth = 0, value = default = true)
|
54
54
|
if depth.zero?
|
55
55
|
if default
|
56
56
|
if block_given?
|
data/lib/nuggets/hash/only.rb
CHANGED
@@ -36,7 +36,7 @@ class Hash
|
|
36
36
|
# Returns the only key/value pair of _hash_. Raises an IndexError if _hash_'s
|
37
37
|
# size is not 1, unless parameter +true+ is passed.
|
38
38
|
def only(relax = size == 1, split = false)
|
39
|
-
raise IndexError, 'not a single-element hash' unless relax
|
39
|
+
raise ::IndexError, 'not a single-element hash' unless relax
|
40
40
|
|
41
41
|
split ? Array(*first) : first
|
42
42
|
end
|
@@ -60,7 +60,7 @@ if $0 == __FILE__
|
|
60
60
|
begin
|
61
61
|
p h.only
|
62
62
|
p h.only_pair
|
63
|
-
rescue IndexError => err
|
63
|
+
rescue ::IndexError => err
|
64
64
|
warn err
|
65
65
|
end
|
66
66
|
|
data/lib/nuggets/io/agrep.rb
CHANGED
data/lib/nuggets/io/modes.rb
CHANGED
@@ -50,13 +50,13 @@ class IO
|
|
50
50
|
when true, false, nil
|
51
51
|
# ok
|
52
52
|
else
|
53
|
-
raise TypeError, "wrong argument type #{binary.class} (expected boolean)"
|
53
|
+
raise ::TypeError, "wrong argument type #{binary.class} (expected boolean)"
|
54
54
|
end
|
55
55
|
else
|
56
|
-
raise ArgumentError, "wrong number of arguments (#{args.size + 1} for 1-2)"
|
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, &::Proc.new)
|
60
60
|
end
|
61
61
|
|
62
62
|
# call-seq:
|
@@ -65,7 +65,7 @@ class IO
|
|
65
65
|
#
|
66
66
|
# Opens +name+ with mode +w+.
|
67
67
|
def write(name, binary = false)
|
68
|
-
open_with_mode(name, 'w', binary, &block_given? ? Proc.new : nil)
|
68
|
+
open_with_mode(name, 'w', binary, &block_given? ? ::Proc.new : nil)
|
69
69
|
end
|
70
70
|
|
71
71
|
# call-seq:
|
@@ -74,7 +74,7 @@ class IO
|
|
74
74
|
#
|
75
75
|
# Opens +name+ with mode +a+.
|
76
76
|
def append(name, binary = false)
|
77
|
-
open_with_mode(name, 'a', binary, &block_given? ? Proc.new : nil)
|
77
|
+
open_with_mode(name, 'a', binary, &block_given? ? ::Proc.new : nil)
|
78
78
|
end
|
79
79
|
|
80
80
|
# call-seq:
|
@@ -83,7 +83,7 @@ class IO
|
|
83
83
|
#
|
84
84
|
# Opens +name+ with mode <tt>r+</tt>.
|
85
85
|
def read_write(name, binary = false)
|
86
|
-
open_with_mode(name, 'r+', binary, &block_given? ? Proc.new : nil)
|
86
|
+
open_with_mode(name, 'r+', binary, &block_given? ? ::Proc.new : nil)
|
87
87
|
end
|
88
88
|
|
89
89
|
# call-seq:
|
@@ -92,7 +92,7 @@ class IO
|
|
92
92
|
#
|
93
93
|
# Opens +name+ with mode <tt>w+</tt>.
|
94
94
|
def write_read(name, binary = false)
|
95
|
-
open_with_mode(name, 'w+', binary, &block_given? ? Proc.new : nil)
|
95
|
+
open_with_mode(name, 'w+', binary, &block_given? ? ::Proc.new : nil)
|
96
96
|
end
|
97
97
|
|
98
98
|
# call-seq:
|
@@ -101,14 +101,14 @@ class IO
|
|
101
101
|
#
|
102
102
|
# Opens +name+ with mode <tt>a+</tt>.
|
103
103
|
def append_read(name, binary = false)
|
104
|
-
open_with_mode(name, 'a+', binary, &block_given? ? Proc.new : nil)
|
104
|
+
open_with_mode(name, 'a+', binary, &block_given? ? ::Proc.new : nil)
|
105
105
|
end
|
106
106
|
|
107
107
|
private
|
108
108
|
|
109
109
|
# Just a helper to DRY things up.
|
110
110
|
def open_with_mode(name, mode, binary = false)
|
111
|
-
open(name, "#{mode}#{'b' if binary}", &block_given? ? Proc.new : nil)
|
111
|
+
open(name, "#{mode}#{'b' if binary}", &block_given? ? ::Proc.new : nil)
|
112
112
|
end
|
113
113
|
|
114
114
|
end
|
@@ -116,7 +116,7 @@ class IO
|
|
116
116
|
end
|
117
117
|
|
118
118
|
if $0 == __FILE__
|
119
|
-
# File.read(__FILE__) { |f| ... }
|
120
|
-
# File.write(__FILE__) { |f| ... }
|
121
|
-
# File.append(__FILE__) { |f| ... }
|
119
|
+
# ::File.read(__FILE__) { |f| ... }
|
120
|
+
# ::File.write(__FILE__) { |f| ... }
|
121
|
+
# ::File.append(__FILE__) { |f| ... }
|
122
122
|
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2011 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 Affero General Public License as published by #
|
14
|
+
# the Free Software Foundation; either version 3 of the License, or (at your #
|
15
|
+
# option) 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 Affero General Public License #
|
20
|
+
# for more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU Affero General Public License #
|
23
|
+
# along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
require 'net/http'
|
29
|
+
|
30
|
+
module Net
|
31
|
+
|
32
|
+
class HTTPResponse
|
33
|
+
def success?; nil end
|
34
|
+
end
|
35
|
+
|
36
|
+
class HTTPInformation
|
37
|
+
# ???
|
38
|
+
end
|
39
|
+
|
40
|
+
class HTTPSuccess
|
41
|
+
def success?; true end
|
42
|
+
end
|
43
|
+
|
44
|
+
class HTTPRedirection
|
45
|
+
def success?; true end
|
46
|
+
end
|
47
|
+
|
48
|
+
class HTTPMultipleChoice
|
49
|
+
def success?; false end
|
50
|
+
end
|
51
|
+
|
52
|
+
class HTTPClientError
|
53
|
+
def success?; false end
|
54
|
+
end
|
55
|
+
|
56
|
+
class HTTPServerError
|
57
|
+
def success?; false end
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
@@ -32,7 +32,7 @@ class Numeric
|
|
32
32
|
#
|
33
33
|
# Converts _num_ into hour, minute, and second portions.
|
34
34
|
def hms
|
35
|
-
raise ArgumentError, "negative duration #{self}" if self < 0
|
35
|
+
raise ::ArgumentError, "negative duration #{self}" if self < 0
|
36
36
|
|
37
37
|
one_minute = 60
|
38
38
|
one_hour = 60 * one_minute
|
@@ -45,7 +45,7 @@ class Numeric
|
|
45
45
|
#
|
46
46
|
# Converts _num_ into year, month, and day portions.
|
47
47
|
def ymd
|
48
|
-
raise ArgumentError, "negative duration #{self}" if self < 0
|
48
|
+
raise ::ArgumentError, "negative duration #{self}" if self < 0
|
49
49
|
|
50
50
|
one_day = 24 * 60 * 60
|
51
51
|
one_month = 30 * one_day
|
@@ -54,15 +54,15 @@ module Nuggets
|
|
54
54
|
}
|
55
55
|
|
56
56
|
# raises TypeError if neither class nor module
|
57
|
-
ObjectSpace.each_object(self) { |obj|
|
57
|
+
::ObjectSpace.each_object(self) { |obj|
|
58
58
|
return obj if self.equal?(obj.singleton_class)
|
59
59
|
}
|
60
60
|
|
61
61
|
# if we got here, it can't be a singleton class
|
62
62
|
# or its singleton object doesn't exist anymore
|
63
|
-
raise TypeError
|
64
|
-
rescue TypeError
|
65
|
-
raise TypeError, 'not a singleton class'
|
63
|
+
raise ::TypeError
|
64
|
+
rescue ::TypeError
|
65
|
+
raise ::TypeError, 'not a singleton class'
|
66
66
|
end
|
67
67
|
|
68
68
|
alias_method :virtual_object, :singleton_object
|
@@ -81,7 +81,7 @@ module Nuggets
|
|
81
81
|
def singleton_class?
|
82
82
|
singleton_object
|
83
83
|
true
|
84
|
-
rescue TypeError
|
84
|
+
rescue ::TypeError
|
85
85
|
false
|
86
86
|
end
|
87
87
|
|
@@ -38,7 +38,7 @@ module Nuggets
|
|
38
38
|
# different environment (= +binding+) at a later point.
|
39
39
|
#
|
40
40
|
# Passes optional arguments +filename+ and +lineno+ on to Kernel#eval.
|
41
|
-
def evaluate(binding = TOPLEVEL_BINDING, filename = nil, lineno = nil)
|
41
|
+
def evaluate(binding = ::TOPLEVEL_BINDING, filename = nil, lineno = nil)
|
42
42
|
buffer = gsub(/\\*"/) { |m| "#{"\\" * m.length}#{m}" }
|
43
43
|
eval(%Q{"#{buffer}"}, binding, filename || __FILE__, lineno || __LINE__)
|
44
44
|
end
|
data/lib/nuggets/string/msub.rb
CHANGED
@@ -52,19 +52,19 @@ class String
|
|
52
52
|
#
|
53
53
|
# Destructive version of #msub.
|
54
54
|
def msub!(*substitutions)
|
55
|
-
options = substitutions.last.is_a?(Hash) ? substitutions.pop : {}
|
56
|
-
binding = options.delete(:__binding__) || Kernel.binding
|
55
|
+
options = substitutions.last.is_a?(::Hash) ? substitutions.pop : {}
|
56
|
+
binding = options.delete(:__binding__) || ::Kernel.binding
|
57
57
|
|
58
58
|
keys, subs, cache = [], [], {}
|
59
59
|
|
60
60
|
substitutions.concat(options.to_a).each { |key, value|
|
61
|
-
key = Regexp.new(Regexp.escape(key)) unless key.is_a?(Regexp)
|
61
|
+
key = ::Regexp.new(::Regexp.escape(key)) unless key.is_a?(::Regexp)
|
62
62
|
|
63
63
|
keys << key
|
64
64
|
subs << [key, value]
|
65
65
|
}
|
66
66
|
|
67
|
-
gsub!(Regexp.union(*keys)) { |match|
|
67
|
+
gsub!(::Regexp.union(*keys)) { |match|
|
68
68
|
cache[match] ||= begin
|
69
69
|
value = subs.find { |key, _| key =~ match }.last
|
70
70
|
|
@@ -95,7 +95,7 @@ if $0 == __FILE__
|
|
95
95
|
p s
|
96
96
|
|
97
97
|
t = '!!!'
|
98
|
-
begin; p s.msub('r' => '???', 'z' => '#{t}'); rescue NameError => err; warn err; end
|
98
|
+
begin; p s.msub('r' => '???', 'z' => '#{t}'); rescue ::NameError => err; warn err; end
|
99
99
|
p s.msub('r' => '???', 'z' => '#{t}', :__binding__ => binding)
|
100
100
|
|
101
101
|
p s.msub(/[A-Z]/ => '#{__match__.downcase}', :__binding__ => binding)
|
data/lib/nuggets/string/nsub.rb
CHANGED
@@ -62,7 +62,7 @@ class String
|
|
62
62
|
|
63
63
|
gsub!(pattern) { |match| (i += 1) <= count ? replacement : match }
|
64
64
|
else
|
65
|
-
raise ArgumentError, "wrong number of arguments (#{args.size} for 2-3)"
|
65
|
+
raise ::ArgumentError, "wrong number of arguments (#{args.size} for 2-3)"
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
@@ -108,13 +108,13 @@ if $0 == __FILE__
|
|
108
108
|
"#{md[1].gsub_with_md(/[ao]/) { |md2| md2[0].upcase }}#{md[2]}"
|
109
109
|
}
|
110
110
|
|
111
|
-
String.gimme_match_data!
|
111
|
+
::String.gimme_match_data!
|
112
112
|
|
113
113
|
p s.gsub(/\w(\w+)(\W*)/) { |m|
|
114
114
|
#p m
|
115
115
|
begin
|
116
116
|
"#{$1.gsub(/[ao]/, 'X')}#{$2}"
|
117
|
-
rescue NoMethodError => err
|
117
|
+
rescue ::NoMethodError => err
|
118
118
|
warn err
|
119
119
|
end
|
120
120
|
}
|