blackwinter-ruby-nuggets 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. data/COPYING +676 -0
  2. data/ChangeLog +5 -0
  3. data/README +60 -0
  4. data/Rakefile +20 -0
  5. data/lib/nuggets/all.rb +34 -0
  6. data/lib/nuggets/array/combination.rb +86 -0
  7. data/lib/nuggets/array/flatten_once.rb +68 -0
  8. data/lib/nuggets/array/format.rb +124 -0
  9. data/lib/nuggets/array/in_order.rb +62 -0
  10. data/lib/nuggets/array/monotone.rb +101 -0
  11. data/lib/nuggets/array/only.rb +56 -0
  12. data/lib/nuggets/array/rand.rb +45 -0
  13. data/lib/nuggets/array/shuffle.rb +133 -0
  14. data/lib/nuggets/array/to_hash.rb +86 -0
  15. data/lib/nuggets/enumerable/agrep.rb +82 -0
  16. data/lib/nuggets/enumerable/all_any_extended.rb +99 -0
  17. data/lib/nuggets/enumerable/minmax.rb +119 -0
  18. data/lib/nuggets/env/user_encoding.rb +53 -0
  19. data/lib/nuggets/env/user_home.rb +54 -0
  20. data/lib/nuggets/file/which.rb +74 -0
  21. data/lib/nuggets/hash/at.rb +87 -0
  22. data/lib/nuggets/hash/in_order.rb +52 -0
  23. data/lib/nuggets/hash/insert.rb +65 -0
  24. data/lib/nuggets/hash/only.rb +68 -0
  25. data/lib/nuggets/integer/factorial.rb +74 -0
  26. data/lib/nuggets/integer/to_binary_s.rb +47 -0
  27. data/lib/nuggets/io/agrep.rb +43 -0
  28. data/lib/nuggets/io/modes.rb +133 -0
  29. data/lib/nuggets/numeric/between.rb +2 -0
  30. data/lib/nuggets/numeric/duration.rb +109 -0
  31. data/lib/nuggets/numeric/limit.rb +70 -0
  32. data/lib/nuggets/numeric/signum.rb +60 -0
  33. data/lib/nuggets/numeric/to_multiple.rb +68 -0
  34. data/lib/nuggets/object/blank.rb +119 -0
  35. data/lib/nuggets/object/boolean.rb +69 -0
  36. data/lib/nuggets/object/eigenclass.rb +2 -0
  37. data/lib/nuggets/object/ghost_class.rb +2 -0
  38. data/lib/nuggets/object/metaclass.rb +2 -0
  39. data/lib/nuggets/object/msend.rb +55 -0
  40. data/lib/nuggets/object/singleton_class.rb +150 -0
  41. data/lib/nuggets/object/uniclass.rb +2 -0
  42. data/lib/nuggets/object/virtual_class.rb +2 -0
  43. data/lib/nuggets/proc/bind.rb +68 -0
  44. data/lib/nuggets/string/capitalize_first.rb +63 -0
  45. data/lib/nuggets/string/case.rb +104 -0
  46. data/lib/nuggets/string/evaluate.rb +53 -0
  47. data/lib/nuggets/string/msub.rb +82 -0
  48. data/lib/nuggets/string/nsub.rb +80 -0
  49. data/lib/nuggets/string/sub_with_md.rb +131 -0
  50. data/lib/nuggets/string/word_wrap.rb +111 -0
  51. data/lib/nuggets/tempfile/open.rb +54 -0
  52. data/lib/nuggets/uri/content_type.rb +65 -0
  53. data/lib/nuggets/uri/exist.rb +63 -0
  54. data/lib/nuggets/util/added_methods/init.rb +3 -0
  55. data/lib/nuggets/util/added_methods.rb +407 -0
  56. data/lib/nuggets/util/ansicolor2css.rb +90 -0
  57. data/lib/nuggets/util/content_type.rb +104 -0
  58. data/lib/nuggets/util/dotted_decimal.rb +66 -0
  59. data/lib/nuggets/util/i18n.rb +143 -0
  60. data/lib/nuggets/version.rb +27 -0
  61. data/lib/nuggets.rb +73 -0
  62. metadata +124 -0
data/README ADDED
@@ -0,0 +1,60 @@
1
+ = ruby-nuggets - Some extensions to the Ruby programming language
2
+
3
+ == VERSION
4
+
5
+ This documentation refers to ruby-nuggets version 0.4.0
6
+
7
+
8
+ == DESCRIPTION
9
+
10
+ ruby-nuggets provides a collection of extensions to Ruby core classes. In
11
+ its effort, it's similar to projects like Facets[http://facets.rubyforge.org]
12
+ and Extensions[http://extensions.rubyforge.org] (or even
13
+ Labrador[http://rubyforge.org/projects/labrador/]), though not as ambitious ;-)
14
+ This is simply where I put stuff that I need on a more or less regular basis
15
+ or that struck me as interesting while surfing the web, reading books, or
16
+ following discussions on ruby-talk.
17
+
18
+ There are several ways to get at the nuggets you're interested in:
19
+
20
+ # The usual way: Just require() what you need
21
+ require 'nuggets/object/singleton_class'
22
+
23
+ # The more fancy way ;-) (see the documentation of Nuggets() for more examples)
24
+ require 'nuggets'
25
+ Nuggets(:object => 'singleton_class', :string => %w[nsub sub_with_md])
26
+
27
+ # Finally, get everything there is...
28
+ require 'nuggets/all'
29
+
30
+ Please be aware that some of the things herein might not work as expected or
31
+ cause other libraries to misbehave. Use at your own risk!
32
+
33
+
34
+ == LINKS
35
+
36
+ * <http://prometheus.rubyforge.org/ruby-nuggets>
37
+ * <http://prometheus.rubyforge.org/svn/scratch/ruby-nuggets>
38
+ * <http://github.com/blackwinter/ruby-nuggets>
39
+
40
+
41
+ == AUTHORS
42
+
43
+ * Jens Wille <mailto:jens.wille@uni-koeln.de>
44
+
45
+
46
+ == LICENSE AND COPYRIGHT
47
+
48
+ Copyright (C) 2007-2008 Jens Wille
49
+
50
+ ruby-nuggets is free software: you can redistribute it and/or modify it under
51
+ the terms of the GNU General Public License as published by the Free Software
52
+ Foundation, either version 3 of the License, or (at your option) any later
53
+ version.
54
+
55
+ ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT ANY
56
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
57
+ A PARTICULAR PURPOSE. See the GNU General Public License for more details.
58
+
59
+ You should have received a copy of the GNU General Public License along with
60
+ ruby-nuggets. If not, see <http://www.gnu.org/licenses/>.
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ begin
2
+ require 'hen'
3
+ rescue LoadError
4
+ abort "Please install the 'hen' gem first."
5
+ end
6
+
7
+ require 'lib/nuggets/version'
8
+
9
+ Hen.lay! {{
10
+ :rubyforge => {
11
+ :package => 'ruby-nuggets'
12
+ },
13
+
14
+ :gem => {
15
+ :version => Nuggets::VERSION,
16
+ :summary => 'Some extensions to the Ruby programming language.',
17
+ :files => FileList['lib/**/*.rb'].to_a,
18
+ :extra_files => FileList['[A-Z]*'].to_a
19
+ }
20
+ }}
@@ -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 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[* ** *.rb])].sort.each { |path|
32
+ ext_re = Regexp.escape(File.extname(path))
33
+ require path.sub(/#{base_re}(.*)#{ext_re}/, 'nuggets\1')
34
+ }
@@ -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,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
+ class Array
29
+
30
+ # call-seq:
31
+ # array.flatten_once => new_array
32
+ #
33
+ # Flatten _array_ by _one_ level only. Pretty straight-forward port of David
34
+ # Alan Black's flattenx C implementation (though much slower, of course ;-).
35
+ def flatten_once
36
+ flat = []
37
+
38
+ each { |element|
39
+ if element.is_a?(Array)
40
+ flat += element
41
+ else
42
+ flat << element
43
+ end
44
+ }
45
+
46
+ flat
47
+ end
48
+
49
+ # call-seq:
50
+ # array.flatten_once! => array
51
+ #
52
+ # Destructive version of #flatten_once.
53
+ def flatten_once!
54
+ replace flatten_once
55
+ end
56
+
57
+ end
58
+
59
+ if $0 == __FILE__
60
+ a = [1, 2, [3, 4, 5], 6, [7, [8, 9]]]
61
+ p a
62
+
63
+ p a.flatten
64
+ p a.flatten_once
65
+
66
+ a.flatten_once!
67
+ p a
68
+ 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 'nuggets/array/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
@@ -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?(operator) => true or false
32
+ #
33
+ # Check whether _array_ is monotone according to +operator+.
34
+ def monotone?(operator)
35
+ inject { |a, b|
36
+ return false unless a.send(operator, 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,56 @@
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 Array
29
+
30
+ # call-seq:
31
+ # array.only(relax = true or false) => anObject
32
+ #
33
+ # Returns the only element of _array_. Raises an IndexError if _array_'s
34
+ # size is not 1, unless +relax+ is true.
35
+ #
36
+ # Idea stolen from Gavin Sinclair's Ruby Extensions Project.
37
+ def only(relax = size == 1)
38
+ raise IndexError, 'not a single-element array' unless relax
39
+ first
40
+ end
41
+
42
+ end
43
+
44
+ if $0 == __FILE__
45
+ [[5], [1, 2, 3], []].each { |a|
46
+ p a
47
+
48
+ begin
49
+ p a.only
50
+ rescue IndexError => err
51
+ warn err
52
+ end
53
+
54
+ p a.only(true)
55
+ }
56
+ 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