ruby-nuggets 0.4.5 → 0.5.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.
- data/README +1 -1
- data/Rakefile +7 -6
- data/lib/nuggets/all.rb +2 -0
- data/lib/nuggets/all_mixins.rb +34 -0
- data/lib/nuggets/enumerable/all_any_extended.rb +9 -14
- data/lib/nuggets/enumerable/minmax.rb +5 -0
- data/lib/nuggets/file/which.rb +2 -71
- data/lib/nuggets/file/which_mixin.rb +60 -0
- data/lib/nuggets/integer/length.rb +5 -0
- data/lib/nuggets/integer/length_mixin.rb +50 -0
- data/lib/nuggets/object/blank.rb +10 -109
- data/lib/nuggets/object/blank_mixin.rb +100 -0
- data/lib/nuggets/object/boolean.rb +2 -66
- data/lib/nuggets/object/boolean_mixin.rb +62 -0
- data/lib/nuggets/object/msend.rb +2 -52
- data/lib/nuggets/object/msend_mixin.rb +44 -0
- data/lib/nuggets/object/silence.rb +5 -0
- data/lib/nuggets/object/silence_mixin.rb +45 -0
- data/lib/nuggets/object/singleton_class.rb +2 -149
- data/lib/nuggets/object/singleton_class_mixin.rb +96 -0
- data/lib/nuggets/proc/bind.rb +2 -65
- data/lib/nuggets/proc/bind_mixin.rb +52 -0
- data/lib/nuggets/tempfile/open.rb +21 -17
- data/lib/nuggets/uri/content_type.rb +2 -62
- data/lib/nuggets/uri/content_type_mixin.rb +47 -0
- data/lib/nuggets/uri/exist.rb +2 -60
- data/lib/nuggets/uri/exist_mixin.rb +48 -0
- data/lib/nuggets/version.rb +2 -2
- data/lib/nuggets.rb +2 -2
- metadata +17 -4
data/README
CHANGED
data/Rakefile
CHANGED
@@ -19,12 +19,13 @@ Hen.lay! {{
|
|
19
19
|
}
|
20
20
|
}}
|
21
21
|
|
22
|
-
desc "Run all
|
23
|
-
task :
|
24
|
-
|
22
|
+
desc "Run all specs in isolation"
|
23
|
+
task 'spec:isolated' do
|
24
|
+
ARGV.delete('spec:isolated')
|
25
|
+
ARGV.unshift('spec')
|
25
26
|
|
26
|
-
Dir['
|
27
|
-
|
28
|
-
system(
|
27
|
+
Dir['spec/*/**/*_spec.rb'].each { |spec|
|
28
|
+
ENV['SPEC'] = spec
|
29
|
+
system($0, *ARGV)
|
29
30
|
}
|
30
31
|
end
|
data/lib/nuggets/all.rb
CHANGED
@@ -29,6 +29,8 @@ base = File.dirname(__FILE__)
|
|
29
29
|
base_re = Regexp.escape(base)
|
30
30
|
|
31
31
|
Dir[File.join(base, %w[* ** *.rb])].sort.each { |path|
|
32
|
+
next if path =~ /_mixin\.rb\z/
|
33
|
+
|
32
34
|
ext_re = Regexp.escape(File.extname(path))
|
33
35
|
require path.sub(/#{base_re}(.*)#{ext_re}/, 'nuggets\1')
|
34
36
|
}
|
@@ -0,0 +1,34 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2009 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
|
+
base = File.dirname(__FILE__)
|
29
|
+
base_re = Regexp.escape(base)
|
30
|
+
|
31
|
+
Dir[File.join(base, %w[* ** *_mixin.rb])].sort.each { |path|
|
32
|
+
ext_re = Regexp.escape(File.extname(path))
|
33
|
+
require path.sub(/#{base_re}(.*)#{ext_re}/, 'nuggets\1')
|
34
|
+
}
|
@@ -37,10 +37,8 @@ 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?(&
|
42
|
-
_block_for_all_any_extended(object, default, operator) { |*a| yield(*a) } :
|
43
|
-
_block_for_all_any_extended(object, default, operator))
|
40
|
+
def all?(object = default = Object.new, operator = :===, &block)
|
41
|
+
_nuggets_original_all?(&_block_for_all_any_extended(object, default, operator, &block))
|
44
42
|
end
|
45
43
|
|
46
44
|
# call-seq:
|
@@ -50,22 +48,19 @@ module Enumerable
|
|
50
48
|
# Adds the ability to pass an +object+ instead of a block, which will then
|
51
49
|
# be tested against each item in _enum_ according to +operator+, defaulting
|
52
50
|
# to :===.
|
53
|
-
def any?(object = default = Object.new, operator =
|
54
|
-
_nuggets_original_any?(&
|
55
|
-
_block_for_all_any_extended(object, default, operator) { |*a| yield(*a) } :
|
56
|
-
_block_for_all_any_extended(object, default, operator))
|
51
|
+
def any?(object = default = Object.new, operator = :===, &block)
|
52
|
+
_nuggets_original_any?(&_block_for_all_any_extended(object, default, operator, &block))
|
57
53
|
end
|
58
54
|
|
59
55
|
private
|
60
56
|
|
61
57
|
# Common argument processing for extended versions of #all? and #any?.
|
62
|
-
def _block_for_all_any_extended(object, default, operator)
|
58
|
+
def _block_for_all_any_extended(object, default, operator, &block)
|
63
59
|
if default.nil?
|
64
|
-
raise ArgumentError, "both block and object argument given", caller(1) if
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
lambda { |item| yield item }
|
60
|
+
raise ArgumentError, "both block and object argument given", caller(1) if block
|
61
|
+
lambda { |*a| object.send(operator, *a) }
|
62
|
+
else
|
63
|
+
block
|
69
64
|
end
|
70
65
|
end
|
71
66
|
|
@@ -27,6 +27,11 @@
|
|
27
27
|
|
28
28
|
module Enumerable
|
29
29
|
|
30
|
+
alias_method :_nuggets_original_minmax_by, :minmax_by if method_defined?(:minmax_by)
|
31
|
+
alias_method :_nuggets_original_min_by, :min_by if method_defined?(:min_by)
|
32
|
+
alias_method :_nuggets_original_max_by, :max_by if method_defined?(:max_by)
|
33
|
+
alias_method :_nuggets_original_minmax, :minmax if method_defined?(:minmax)
|
34
|
+
|
30
35
|
alias_method :_nuggets_original_max, :max
|
31
36
|
alias_method :_nuggets_original_min, :min
|
32
37
|
|
data/lib/nuggets/file/which.rb
CHANGED
@@ -1,74 +1,5 @@
|
|
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
|
-
#++
|
1
|
+
require 'nuggets/file/which_mixin'
|
27
2
|
|
28
3
|
class File
|
29
|
-
|
30
|
-
class << self
|
31
|
-
|
32
|
-
# call-seq:
|
33
|
-
# File.which(executable) => aString or nil
|
34
|
-
#
|
35
|
-
# Returns the full path to +executable+, or +nil+ if not found in PATH.
|
36
|
-
# Inspired by Gnuplot.which -- thx, Gordon!
|
37
|
-
def which(executable)
|
38
|
-
return executable if executable?(executable)
|
39
|
-
|
40
|
-
if path = ENV['PATH']
|
41
|
-
path.split(PATH_SEPARATOR).each { |dir|
|
42
|
-
candidate = join(expand_path(dir), executable)
|
43
|
-
return candidate if executable?(candidate)
|
44
|
-
}
|
45
|
-
end
|
46
|
-
|
47
|
-
nil
|
48
|
-
end
|
49
|
-
|
50
|
-
# call-seq:
|
51
|
-
# File.which_command(commands) => aString or nil
|
52
|
-
#
|
53
|
-
# Returns the first of +commands+ that is executable.
|
54
|
-
def which_command(commands)
|
55
|
-
commands.find { |command| which(command[/\S+/]) }
|
56
|
-
end
|
57
|
-
|
58
|
-
end
|
59
|
-
|
60
|
-
end
|
61
|
-
|
62
|
-
if $0 == __FILE__
|
63
|
-
%w[cat dog rat gcc /usr/bin/X11/gcc].each { |e|
|
64
|
-
p [e, File.which(e)]
|
65
|
-
}
|
66
|
-
|
67
|
-
c = [
|
68
|
-
'unison --args source target',
|
69
|
-
'rsync --args source target',
|
70
|
-
'scp --args source target'
|
71
|
-
]
|
72
|
-
p c
|
73
|
-
p File.which_command(c)
|
4
|
+
extend Nuggets::File::WhichMixin
|
74
5
|
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-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
|
+
module Nuggets
|
29
|
+
class File
|
30
|
+
module WhichMixin
|
31
|
+
|
32
|
+
# call-seq:
|
33
|
+
# File.which(executable) => aString or nil
|
34
|
+
#
|
35
|
+
# Returns the full path to +executable+, or +nil+ if not found in PATH.
|
36
|
+
# Inspired by Gnuplot.which -- thx, Gordon!
|
37
|
+
def which(executable)
|
38
|
+
return executable if executable?(executable)
|
39
|
+
|
40
|
+
if path = ENV['PATH']
|
41
|
+
path.split(::File::PATH_SEPARATOR).each { |dir|
|
42
|
+
candidate = join(expand_path(dir), executable)
|
43
|
+
return candidate if executable?(candidate)
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
nil
|
48
|
+
end
|
49
|
+
|
50
|
+
# call-seq:
|
51
|
+
# File.which_command(commands) => aString or nil
|
52
|
+
#
|
53
|
+
# Returns the first of +commands+ that is executable.
|
54
|
+
def which_command(commands)
|
55
|
+
commands.find { |command| which(command[/\S+/]) }
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2009 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
|
+
module Nuggets
|
29
|
+
class Integer
|
30
|
+
module LengthMixin
|
31
|
+
|
32
|
+
# call-seq:
|
33
|
+
# int.length => anInteger
|
34
|
+
#
|
35
|
+
# Returns the length of _int_.
|
36
|
+
def length
|
37
|
+
to_s.length
|
38
|
+
end
|
39
|
+
|
40
|
+
# call-seq:
|
41
|
+
# int.digit_count => anInteger
|
42
|
+
#
|
43
|
+
# Returns the digit count of _int_. [ruby-core:22383]
|
44
|
+
def digit_count
|
45
|
+
abs.length
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lib/nuggets/object/blank.rb
CHANGED
@@ -1,119 +1,20 @@
|
|
1
|
-
|
2
|
-
###############################################################################
|
3
|
-
# #
|
4
|
-
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
-
# language. #
|
6
|
-
# #
|
7
|
-
# Copyright (C) 2007 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
|
-
#++
|
1
|
+
require 'nuggets/object/blank_mixin'
|
27
2
|
|
28
3
|
class Object
|
29
|
-
|
30
|
-
# call-seq:
|
31
|
-
# object.blank? => true or false
|
32
|
-
#
|
33
|
-
# Basically a short-cut to <tt>object.nil? || object.empty?</tt>.
|
34
|
-
def blank?(*modifiers)
|
35
|
-
if block_given?
|
36
|
-
return true if yield(dup).blank?
|
37
|
-
end
|
38
|
-
|
39
|
-
if modifiers.empty?
|
40
|
-
respond_to?(:empty?) ? empty? : !self
|
41
|
-
else
|
42
|
-
return true if blank?
|
43
|
-
|
44
|
-
modifiers.each { |modifier|
|
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
|
-
end
|
52
|
-
}
|
53
|
-
|
54
|
-
false
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
# call-seq:
|
59
|
-
# object.void? => true or false
|
60
|
-
#
|
61
|
-
# Adds white-space strings, 0 and arrays of +nil+ objects to the list of
|
62
|
-
# blank objects.
|
63
|
-
def void?
|
64
|
-
blank?(:zero?, :strip, :compact)
|
65
|
-
end
|
66
|
-
alias_method :vain?, :void?
|
67
|
-
|
4
|
+
include Nuggets::Object::BlankMixin
|
68
5
|
end
|
69
6
|
|
70
7
|
class Array
|
71
|
-
|
72
|
-
# call-seq:
|
73
|
-
# array.vain? => true or false
|
74
|
-
#
|
75
|
-
# Returns true if all of _array_'s elements are themselves vain.
|
76
|
-
def vain?
|
77
|
-
blank? { |a| a.delete_if { |i| i.vain? } }
|
78
|
-
end
|
79
|
-
|
8
|
+
include Nuggets::Array::BlankMixin
|
80
9
|
end
|
81
10
|
|
82
11
|
class Hash
|
83
|
-
|
84
|
-
# call-seq:
|
85
|
-
# hash.vain? => true or false
|
86
|
-
#
|
87
|
-
# Returns true if all of _hash_'s values are themselves vain.
|
88
|
-
def vain?
|
89
|
-
blank? { |h| h.delete_if { |_, v| v.vain? } }
|
90
|
-
end
|
91
|
-
|
92
|
-
end
|
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
|
12
|
+
include Nuggets::Hash::BlankMixin
|
99
13
|
end
|
100
14
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
o = ['', [], [nil], {}]
|
109
|
-
p o
|
110
|
-
p o.blank?
|
111
|
-
p o.void?
|
112
|
-
p o.vain?
|
113
|
-
|
114
|
-
o = { :x => nil, :y => [], :z => { :zz => nil } }
|
115
|
-
p o
|
116
|
-
p o.blank?
|
117
|
-
p o.void?
|
118
|
-
p o.vain?
|
119
|
-
end
|
15
|
+
#class String
|
16
|
+
# if public_instance_methods(false).include?(method = 'blank?')
|
17
|
+
# # remove incompatible implementation added by utility_belt/language_greps.rb
|
18
|
+
# remove_method method
|
19
|
+
# end
|
20
|
+
#end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007 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
|
+
module Nuggets
|
29
|
+
class Object
|
30
|
+
module BlankMixin
|
31
|
+
|
32
|
+
# call-seq:
|
33
|
+
# object.blank? => true or false
|
34
|
+
#
|
35
|
+
# Basically a short-cut to <tt>object.nil? || object.empty?</tt>.
|
36
|
+
def blank?(*modifiers)
|
37
|
+
if block_given?
|
38
|
+
return true if yield(dup).blank?
|
39
|
+
end
|
40
|
+
|
41
|
+
if modifiers.empty?
|
42
|
+
respond_to?(:empty?) ? empty? : !self
|
43
|
+
else
|
44
|
+
return true if blank?
|
45
|
+
|
46
|
+
modifiers.each { |modifier|
|
47
|
+
if respond_to?(modifier)
|
48
|
+
if modifier.to_s =~ /\?\z/
|
49
|
+
return true if send(modifier)
|
50
|
+
else
|
51
|
+
return true if send(modifier).blank?
|
52
|
+
end
|
53
|
+
end
|
54
|
+
}
|
55
|
+
|
56
|
+
false
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# call-seq:
|
61
|
+
# object.void? => true or false
|
62
|
+
#
|
63
|
+
# Adds white-space strings, 0 and arrays of +nil+ objects to the list of
|
64
|
+
# blank objects.
|
65
|
+
def void?
|
66
|
+
blank?(:zero?, :strip, :compact)
|
67
|
+
end
|
68
|
+
alias_method :vain?, :void?
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class Array
|
74
|
+
module BlankMixin
|
75
|
+
|
76
|
+
# call-seq:
|
77
|
+
# array.vain? => true or false
|
78
|
+
#
|
79
|
+
# Returns true if all of _array_'s elements are themselves vain.
|
80
|
+
def vain?
|
81
|
+
blank? { |a| a.delete_if { |i| i.vain? } }
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
class Hash
|
88
|
+
module BlankMixin
|
89
|
+
|
90
|
+
# call-seq:
|
91
|
+
# hash.vain? => true or false
|
92
|
+
#
|
93
|
+
# Returns true if all of _hash_'s values are themselves vain.
|
94
|
+
def vain?
|
95
|
+
blank? { |h| h.delete_if { |_, v| v.vain? } }
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -1,69 +1,5 @@
|
|
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
|
-
#++
|
1
|
+
require 'nuggets/object/boolean_mixin'
|
27
2
|
|
28
3
|
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
|
-
}
|
4
|
+
include Nuggets::Object::BooleanMixin
|
69
5
|
end
|