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
@@ -0,0 +1,133 @@
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. Select a different shuffling algorithm:
34
+ # <tt>Array.send(:alias_method, :shuffle, :shuffle_kfy)</tt>.
35
+ def shuffle
36
+ sort_by { Kernel.rand }
37
+ end
38
+
39
+ # call-seq:
40
+ # array.shuffle_knuth => new_array
41
+ #
42
+ # Non-destructive version of #shuffle_knuth!.
43
+ def shuffle_knuth
44
+ dup.shuffle_knuth!
45
+ end
46
+
47
+ # call-seq:
48
+ # array.shuffle_kfy => new_array
49
+ #
50
+ # Non-destructive version of #shuffle_kfy!.
51
+ def shuffle_kfy
52
+ dup.shuffle_kfy!
53
+ end
54
+
55
+ # call-seq:
56
+ # array.shuffle! => array
57
+ #
58
+ # Destructive version of #shuffle.
59
+ def shuffle!
60
+ replace shuffle
61
+ end
62
+
63
+ # call-seq:
64
+ # array.shuffle_knuth! => array
65
+ #
66
+ # Shuffles _array_ in random order using Knuth's algorithm.
67
+ def shuffle_knuth!
68
+ 0.upto(length - 2) { |i|
69
+ n = i + rand(length - i)
70
+ self[i], self[n] = self[n], self[i]
71
+ }
72
+
73
+ self
74
+ end
75
+
76
+ # call-seq:
77
+ # array.shuffle_kfy! => array
78
+ #
79
+ # Shuffles _array_ in random order using the Knuth-Fisher-Yates algorithm.
80
+ def shuffle_kfy!
81
+ (length - 1).downto(0) { |i|
82
+ n = rand(i + 1)
83
+ self[n], self[i] = self[i], self[n]
84
+ }
85
+
86
+ self
87
+ end
88
+
89
+ end
90
+
91
+ if $0 == __FILE__
92
+ a = %w[1 2 3 4 5 6 7 8]
93
+ p a
94
+
95
+ p a.shuffle
96
+ p a.shuffle
97
+
98
+ p a.shuffle_knuth
99
+ p a.shuffle_kfy
100
+
101
+ a.shuffle!
102
+ p a
103
+
104
+ require 'nuggets/integer/factorial'
105
+ require 'nuggets/enumerable/minmax'
106
+
107
+ a = %w[a b c]
108
+ n = 100_000
109
+ m = a.length.f!
110
+ e = n / m.to_f
111
+ puts '%d / %d / %d / %.2f' % [a.length, n, m, e]
112
+
113
+ algorithms = %w[shuffle shuffle_knuth shuffle_kfy]
114
+ max = algorithms.max(:length)
115
+
116
+ algorithms.each { |algorithm|
117
+ score = Hash.new { |h, k| h[k] = 0 }
118
+
119
+ n.times {
120
+ score[a.send(algorithm)] += 1
121
+ }
122
+
123
+ x2 = 0
124
+ score.sort.each { |key, value|
125
+ x = value - e
126
+ y = x**2 / e
127
+ #puts '%s: %d (% .2f/%.2f)' % [key, value, x, y]
128
+
129
+ x2 += y
130
+ }
131
+ puts "%-#{max}s = %.2f (%.2f)" % [algorithm, x2, x2 / m]
132
+ }
133
+ end
@@ -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-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
+ require 'nuggets/array/flatten_once'
29
+
30
+ class Array
31
+
32
+ # call-seq:
33
+ # array.to_hash => aHash
34
+ # array.to_hash(value) => aHash
35
+ # array.to_hash { |element| ... } => aHash
36
+ #
37
+ # If neither +value+ nor block is given, converts _array_, taken as an
38
+ # array of key/value pairs, into a hash, preserving sub-arrays (Thus:
39
+ # <tt>hash.to_a.to_h == hash</tt>). Otherwise, maps each element of
40
+ # _array_ to +value+ or the result of the block.
41
+ #
42
+ # Examples:
43
+ # [[0, 0], [1, [2, 3]]].to_h #=> { 0 => 0, 1 => [2, 3] }
44
+ # %w[a b c d].to_h #=> { "a" => "b", "c" => "d" }
45
+ # %w[a b c d].to_h(1) #=> { "a" => 1, "b" => 1, "c" => 1, "d" => 1 }
46
+ # %w[a b].to_h { |e| e * 2 } #=> { "a" => "aa", "b" => "bb" }
47
+ def to_hash(value = default = Object.new)
48
+ hash = {}
49
+
50
+ if block_given?
51
+ raise ArgumentError, "both block and value argument given" if default.nil?
52
+
53
+ each { |element| hash[element] = yield element }
54
+ elsif default.nil?
55
+ each { |element| hash[element] = value }
56
+ else
57
+ return Hash[*flatten_once]
58
+ end
59
+
60
+ hash
61
+ end
62
+ alias_method :to_h, :to_hash
63
+
64
+ end
65
+
66
+ if $0 == __FILE__
67
+ a = [[:a, 1], [:b, 2], [:c, 3]]
68
+ p a
69
+ p a.to_h
70
+
71
+ b = [[:a, [1, 2]], [:b, 3], [[:c, :d], [4, [5, 6]]]]
72
+ p b
73
+ p b.to_h
74
+
75
+ c = %w[a b c d]
76
+ p c
77
+ p c.to_h
78
+ p c.to_h(1)
79
+ p c.to_h { nil }
80
+
81
+ h = { :a => 1, :b => [2, 3], :c => { :d => 4}}
82
+ p h
83
+ p h.to_a
84
+ p h.to_a.to_h
85
+ p h.to_a.to_h == h
86
+ end
@@ -0,0 +1,82 @@
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
+ begin
29
+ require 'rubygems'
30
+ rescue LoadError
31
+ end
32
+
33
+ begin
34
+ require 'amatch'
35
+ rescue LoadError
36
+ warn "Couldn't load amatch..." if $VERBOSE
37
+ end
38
+
39
+ module Enumerable
40
+
41
+ # call-seq:
42
+ # enum.agrep(pattern[, distance]) => anArray
43
+ # enum.agrep(pattern[, distance]) { |obj| ... } => anArray
44
+ #
45
+ # Returns an array of every element in _enum_ for which +pattern+ approximately
46
+ # matches +element+ (see Amatch::Levenshtein#search). If the optional +block+
47
+ # is supplied, each matching element is passed to it, and the block‘s result
48
+ # is stored in the output array.
49
+ #
50
+ # LIMITATIONS:
51
+ #
52
+ # - Only strings are allowed as +pattern+. Regular expressions are reverted
53
+ # to their respective source. (Equivalent to <tt>agrep -k</tt>)
54
+ # - Only works with string elements in _enum_. (Calls +to_s+ on each element)
55
+ # - The cost for individual error types (substitution, insertion, deletion)
56
+ # cannot be adjusted.
57
+ def agrep(pattern, distance = 0)
58
+ raise 'Amatch not available!' unless defined?(Amatch)
59
+
60
+ pattern = pattern.source if pattern.is_a?(Regexp)
61
+
62
+ amatch = Amatch::Levenshtein.new(pattern)
63
+ matches = select { |obj| amatch.search(obj.to_s) <= distance }
64
+
65
+ block_given? ? matches.map { |match| yield match } : matches
66
+ end
67
+
68
+ end
69
+
70
+ if $0 == __FILE__
71
+ e = %w[quux quuux quix quixx]
72
+ p e
73
+
74
+ p e.agrep(/quux/)
75
+ p e.agrep(/quux/, 1)
76
+ p e.agrep(/quux/, 2)
77
+
78
+ p e.grep(/qu.x/)
79
+ p e.agrep(/qu.x/)
80
+
81
+ #p [123, 124, 1233].agrep(/123/, 1)
82
+ end
@@ -0,0 +1,99 @@
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 Enumerable
29
+
30
+ alias_method :_nuggets_original_all?, :all?
31
+ alias_method :_nuggets_original_any?, :any?
32
+
33
+ # call-seq:
34
+ # enum.all?(obj[, operator]) => true or false
35
+ # enum.all? { ... } => true or false
36
+ #
37
+ # Adds the ability to pass an +object+ instead of a block, which will then
38
+ # be tested against each item in _enum_ according to +operator+, defaulting
39
+ # to :===.
40
+ def all?(object = default = Object.new, operator = :===)
41
+ _nuggets_original_all?(&block_given? ?
42
+ _block_for_all_any_extended(object, default, operator) { |*a| yield(*a) } :
43
+ _block_for_all_any_extended(object, default, operator))
44
+ end
45
+
46
+ # call-seq:
47
+ # enum.any?(obj[, operator]) => true or false
48
+ # enum.any? { ... } => true or false
49
+ #
50
+ # Adds the ability to pass an +object+ instead of a block, which will then
51
+ # be tested against each item in _enum_ according to +operator+, defaulting
52
+ # to :===.
53
+ def any?(object = default = Object.new, operator = :===)
54
+ _nuggets_original_any?(&block_given? ?
55
+ _block_for_all_any_extended(object, default, operator) { |*a| yield(*a) } :
56
+ _block_for_all_any_extended(object, default, operator))
57
+ end
58
+
59
+ private
60
+
61
+ # Common argument processing for extended versions of #all? and #any?.
62
+ def _block_for_all_any_extended(object, default, operator)
63
+ if default.nil?
64
+ raise ArgumentError, "both block and object argument given", caller(1) if block_given?
65
+
66
+ lambda { |item| object.send(operator, item) }
67
+ elsif block_given?
68
+ lambda { |item| yield item }
69
+ end
70
+ end
71
+
72
+ end
73
+
74
+ if $0 == __FILE__
75
+ e = %w[quux quuux quix]
76
+ p e
77
+
78
+ p e.all?(String)
79
+ p e.any?(Numeric)
80
+
81
+ e = [:one, 'c', nil, 88]
82
+ p e
83
+
84
+ p e.all?(Object)
85
+ p e.any?(NilClass)
86
+
87
+ begin
88
+ e.any?(NilClass) { |i| i.nil? }
89
+ rescue ArgumentError => err
90
+ puts "#{err.backtrace.first}: #{err} (#{err.class})"
91
+ end
92
+
93
+ e = [0, 10, 20]
94
+ p e
95
+
96
+ p e.any?(9..99)
97
+ p e.any?(9, :<)
98
+ p e.any? { |i| i < 9 }
99
+ 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-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 Enumerable
29
+
30
+ alias_method :_nuggets_original_max, :max
31
+ alias_method :_nuggets_original_min, :min
32
+
33
+ # call-seq:
34
+ # enum.minmax_by(meth, by) => aValue
35
+ #
36
+ # Finds the maximum/minimum (or whatever +meth+ is) value in _enum_ according
37
+ # to +by+ (which may be a symbol/string that is sent to each value, or a proc
38
+ # that receives each value as parameter).
39
+ def minmax_by(meth, by)
40
+ _by = by.is_a?(Proc) ? by : lambda { |i| i.send(by) }
41
+ send(meth) { |a, b| _by[a] <=> _by[b] }
42
+ end
43
+
44
+ # call-seq:
45
+ # enum.max_by(by) => aValue
46
+ #
47
+ # Maximum #minmax_by.
48
+ def max_by(by)
49
+ minmax_by(:max, by)
50
+ end
51
+
52
+ # call-seq:
53
+ # enum.min_by(by) => aValue
54
+ #
55
+ # Minimum #minmax_by.
56
+ def min_by(by)
57
+ minmax_by(:min, by)
58
+ end
59
+
60
+ # call-seq:
61
+ # enum.minmax(meth, what) => anObject
62
+ #
63
+ # Finds the #minmax_by according to +what+ and returns that "what".
64
+ #
65
+ # Example:
66
+ # %w[a bcd ef].max(:length) #=> 3
67
+ def minmax(meth, what)
68
+ #m = minmax_by(meth, what)
69
+ #what.is_a?(Proc) ? what[m] : m.send(what)
70
+
71
+ _what = what.is_a?(Proc) ? what : lambda { |i| i.send(what) }
72
+ map { |i| _what[i] }.send(meth)
73
+
74
+ # Benchmark (:max, :length; enum.size = 20, N = 100_000):
75
+ #
76
+ # max_by(:length).length 7.920000 0.890000 8.810000 ( 8.991915)
77
+ # map(:length).max 4.800000 0.600000 5.400000 ( 5.418114)
78
+ end
79
+
80
+ # call-seq:
81
+ # enum.max(what) => aValue
82
+ #
83
+ # Maximum #minmax. If +what+ is omitted, or nil, the original Enumerable#max
84
+ # is called.
85
+ def max(what = nil)
86
+ what ? minmax(:max, what) : block_given? ?
87
+ _nuggets_original_max { |*a| yield(*a) } : _nuggets_original_max
88
+ end
89
+
90
+ # call-seq:
91
+ # enum.min(what) => aValue
92
+ #
93
+ # Minimum #minmax. If +what+ is omitted, or nil, the original Enumerable#min
94
+ # is called.
95
+ def min(what = nil)
96
+ what ? minmax(:min, what) : block_given? ?
97
+ _nuggets_original_min { |*a| yield(*a) } : _nuggets_original_min
98
+ end
99
+
100
+ end
101
+
102
+ if $0 == __FILE__
103
+ e = %w[quux quuux quix]
104
+ p e
105
+
106
+ p e.max
107
+ p e.max_by(:length)
108
+ p e.max(:length)
109
+
110
+ e = [3, 222, 45]
111
+ p e
112
+
113
+ # the last digit counts ;-)
114
+ l = lambda { |i| i.to_s.split(//).last.to_i }
115
+
116
+ p e.max
117
+ p e.max_by(l)
118
+ p e.max(l)
119
+ end
@@ -0,0 +1,53 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of ruby-nuggets, some extensions to the Ruby programming #
5
+ # language. #
6
+ # #
7
+ # Copyright (C) 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
+ begin
29
+ require 'win32console'
30
+ rescue LoadError
31
+ end
32
+
33
+ class << ENV
34
+
35
+ # call-seq:
36
+ # ENV.user_encoding => aString or nil
37
+ #
38
+ # Finds the user's selected encoding.
39
+ def user_encoding
40
+ ENV['ENCODING'] ||
41
+ ENV['LANG'][/\.(.*)/, 1] ||
42
+ if defined?(Win32::Console)
43
+ "CP#{Win32::Console.InputCP}"
44
+ else
45
+ cp = %x{chcp}[/:\s*(.*?)\./, 1] and "CP#{cp}"
46
+ end
47
+ end
48
+
49
+ end
50
+
51
+ if $0 == __FILE__
52
+ p ENV.user_encoding
53
+ end
@@ -0,0 +1,54 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of ruby-nuggets, some extensions to the Ruby programming #
5
+ # language. #
6
+ # #
7
+ # Copyright (C) 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 << ENV
29
+
30
+ # call-seq:
31
+ # ENV.user_home => aString
32
+ #
33
+ # Finds the user's home directory. Stolen from RubyGems ;-)
34
+ def user_home
35
+ %w[HOME USERPROFILE].each { |homekey|
36
+ return ENV[homekey] if ENV[homekey]
37
+ }
38
+
39
+ if ENV['HOMEDRIVE'] && ENV['HOMEPATH']
40
+ return "#{ENV['HOMEDRIVE']}:#{ENV['HOMEPATH']}"
41
+ end
42
+
43
+ begin
44
+ File.expand_path('~')
45
+ rescue ArgumentError
46
+ File::ALT_SEPARATOR ? 'C:/' : '/'
47
+ end
48
+ end
49
+
50
+ end
51
+
52
+ if $0 == __FILE__
53
+ p ENV.user_home
54
+ end