blackwinter-ruby-nuggets 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.
- data/COPYING +676 -0
- data/ChangeLog +5 -0
- data/README +60 -0
- data/Rakefile +20 -0
- data/lib/nuggets/all.rb +34 -0
- data/lib/nuggets/array/combination.rb +86 -0
- data/lib/nuggets/array/flatten_once.rb +68 -0
- data/lib/nuggets/array/format.rb +124 -0
- data/lib/nuggets/array/in_order.rb +62 -0
- data/lib/nuggets/array/monotone.rb +101 -0
- data/lib/nuggets/array/only.rb +56 -0
- data/lib/nuggets/array/rand.rb +45 -0
- data/lib/nuggets/array/shuffle.rb +133 -0
- data/lib/nuggets/array/to_hash.rb +86 -0
- data/lib/nuggets/enumerable/agrep.rb +82 -0
- data/lib/nuggets/enumerable/all_any_extended.rb +99 -0
- data/lib/nuggets/enumerable/minmax.rb +119 -0
- data/lib/nuggets/env/user_encoding.rb +53 -0
- data/lib/nuggets/env/user_home.rb +54 -0
- data/lib/nuggets/file/which.rb +74 -0
- data/lib/nuggets/hash/at.rb +87 -0
- data/lib/nuggets/hash/in_order.rb +52 -0
- data/lib/nuggets/hash/insert.rb +65 -0
- data/lib/nuggets/hash/only.rb +68 -0
- data/lib/nuggets/integer/factorial.rb +74 -0
- data/lib/nuggets/integer/to_binary_s.rb +47 -0
- data/lib/nuggets/io/agrep.rb +43 -0
- data/lib/nuggets/io/modes.rb +133 -0
- data/lib/nuggets/numeric/between.rb +2 -0
- data/lib/nuggets/numeric/duration.rb +109 -0
- data/lib/nuggets/numeric/limit.rb +70 -0
- data/lib/nuggets/numeric/signum.rb +60 -0
- data/lib/nuggets/numeric/to_multiple.rb +68 -0
- data/lib/nuggets/object/blank.rb +119 -0
- data/lib/nuggets/object/boolean.rb +69 -0
- data/lib/nuggets/object/eigenclass.rb +2 -0
- data/lib/nuggets/object/ghost_class.rb +2 -0
- data/lib/nuggets/object/metaclass.rb +2 -0
- data/lib/nuggets/object/msend.rb +55 -0
- data/lib/nuggets/object/singleton_class.rb +150 -0
- data/lib/nuggets/object/uniclass.rb +2 -0
- data/lib/nuggets/object/virtual_class.rb +2 -0
- data/lib/nuggets/proc/bind.rb +68 -0
- data/lib/nuggets/string/capitalize_first.rb +63 -0
- data/lib/nuggets/string/case.rb +104 -0
- data/lib/nuggets/string/evaluate.rb +53 -0
- data/lib/nuggets/string/msub.rb +82 -0
- data/lib/nuggets/string/nsub.rb +80 -0
- data/lib/nuggets/string/sub_with_md.rb +131 -0
- data/lib/nuggets/string/word_wrap.rb +111 -0
- data/lib/nuggets/tempfile/open.rb +54 -0
- data/lib/nuggets/uri/content_type.rb +65 -0
- data/lib/nuggets/uri/exist.rb +63 -0
- data/lib/nuggets/util/added_methods/init.rb +3 -0
- data/lib/nuggets/util/added_methods.rb +407 -0
- data/lib/nuggets/util/ansicolor2css.rb +90 -0
- data/lib/nuggets/util/content_type.rb +104 -0
- data/lib/nuggets/util/dotted_decimal.rb +66 -0
- data/lib/nuggets/util/i18n.rb +143 -0
- data/lib/nuggets/version.rb +27 -0
- data/lib/nuggets.rb +73 -0
- metadata +124 -0
@@ -0,0 +1,109 @@
|
|
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 Numeric
|
29
|
+
|
30
|
+
# call-seq:
|
31
|
+
# num.hms => anArray
|
32
|
+
#
|
33
|
+
# Converts _num_ into hour, minute, and second portions.
|
34
|
+
def hms
|
35
|
+
raise ArgumentError, "negative duration #{self}" if self < 0
|
36
|
+
|
37
|
+
one_minute = 60
|
38
|
+
one_hour = 60 * one_minute
|
39
|
+
|
40
|
+
[((h,) = divmod(one_hour)).last.divmod(one_minute), h].reverse.flatten # *SCNR* ;-)
|
41
|
+
end
|
42
|
+
|
43
|
+
# call-seq:
|
44
|
+
# num.ymd => anArray
|
45
|
+
#
|
46
|
+
# Converts _num_ into year, month, and day portions.
|
47
|
+
def ymd
|
48
|
+
raise ArgumentError, "negative duration #{self}" if self < 0
|
49
|
+
|
50
|
+
one_day = 24 * 60 * 60
|
51
|
+
one_month = 30 * one_day
|
52
|
+
one_year = 365.25 * one_day
|
53
|
+
|
54
|
+
y, m = divmod(one_year)
|
55
|
+
m, d = m.divmod(one_month)
|
56
|
+
|
57
|
+
[y, m, d / one_day]
|
58
|
+
end
|
59
|
+
|
60
|
+
# call-seq:
|
61
|
+
# num.to_hms(precision = 0, labels = %w[h m s]) => aString
|
62
|
+
#
|
63
|
+
# Produces a stringified version of _num_'s time portions (cf. #hms),
|
64
|
+
# with the specified +precision+ for the seconds (treated as floating
|
65
|
+
# point). The individual parts are labelled as specified in the +labels+
|
66
|
+
# parameter (hours, minutes, seconds in that order). Leading parts with
|
67
|
+
# a value of zero are omitted.
|
68
|
+
#
|
69
|
+
# Examples:
|
70
|
+
# 180.to_hms #=> "3m0s"
|
71
|
+
# 180.75.to_hms #=> "3m1s"
|
72
|
+
# 180.75.to_hms(2) #=> "3m0.75s"
|
73
|
+
# 8180.to_hms #=> "2h16m20s"
|
74
|
+
# 8180.to_hms(0, %w[: :]) #=> "2:16:20"
|
75
|
+
def to_hms(precision = 0, labels = %w[h m s], time = hms)
|
76
|
+
h, m, s = time
|
77
|
+
|
78
|
+
h.zero? ? m.zero? ?
|
79
|
+
"%0.#{precision}f#{labels[2]}" % [s] :
|
80
|
+
"%d#{labels[1]}%0.#{precision}f#{labels[2]}" % [m, s] :
|
81
|
+
"%d#{labels[0]}%d#{labels[1]}%0.#{precision}f#{labels[2]}" % [h, m, s]
|
82
|
+
end
|
83
|
+
|
84
|
+
# call-seq:
|
85
|
+
# num.to_ymd(include_hms = false, labels = %w[y m d]) => aString
|
86
|
+
#
|
87
|
+
# Produces a stringified version of _num_'s date portions (cf. #ymd),
|
88
|
+
# analogous to #to_hms. Includes time portions (cf. #hms) if +include_hms+
|
89
|
+
# is true.
|
90
|
+
def to_ymd(include_hms = false, labels = %w[y m d])
|
91
|
+
unless include_hms
|
92
|
+
to_hms(0, labels, ymd)
|
93
|
+
else
|
94
|
+
y, m, d = ymd
|
95
|
+
e = d.truncate
|
96
|
+
|
97
|
+
"#{to_hms(0, labels, [y, m, e])} #{((d - e) * 24 * 60 * 60).to_hms}"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
if $0 == __FILE__
|
104
|
+
[123, 123456789, 0, 0.001, 1.23, 1234.56789].each { |n|
|
105
|
+
p n
|
106
|
+
p [n.hms, n.to_hms, n.to_hms(2)]
|
107
|
+
p [n.ymd, n.to_ymd, n.to_ymd(true)]
|
108
|
+
}
|
109
|
+
end
|
@@ -0,0 +1,70 @@
|
|
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 Numeric
|
29
|
+
|
30
|
+
# call-seq:
|
31
|
+
# num.limit(min, max) => aNumeric
|
32
|
+
#
|
33
|
+
# Returns +min+ if that's larger than _num_, or +max+ if that's smaller than
|
34
|
+
# _num_. Otherwise returns _num_.
|
35
|
+
def limit(min, max)
|
36
|
+
min, max = max, min if max < min
|
37
|
+
|
38
|
+
self.min(min).max(max)
|
39
|
+
end
|
40
|
+
|
41
|
+
alias_method :between, :limit
|
42
|
+
|
43
|
+
# call-seq:
|
44
|
+
# num.min(min) => aNumeric
|
45
|
+
#
|
46
|
+
# Returns _num_ or +min+, whatever is larger.
|
47
|
+
def min(min)
|
48
|
+
self < min ? min : self
|
49
|
+
end
|
50
|
+
|
51
|
+
alias_method :at_least, :min
|
52
|
+
|
53
|
+
# call-seq:
|
54
|
+
# num.max(max) => aNumeric
|
55
|
+
#
|
56
|
+
# Returns _num_ or +max+, whatever is smaller.
|
57
|
+
def max(max)
|
58
|
+
self > max ? max : self
|
59
|
+
end
|
60
|
+
|
61
|
+
alias_method :at_most, :max
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
if $0 == __FILE__
|
66
|
+
[123, -123, 0, 0.001, 1.23, -12.3].each { |n|
|
67
|
+
p n
|
68
|
+
p n.between(0, 10)
|
69
|
+
}
|
70
|
+
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
|
+
class Numeric
|
29
|
+
|
30
|
+
def positive?
|
31
|
+
self > 0
|
32
|
+
end
|
33
|
+
|
34
|
+
def negative?
|
35
|
+
self < 0
|
36
|
+
end
|
37
|
+
|
38
|
+
def non_negative?
|
39
|
+
!negative?
|
40
|
+
end
|
41
|
+
|
42
|
+
# call-seq:
|
43
|
+
# num.signum => -1, 0, 1
|
44
|
+
#
|
45
|
+
# Returns the sign of _num_.
|
46
|
+
def signum
|
47
|
+
positive? ? 1 : negative? ? -1 : 0
|
48
|
+
end
|
49
|
+
|
50
|
+
alias_method :sign, :signum
|
51
|
+
alias_method :sgn, :signum
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
if $0 == __FILE__
|
56
|
+
[123, -123, 0, 0.001, 1.23, -12.3].each { |n|
|
57
|
+
p n
|
58
|
+
p n.sgn
|
59
|
+
}
|
60
|
+
end
|
@@ -0,0 +1,68 @@
|
|
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 Numeric
|
29
|
+
|
30
|
+
# call-seq:
|
31
|
+
# num.to_multiple_of(target, what) => aNumeric
|
32
|
+
#
|
33
|
+
# Returns the nearest multiple of +target+ according to +what+.
|
34
|
+
def to_multiple_of(target, what = :round)
|
35
|
+
target.zero? ? self : (to_f / target).send(what) * target
|
36
|
+
end
|
37
|
+
|
38
|
+
# call-seq:
|
39
|
+
# num.round_to(target) => aNumeric
|
40
|
+
#
|
41
|
+
# Rounds _num_ to the nearest multiple of +target+.
|
42
|
+
def round_to(target)
|
43
|
+
to_multiple_of(target, :round)
|
44
|
+
end
|
45
|
+
|
46
|
+
# call-seq:
|
47
|
+
# num.floor_to(target) => aNumeric
|
48
|
+
#
|
49
|
+
# Returns the largest multiple of +target+ less than or equal to _num_.
|
50
|
+
def floor_to(target)
|
51
|
+
to_multiple_of(target, :floor)
|
52
|
+
end
|
53
|
+
|
54
|
+
# call-seq:
|
55
|
+
# num.ceil_to(target) => aNumeric
|
56
|
+
#
|
57
|
+
# Returns the smallest multiple of +target+ greater than or equal to _num_.
|
58
|
+
def ceil_to(target)
|
59
|
+
to_multiple_of(target, :ceil)
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
if $0 == __FILE__
|
65
|
+
[123, -123, 0, 0.001, 5.67, -12.3].each { |n|
|
66
|
+
p [n, n.round_to(10), n.floor_to(10), n.ceil_to(10)]
|
67
|
+
}
|
68
|
+
end
|
@@ -0,0 +1,119 @@
|
|
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
|
+
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
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
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
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
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
|
99
|
+
end
|
100
|
+
|
101
|
+
if $0 == __FILE__
|
102
|
+
['', ' ', 's', 0, 1, nil, true, false, [], [nil], {}].each { |o|
|
103
|
+
p o
|
104
|
+
p o.blank?
|
105
|
+
p o.void?
|
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
|
@@ -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
|
@@ -0,0 +1,55 @@
|
|
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.msend(*messages) => anArray
|
32
|
+
#
|
33
|
+
# Sends _object_ multiple +messages+ and returns an array of the individual
|
34
|
+
# return values.
|
35
|
+
def msend(*messages)
|
36
|
+
hash = messages.last.is_a?(Hash) ? messages.pop : {}
|
37
|
+
(messages + hash.to_a).map { |msg| send *msg.is_a?(Array) ? msg : [msg] }
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
if $0 == __FILE__
|
43
|
+
o = 'foo bar'
|
44
|
+
p o
|
45
|
+
p o.msend(:length, :reverse)
|
46
|
+
|
47
|
+
o = 42
|
48
|
+
p o
|
49
|
+
p o.msend(:to_s, :* => 2)
|
50
|
+
p o.msend([:to_s, 2], '-@')
|
51
|
+
|
52
|
+
o = Time.now
|
53
|
+
p o
|
54
|
+
p o.msend(:year, :month, :day)
|
55
|
+
end
|