ruby-nuggets 0.0.1.126

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.
@@ -0,0 +1,101 @@
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 Array
29
+
30
+ # call-seq:
31
+ # array.monotone?(op) => true or false
32
+ #
33
+ # Check whether _array_ is monotone according to operator +op+.
34
+ def monotone?(op)
35
+ inject { |a, b|
36
+ return false unless a.send(op, b)
37
+ b
38
+ }
39
+
40
+ true
41
+ end
42
+ alias_method :monotonic?, :monotone?
43
+
44
+ # call-seq:
45
+ # array.ascending? => true or false
46
+ #
47
+ # Check whether _array_ is (strictly) ascending.
48
+ def ascending?(strict = false)
49
+ monotone?(strict ? :< : :<=)
50
+ end
51
+ alias_method :increasing?, :ascending?
52
+
53
+ # call-seq:
54
+ # array.strictly_ascending? => true or false
55
+ #
56
+ # Check whether _array_ is strictly ascending.
57
+ def strictly_ascending?
58
+ ascending?(true)
59
+ end
60
+ alias_method :strictly_increasing?, :strictly_ascending?
61
+
62
+ # call-seq:
63
+ # array.descending? => true or false
64
+ #
65
+ # Check whether _array_ is (strictly) descending.
66
+ def descending?(strict = false)
67
+ monotone?(strict ? :> : :>=)
68
+ end
69
+ alias_method :decreasing?, :descending?
70
+
71
+ # call-seq:
72
+ # array.strictly_descending? => true or false
73
+ #
74
+ # Check whether _array_ is strictly descending.
75
+ def strictly_descending?
76
+ descending?(true)
77
+ end
78
+ alias_method :strictly_decreasing?, :strictly_descending?
79
+
80
+ end
81
+
82
+ if $0 == __FILE__
83
+ a = [1, 2, 3, 4]
84
+ p a
85
+
86
+ p a.ascending? # => true
87
+ p a.strictly_ascending? # => true
88
+ p a.descending? # => false
89
+
90
+ b = [1, 2, 4, 3]
91
+ p b
92
+
93
+ p b.ascending? # => false
94
+ p b.descending? # => false
95
+
96
+ c = [1, 2, 4, 4]
97
+ p c
98
+
99
+ p c.ascending? # => true
100
+ p c.strictly_ascending? # => false
101
+ end
@@ -0,0 +1,45 @@
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 Array
29
+
30
+ # call-seq:
31
+ # array.rand => anItem
32
+ #
33
+ # Randomly pick an item from _array_.
34
+ def rand
35
+ at(Kernel.rand(size))
36
+ end
37
+
38
+ end
39
+
40
+ if $0 == __FILE__
41
+ a = %w[a b c d]
42
+ p a
43
+ p a.rand
44
+ p a.rand
45
+ end
@@ -0,0 +1,57 @@
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 Array
29
+
30
+ # call-seq:
31
+ # array.shuffle => new_array
32
+ #
33
+ # Shuffles _array_ in random order.
34
+ def shuffle
35
+ sort_by { Kernel.rand }
36
+ end
37
+
38
+ # call-seq:
39
+ # array.shuffle! => array
40
+ #
41
+ # Destructive version of #shuffle.
42
+ def shuffle!
43
+ replace shuffle
44
+ end
45
+
46
+ end
47
+
48
+ if $0 == __FILE__
49
+ a = %w[1 2 3 4 5 6 7 8]
50
+ p a
51
+
52
+ p a.shuffle
53
+ p a.shuffle
54
+
55
+ a.shuffle!
56
+ p a
57
+ 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 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
+ require File.join(File.dirname(__FILE__), 'flatten_once')
29
+
30
+ class Array
31
+
32
+ # call-seq:
33
+ # array.to_h => aHash
34
+ # array.to_h(value) => aHash
35
+ #
36
+ # If no +value+ is given, converts _array_, being an array of two-element
37
+ # arrays, into a hash, preserving sub-arrays. Otherwise, maps each element
38
+ # of _array_ to +value+.
39
+ #
40
+ # Examples:
41
+ # %w[a b c d].to_h # => { "a" => "b", "c" => "d" }
42
+ # %w[a b c d].to_h(1) # => { "a" => 1, "b" => 1, "c" => 1, "d" => 1 }
43
+ def to_hash(value = default = Object.new)
44
+ if value == default
45
+ Hash[*flatten_once]
46
+ else
47
+ inject({}) { |hash, element|
48
+ hash.update(element => value)
49
+ }
50
+ end
51
+ end
52
+ alias_method :to_h, :to_hash
53
+
54
+ end
55
+
56
+ if $0 == __FILE__
57
+ a = [[:a, 1], [:b, 2], [:c, 3]]
58
+ p a
59
+ p a.to_h
60
+
61
+ b = [[:a, [1, 2]], [:b, 3], [[:c, :d], [4, [5, 6]]]]
62
+ p b
63
+ p b.to_h
64
+
65
+ c = %w[a b c d]
66
+ p c
67
+ p c.to_h(1)
68
+ end
@@ -0,0 +1,73 @@
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 Hash
29
+
30
+ # call-seq:
31
+ # hash.insert(key, value) => new_hash
32
+ # hash.insert(key, value) { |old_value, value| ... } => new_hash
33
+ #
34
+ # Inserts +value+ into _hash_ at +key+, while merging existing values at
35
+ # +key+ instead of just overwriting. Uses default Hash#merge or block for
36
+ # merging.
37
+ def insert(key, value, relax = true, &block)
38
+ dup.insert!(key, value, relax, &block) || dup
39
+ end
40
+
41
+ # call-seq:
42
+ # hash.insert!(key, value) => hash
43
+ # hash.insert!(key, value) { |old_value, value| ... } => hash
44
+ #
45
+ # Destructive version of #insert.
46
+ def insert!(key, value, relax = true, &block)
47
+ block ||= lambda { |old_val, val| old_val.merge(val) }
48
+
49
+ self[key] = begin
50
+ block[self[key], value]
51
+ rescue NoMethodError, TypeError => err
52
+ unless relax
53
+ raise err
54
+ else
55
+ value
56
+ end
57
+ end
58
+
59
+ self
60
+ end
61
+
62
+ end
63
+
64
+ if $0 == __FILE__
65
+ h = { :a => 0, :b => { :b1 => 1, :b2 => 2 } }
66
+ p h
67
+
68
+ p h.insert(:a, -1)
69
+ p h.insert(:b, :b3 => 3)
70
+
71
+ h.insert!(:b, :b0 => 0)
72
+ p h
73
+ end
@@ -0,0 +1,112 @@
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
+ next unless respond_to?(modifier)
46
+
47
+ if modifier.to_s[-1] == ??
48
+ return true if send(modifier)
49
+ else
50
+ return true if send(modifier).blank?
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 any 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 any of _hash_'s values are themselves vain.
88
+ def vain?
89
+ blank? { |h| h.delete_if { |k, v| v.vain? } }
90
+ end
91
+
92
+ end
93
+
94
+ if $0 == __FILE__
95
+ ['', ' ', 's', 0, 1, nil, true, false, [], [nil], {}].each { |o|
96
+ p o
97
+ p o.blank?
98
+ p o.void?
99
+ }
100
+
101
+ o = ['', [], [nil], {}]
102
+ p o
103
+ p o.blank?
104
+ p o.void?
105
+ p o.vain?
106
+
107
+ o = { :x => nil, :y => [], :z => { :zz => nil } }
108
+ p o
109
+ p o.blank?
110
+ p o.void?
111
+ p o.vain?
112
+ end