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.
- data/COPYING +676 -0
- data/ChangeLog +5 -0
- data/HEADER +27 -0
- data/README +32 -0
- data/Rakefile +22 -0
- data/lib/nuggets/all.rb +30 -0
- data/lib/nuggets/array/combination.rb +86 -0
- data/lib/nuggets/array/flatten_once.rb +64 -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/rand.rb +45 -0
- data/lib/nuggets/array/shuffle.rb +57 -0
- data/lib/nuggets/array/to_hash.rb +68 -0
- data/lib/nuggets/hash/insert.rb +73 -0
- data/lib/nuggets/object/blank.rb +112 -0
- data/lib/nuggets/object/singleton_class.rb +46 -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 +82 -0
- data/lib/nuggets/string/word_wrap.rb +111 -0
- data/lib/nuggets/util/i18n.rb +143 -0
- data/lib/nuggets/version.rb +27 -0
- metadata +71 -0
data/ChangeLog
ADDED
data/HEADER
ADDED
@@ -0,0 +1,27 @@
|
|
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
|
+
|
data/README
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
= ruby-nuggets - Some extensions to the Ruby programming language
|
2
|
+
|
3
|
+
== VERSION
|
4
|
+
|
5
|
+
This documentation refers to ruby-nuggets version 0.0.1
|
6
|
+
|
7
|
+
|
8
|
+
== DESCRIPTION
|
9
|
+
|
10
|
+
TODO: well, the description... ;-)
|
11
|
+
|
12
|
+
|
13
|
+
== AUTHORS
|
14
|
+
|
15
|
+
* Jens Wille <mailto:jens.wille@uni-koeln.de>
|
16
|
+
|
17
|
+
|
18
|
+
== LICENSE AND COPYRIGHT
|
19
|
+
|
20
|
+
Copyright (C) 2007 Jens Wille
|
21
|
+
|
22
|
+
ruby-nuggets is free software: you can redistribute it and/or modify it under
|
23
|
+
the terms of the GNU General Public License as published by the Free Software
|
24
|
+
Foundation, either version 3 of the License, or (at your option) any later
|
25
|
+
version.
|
26
|
+
|
27
|
+
ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT ANY
|
28
|
+
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
29
|
+
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
30
|
+
|
31
|
+
You should have received a copy of the GNU General Public License along with
|
32
|
+
ruby-nuggets. If not, see <http://www.gnu.org/licenses/>.
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# Utilizes global rake-tasks: alias rake="rake -r rake -R /path/to/rakelibdir"
|
2
|
+
# (Base tasks at <http://prometheus.khi.uni-koeln.de/svn/scratch/rake-tasks/>)
|
3
|
+
|
4
|
+
require 'lib/nuggets/version'
|
5
|
+
|
6
|
+
FILES = FileList['lib/**/*.rb'].to_a
|
7
|
+
RDOCS = %w[README COPYING ChangeLog]
|
8
|
+
OTHER = FileList['[A-Z]*'].to_a
|
9
|
+
|
10
|
+
task(:doc_spec) {{
|
11
|
+
:title => 'ruby-nuggets Application documentation',
|
12
|
+
:rdoc_files => RDOCS + FILES
|
13
|
+
}}
|
14
|
+
|
15
|
+
task(:gem_spec) {{
|
16
|
+
:name => 'ruby-nuggets',
|
17
|
+
:version => Nuggets::VERSION,
|
18
|
+
:summary => 'Some extensions to the Ruby programming language',
|
19
|
+
:files => FILES + OTHER,
|
20
|
+
:require_path => 'lib',
|
21
|
+
:extra_rdoc_files => RDOCS
|
22
|
+
}}
|
data/lib/nuggets/all.rb
ADDED
@@ -0,0 +1,30 @@
|
|
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
|
+
Dir[__FILE__.sub(/\.rb\z/, '/**/*.rb')].sort.each { |rb|
|
29
|
+
require rb
|
30
|
+
}
|
@@ -0,0 +1,86 @@
|
|
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.comb(n, ...) => new_array
|
32
|
+
# array.comb(n, ...) { |combination| ... } => new_array
|
33
|
+
#
|
34
|
+
# Returns an array of arrays of each possible +n+-combination of _array_ for each
|
35
|
+
# given +n+. If a block is given, each +combination+ is yielded to it. Based on
|
36
|
+
# <http://blade.nagaokaut.ac.jp/~sinara/ruby/math/combinatorics/array-comb.rb>.
|
37
|
+
def comb(*sizes)
|
38
|
+
# If no sizes are given, produce all!
|
39
|
+
sizes = (0..size).to_a.reverse if sizes.empty?
|
40
|
+
|
41
|
+
# Container for our combinations
|
42
|
+
combinations = []
|
43
|
+
|
44
|
+
# Collect combinations and, optionally, yield to block.
|
45
|
+
collect_and_yield = lambda { |combination|
|
46
|
+
combinations << combination
|
47
|
+
|
48
|
+
yield(combination) if block_given?
|
49
|
+
}
|
50
|
+
|
51
|
+
sizes.each { |n|
|
52
|
+
case n
|
53
|
+
when 0 # Short-cut (breaks recursion)
|
54
|
+
collect_and_yield[[]]
|
55
|
+
when 1..size # Ignore out-of-range values
|
56
|
+
self[1..-1].comb(n - 1) { |combination|
|
57
|
+
collect_and_yield[combination.unshift(first)]
|
58
|
+
}
|
59
|
+
self[1..-1].comb(n) { |combination|
|
60
|
+
collect_and_yield[combination]
|
61
|
+
}
|
62
|
+
end
|
63
|
+
}
|
64
|
+
|
65
|
+
# Anyway, return what we've found...
|
66
|
+
combinations
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
if $0 == __FILE__
|
72
|
+
a = %w[a b c d]
|
73
|
+
p a
|
74
|
+
|
75
|
+
p a.comb(3)
|
76
|
+
a.comb(3) { |x|
|
77
|
+
p x
|
78
|
+
}
|
79
|
+
|
80
|
+
p a.comb(4, 2, 4)
|
81
|
+
|
82
|
+
p a.comb
|
83
|
+
a.comb { |x|
|
84
|
+
p x
|
85
|
+
}
|
86
|
+
end
|
@@ -0,0 +1,64 @@
|
|
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.flatten_once => new_array
|
32
|
+
#
|
33
|
+
# Flatten _array_ by _one_ level only.
|
34
|
+
def flatten_once
|
35
|
+
inject([]) { |flat, element|
|
36
|
+
case element
|
37
|
+
when Array
|
38
|
+
flat + element
|
39
|
+
else
|
40
|
+
flat << element
|
41
|
+
end
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
# call-seq:
|
46
|
+
# array.flatten_once! => array
|
47
|
+
#
|
48
|
+
# Destructive version of #flatten_once.
|
49
|
+
def flatten_once!
|
50
|
+
replace flatten_once
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
if $0 == __FILE__
|
56
|
+
a = [1, 2, [3, 4, 5], 6, [7, [8, 9]]]
|
57
|
+
p a
|
58
|
+
|
59
|
+
p a.flatten
|
60
|
+
p a.flatten_once
|
61
|
+
|
62
|
+
a.flatten_once!
|
63
|
+
p a
|
64
|
+
end
|
@@ -0,0 +1,124 @@
|
|
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__), 'combination')
|
29
|
+
|
30
|
+
class Array
|
31
|
+
|
32
|
+
# call-seq:
|
33
|
+
# array % other_array => aString
|
34
|
+
# array % str => aString
|
35
|
+
#
|
36
|
+
# Format--Uses the first string in _array_ for which the corresponding
|
37
|
+
# combination of _other_array_ does not contain blank elements as a format
|
38
|
+
# specification, and returns the result of applying it to that combination
|
39
|
+
# (cf. String#%). Returns an empty string if _other_array_ is empty.
|
40
|
+
#
|
41
|
+
# Applies to string argument accordingly: First string in _array_ applied to
|
42
|
+
# _str_; empty string if _str_ is empty.
|
43
|
+
def %(args)
|
44
|
+
opts = { :sep => ', ' }
|
45
|
+
opts.update(pop) if last.is_a?(Hash)
|
46
|
+
|
47
|
+
default = lambda { |n|
|
48
|
+
['%s'] * n * opts[:sep]
|
49
|
+
}
|
50
|
+
|
51
|
+
case args
|
52
|
+
when String
|
53
|
+
return (first || default[1]) % args unless
|
54
|
+
args.nil? || args.empty?
|
55
|
+
when Array
|
56
|
+
i = 0
|
57
|
+
[*args].comb { |x|
|
58
|
+
return (self[i] || default[x.size]) % x unless
|
59
|
+
x.empty? || x.any? { |y|
|
60
|
+
y.nil? || y.empty?
|
61
|
+
}
|
62
|
+
|
63
|
+
i += 1
|
64
|
+
}
|
65
|
+
end
|
66
|
+
|
67
|
+
''
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
if $0 == __FILE__
|
73
|
+
[[], 'string', ''].each { |x|
|
74
|
+
p x
|
75
|
+
puts '>> ' << ['"%s"'] % x
|
76
|
+
}
|
77
|
+
|
78
|
+
[ ['place', 'country'],
|
79
|
+
['place', '' ],
|
80
|
+
['', 'country'],
|
81
|
+
['', '' ]
|
82
|
+
].each { |x|
|
83
|
+
p x
|
84
|
+
puts '>> ' << ['%s, (%s)', '%s', '(%s)'] % x
|
85
|
+
}
|
86
|
+
|
87
|
+
puts '=' * 80
|
88
|
+
|
89
|
+
[ ['author', 'title', 'year'],
|
90
|
+
['author', 'title', '' ],
|
91
|
+
['author', '', 'year'],
|
92
|
+
['', 'title', 'year'],
|
93
|
+
['author', '' , '' ],
|
94
|
+
['', 'title', '' ],
|
95
|
+
['', '', 'year'],
|
96
|
+
['', '', '' ]
|
97
|
+
].each { |x|
|
98
|
+
p x
|
99
|
+
puts '>> ' << ['%s: %s (%s)', '%s: %s', '%s (%s)', '%s (%s)'] % x
|
100
|
+
}
|
101
|
+
|
102
|
+
puts '=' * 80
|
103
|
+
|
104
|
+
[ ['1', '2', '3', '4'],
|
105
|
+
['1', '2', '3', '' ],
|
106
|
+
['1', '2', '', '4'],
|
107
|
+
['1', '', '3', '4'],
|
108
|
+
['', '2', '3', '4'],
|
109
|
+
['1', '2', '', '' ],
|
110
|
+
['1', '', '3', '' ],
|
111
|
+
['1', '', '', '4'],
|
112
|
+
['', '2', '3', '' ],
|
113
|
+
['', '2', '', '4'],
|
114
|
+
['', '', '3', '4'],
|
115
|
+
['1', '', '', '' ],
|
116
|
+
['', '2', '', '' ],
|
117
|
+
['', '', '3', '' ],
|
118
|
+
['', '', '', '4'],
|
119
|
+
['', '', '', '' ]
|
120
|
+
].each { |x|
|
121
|
+
p x
|
122
|
+
puts '>> ' << [{ :sep => ':' }] % x
|
123
|
+
}
|
124
|
+
end
|
@@ -0,0 +1,62 @@
|
|
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.in_order(*ordered) => new_array
|
32
|
+
#
|
33
|
+
# Force order, but ignore non-existing and keep remaining.
|
34
|
+
#
|
35
|
+
# Examples:
|
36
|
+
# [:created_at, :email, :login, :updated_at].in_order(:login, :email) #=> [:login, :email, :created_at, :updated_at]
|
37
|
+
# [:created_at, :email, :login, :updated_at].in_order(:email, :address) #=> [:email, :created_at, :login, :updated_at]
|
38
|
+
def in_order(*ordered)
|
39
|
+
ordered &= self
|
40
|
+
ordered + (self - ordered)
|
41
|
+
end
|
42
|
+
|
43
|
+
# call-seq:
|
44
|
+
# array.in_order!(*ordered) => array
|
45
|
+
#
|
46
|
+
# Destructive version of #in_order.
|
47
|
+
def in_order!(*ordered)
|
48
|
+
replace in_order(*ordered)
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
if $0 == __FILE__
|
54
|
+
a = [:created_at, :email, :login, :updated_at]
|
55
|
+
p a
|
56
|
+
|
57
|
+
p a.in_order(:login, :email)
|
58
|
+
p a.in_order(:email, :address)
|
59
|
+
|
60
|
+
a.in_order!(:login, :email)
|
61
|
+
p a
|
62
|
+
end
|