nuggets 0.9.9 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/COPYING +663 -0
- data/ChangeLog +7 -0
- data/README +74 -0
- data/Rakefile +50 -0
- data/lib/nuggets.rb +73 -0
- data/lib/nuggets/all.rb +35 -0
- data/lib/nuggets/all_mixins.rb +33 -0
- data/lib/nuggets/ansicolor2css.rb +124 -0
- data/lib/nuggets/argv/option.rb +3 -0
- data/lib/nuggets/argv/option_mixin.rb +84 -0
- data/lib/nuggets/array/boost.rb +5 -0
- data/lib/nuggets/array/boost_mixin.rb +77 -0
- data/lib/nuggets/array/combination.rb +62 -0
- data/lib/nuggets/array/correlation.rb +5 -0
- data/lib/nuggets/array/correlation_mixin.rb +63 -0
- data/lib/nuggets/array/flatten_once.rb +56 -0
- data/lib/nuggets/array/flush.rb +5 -0
- data/lib/nuggets/array/flush_mixin.rb +46 -0
- data/lib/nuggets/array/format.rb +65 -0
- data/lib/nuggets/array/hashify.rb +5 -0
- data/lib/nuggets/array/hashify_mixin.rb +47 -0
- data/lib/nuggets/array/histogram.rb +5 -0
- data/lib/nuggets/array/histogram_mixin.rb +169 -0
- data/lib/nuggets/array/in_order.rb +50 -0
- data/lib/nuggets/array/limit.rb +5 -0
- data/lib/nuggets/array/limit_mixin.rb +56 -0
- data/lib/nuggets/array/mean.rb +5 -0
- data/lib/nuggets/array/mean_mixin.rb +202 -0
- data/lib/nuggets/array/median.rb +5 -0
- data/lib/nuggets/array/median_mixin.rb +73 -0
- data/lib/nuggets/array/mode.rb +5 -0
- data/lib/nuggets/array/mode_mixin.rb +69 -0
- data/lib/nuggets/array/monotone.rb +83 -0
- data/lib/nuggets/array/only.rb +41 -0
- data/lib/nuggets/array/rand.rb +44 -0
- data/lib/nuggets/array/regression.rb +5 -0
- data/lib/nuggets/array/regression_mixin.rb +149 -0
- data/lib/nuggets/array/runiq.rb +5 -0
- data/lib/nuggets/array/runiq_mixin.rb +52 -0
- data/lib/nuggets/array/shuffle.rb +132 -0
- data/lib/nuggets/array/standard_deviation.rb +5 -0
- data/lib/nuggets/array/standard_deviation_mixin.rb +50 -0
- data/lib/nuggets/array/to_hash.rb +64 -0
- data/lib/nuggets/array/variance.rb +5 -0
- data/lib/nuggets/array/variance_mixin.rb +81 -0
- data/lib/nuggets/content_type.rb +97 -0
- data/lib/nuggets/dotted_decimal.rb +59 -0
- data/lib/nuggets/enumerable/agrep.rb +79 -0
- data/lib/nuggets/enumerable/all_any_extended.rb +66 -0
- data/lib/nuggets/enumerable/minmax.rb +102 -0
- data/lib/nuggets/env/set.rb +3 -0
- data/lib/nuggets/env/set_mixin.rb +67 -0
- data/lib/nuggets/env/user_encoding.rb +3 -0
- data/lib/nuggets/env/user_encoding_mixin.rb +54 -0
- data/lib/nuggets/env/user_home.rb +3 -0
- data/lib/nuggets/env/user_home_mixin.rb +56 -0
- data/lib/nuggets/file/ext.rb +5 -0
- data/lib/nuggets/file/ext_mixin.rb +87 -0
- data/lib/nuggets/file/replace.rb +5 -0
- data/lib/nuggets/file/replace_mixin.rb +55 -0
- data/lib/nuggets/file/sub.rb +5 -0
- data/lib/nuggets/file/sub_mixin.rb +95 -0
- data/lib/nuggets/file/which.rb +5 -0
- data/lib/nuggets/file/which_mixin.rb +72 -0
- data/lib/nuggets/hash/at.rb +68 -0
- data/lib/nuggets/hash/deep_fetch.rb +5 -0
- data/lib/nuggets/hash/deep_fetch_mixin.rb +74 -0
- data/lib/nuggets/hash/deep_merge.rb +5 -0
- data/lib/nuggets/hash/deep_merge_mixin.rb +53 -0
- data/lib/nuggets/hash/idmap.rb +5 -0
- data/lib/nuggets/hash/idmap_mixin.rb +41 -0
- data/lib/nuggets/hash/in_order.rb +43 -0
- data/lib/nuggets/hash/insert.rb +53 -0
- data/lib/nuggets/hash/nest.rb +5 -0
- data/lib/nuggets/hash/nest_mixin.rb +78 -0
- data/lib/nuggets/hash/only.rb +52 -0
- data/lib/nuggets/hash/seen.rb +5 -0
- data/lib/nuggets/hash/seen_mixin.rb +58 -0
- data/lib/nuggets/hash/unroll.rb +5 -0
- data/lib/nuggets/hash/unroll_mixin.rb +88 -0
- data/lib/nuggets/hash/zip.rb +5 -0
- data/lib/nuggets/hash/zip_mixin.rb +159 -0
- data/lib/nuggets/i18n.rb +155 -0
- data/lib/nuggets/integer/factorial.rb +56 -0
- data/lib/nuggets/integer/length.rb +5 -0
- data/lib/nuggets/integer/length_mixin.rb +49 -0
- data/lib/nuggets/integer/map.rb +5 -0
- data/lib/nuggets/integer/map_mixin.rb +42 -0
- data/lib/nuggets/integer/to_binary_s.rb +38 -0
- data/lib/nuggets/io/agrep.rb +43 -0
- data/lib/nuggets/io/interact.rb +5 -0
- data/lib/nuggets/io/interact_mixin.rb +159 -0
- data/lib/nuggets/io/modes.rb +121 -0
- data/lib/nuggets/io/null.rb +5 -0
- data/lib/nuggets/io/null_mixin.rb +40 -0
- data/lib/nuggets/io/redirect.rb +5 -0
- data/lib/nuggets/io/redirect_mixin.rb +50 -0
- data/lib/nuggets/lazy_attr.rb +44 -0
- data/lib/nuggets/log_parser.rb +70 -0
- data/lib/nuggets/log_parser/apache.rb +101 -0
- data/lib/nuggets/log_parser/rails.rb +219 -0
- data/lib/nuggets/net/success.rb +59 -0
- data/lib/nuggets/numeric/between.rb +2 -0
- data/lib/nuggets/numeric/duration.rb +100 -0
- data/lib/nuggets/numeric/limit.rb +62 -0
- data/lib/nuggets/numeric/signum.rb +52 -0
- data/lib/nuggets/numeric/to_multiple.rb +61 -0
- data/lib/nuggets/object/blank.rb +20 -0
- data/lib/nuggets/object/blank_mixin.rb +99 -0
- data/lib/nuggets/object/boolean.rb +5 -0
- data/lib/nuggets/object/boolean_mixin.rb +61 -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 +5 -0
- data/lib/nuggets/object/msend_mixin.rb +43 -0
- data/lib/nuggets/object/silence.rb +5 -0
- data/lib/nuggets/object/silence_mixin.rb +44 -0
- data/lib/nuggets/object/singleton_class.rb +5 -0
- data/lib/nuggets/object/singleton_class_mixin.rb +95 -0
- data/lib/nuggets/object/uniclass.rb +2 -0
- data/lib/nuggets/object/virtual_class.rb +2 -0
- data/lib/nuggets/pluggable.rb +91 -0
- data/lib/nuggets/proc/bind.rb +5 -0
- data/lib/nuggets/proc/bind_mixin.rb +51 -0
- data/lib/nuggets/range/quantile.rb +5 -0
- data/lib/nuggets/range/quantile_mixin.rb +42 -0
- data/lib/nuggets/ruby.rb +235 -0
- data/lib/nuggets/statistics.rb +12 -0
- data/lib/nuggets/statistics_mixins.rb +12 -0
- data/lib/nuggets/string/camelscore.rb +5 -0
- data/lib/nuggets/string/camelscore_mixin.rb +116 -0
- data/lib/nuggets/string/capitalize_first.rb +46 -0
- data/lib/nuggets/string/case.rb +81 -0
- data/lib/nuggets/string/evaluate.rb +5 -0
- data/lib/nuggets/string/evaluate_mixin.rb +47 -0
- data/lib/nuggets/string/msub.rb +84 -0
- data/lib/nuggets/string/nsub.rb +65 -0
- data/lib/nuggets/string/sub_with_md.rb +111 -0
- data/lib/nuggets/string/wc.rb +5 -0
- data/lib/nuggets/string/wc_mixin.rb +95 -0
- data/lib/nuggets/string/word_wrap.rb +76 -0
- data/lib/nuggets/string/xor.rb +5 -0
- data/lib/nuggets/string/xor_mixin.rb +59 -0
- data/lib/nuggets/tempfile/open.rb +57 -0
- data/lib/nuggets/uri/content_type.rb +5 -0
- data/lib/nuggets/uri/content_type_mixin.rb +47 -0
- data/lib/nuggets/uri/exist.rb +5 -0
- data/lib/nuggets/uri/exist_mixin.rb +56 -0
- data/lib/nuggets/uri/redirect.rb +5 -0
- data/lib/nuggets/uri/redirect_mixin.rb +101 -0
- data/lib/nuggets/version.rb +27 -0
- data/spec/nuggets/array/boost_spec.rb +50 -0
- data/spec/nuggets/array/combination_spec.rb +25 -0
- data/spec/nuggets/array/correlation_spec.rb +81 -0
- data/spec/nuggets/array/flatten_once_spec.rb +16 -0
- data/spec/nuggets/array/flush_spec.rb +43 -0
- data/spec/nuggets/array/format_spec.rb +52 -0
- data/spec/nuggets/array/hashify_spec.rb +41 -0
- data/spec/nuggets/array/histogram_spec.rb +87 -0
- data/spec/nuggets/array/in_order_spec.rb +13 -0
- data/spec/nuggets/array/limit_spec.rb +62 -0
- data/spec/nuggets/array/mean_spec.rb +203 -0
- data/spec/nuggets/array/median_spec.rb +77 -0
- data/spec/nuggets/array/mode_spec.rb +57 -0
- data/spec/nuggets/array/monotone_spec.rb +30 -0
- data/spec/nuggets/array/only_spec.rb +26 -0
- data/spec/nuggets/array/regression_spec.rb +54 -0
- data/spec/nuggets/array/runiq_spec.rb +25 -0
- data/spec/nuggets/array/standard_deviation_spec.rb +33 -0
- data/spec/nuggets/array/to_hash_spec.rb +28 -0
- data/spec/nuggets/array/variance_spec.rb +106 -0
- data/spec/nuggets/dotted_decimal_spec.rb +27 -0
- data/spec/nuggets/enumerable/all_any_extended_spec.rb +31 -0
- data/spec/nuggets/enumerable/minmax_spec.rb +21 -0
- data/spec/nuggets/env/set_spec.rb +29 -0
- data/spec/nuggets/env/user_encoding_spec.rb +38 -0
- data/spec/nuggets/env/user_home_spec.rb +42 -0
- data/spec/nuggets/file/ext_spec.rb +38 -0
- data/spec/nuggets/file/replace_spec.rb +95 -0
- data/spec/nuggets/file/sub_spec.rb +149 -0
- data/spec/nuggets/file/which_spec.rb +22 -0
- data/spec/nuggets/hash/at_spec.rb +19 -0
- data/spec/nuggets/hash/deep_fetch_spec.rb +159 -0
- data/spec/nuggets/hash/deep_merge_spec.rb +78 -0
- data/spec/nuggets/hash/in_order_spec.rb +12 -0
- data/spec/nuggets/hash/insert_spec.rb +13 -0
- data/spec/nuggets/hash/nest_spec.rb +102 -0
- data/spec/nuggets/hash/only_spec.rb +29 -0
- data/spec/nuggets/hash/seen_spec.rb +36 -0
- data/spec/nuggets/hash/unroll_spec.rb +68 -0
- data/spec/nuggets/i18n_spec.rb +13 -0
- data/spec/nuggets/integer/factorial_spec.rb +10 -0
- data/spec/nuggets/integer/length_spec.rb +18 -0
- data/spec/nuggets/integer/map_spec.rb +19 -0
- data/spec/nuggets/integer/to_binary_s_spec.rb +19 -0
- data/spec/nuggets/numeric/duration_spec.rb +25 -0
- data/spec/nuggets/numeric/limit_spec.rb +16 -0
- data/spec/nuggets/numeric/signum_spec.rb +16 -0
- data/spec/nuggets/numeric/to_multiple_spec.rb +16 -0
- data/spec/nuggets/object/blank_spec.rb +34 -0
- data/spec/nuggets/object/boolean_spec.rb +23 -0
- data/spec/nuggets/object/msend_spec.rb +25 -0
- data/spec/nuggets/object/silence_spec.rb +36 -0
- data/spec/nuggets/object/singleton_class_spec.rb +51 -0
- data/spec/nuggets/proc/bind_spec.rb +28 -0
- data/spec/nuggets/range/quantile_spec.rb +33 -0
- data/spec/nuggets/string/camelscore_spec.rb +114 -0
- data/spec/nuggets/string/capitalize_first_spec.rb +13 -0
- data/spec/nuggets/string/case_spec.rb +31 -0
- data/spec/nuggets/string/evaluate_spec.rb +24 -0
- data/spec/nuggets/string/msub_spec.rb +20 -0
- data/spec/nuggets/string/nsub_spec.rb +13 -0
- data/spec/nuggets/string/sub_with_md_spec.rb +25 -0
- data/spec/nuggets/string/wc_spec.rb +73 -0
- data/spec/nuggets/string/word_wrap_spec.rb +81 -0
- data/spec/nuggets/string/xor_spec.rb +57 -0
- data/spec/nuggets/uri/content_type_spec.rb +42 -0
- data/spec/nuggets/uri/exist_spec.rb +49 -0
- data/spec/spec_helper.rb +36 -0
- metadata +309 -17
@@ -0,0 +1,59 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# nuggets -- Extending Ruby #
|
5
|
+
# #
|
6
|
+
# Copyright (C) 2007-2011 Jens Wille #
|
7
|
+
# #
|
8
|
+
# Authors: #
|
9
|
+
# Jens Wille <jens.wille@gmail.com> #
|
10
|
+
# #
|
11
|
+
# nuggets is free software; you can redistribute it and/or modify it under #
|
12
|
+
# the terms of the GNU Affero General Public License as published by the Free #
|
13
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
14
|
+
# any later version. #
|
15
|
+
# #
|
16
|
+
# nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
|
17
|
+
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
|
18
|
+
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
|
19
|
+
# more details. #
|
20
|
+
# #
|
21
|
+
# You should have received a copy of the GNU Affero General Public License #
|
22
|
+
# along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
23
|
+
# #
|
24
|
+
###############################################################################
|
25
|
+
#++
|
26
|
+
|
27
|
+
require 'nuggets/integer/to_binary_s'
|
28
|
+
|
29
|
+
class Integer
|
30
|
+
|
31
|
+
# call-seq:
|
32
|
+
# int.to_dotted_decimal => aString
|
33
|
+
#
|
34
|
+
# Converts _int_ to dotted-decimal notation.
|
35
|
+
def to_dotted_decimal
|
36
|
+
to_binary_s(32).unpack('a8' * 4).map { |s| s.to_i(2) }.join('.')
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
class String
|
42
|
+
|
43
|
+
# call-seq:
|
44
|
+
# str.from_dotted_decimal => anInteger
|
45
|
+
#
|
46
|
+
# Converts _str_ from dotted-decimal notation to integer.
|
47
|
+
def from_dotted_decimal
|
48
|
+
split('.').map { |i| i.to_i.to_binary_s(8) }.join.to_i(2)
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
class Array
|
54
|
+
|
55
|
+
def sort_by_dotted_decimal
|
56
|
+
sort_by { |i| i.split('.').map { |j| j.to_i } }
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# nuggets -- Extending Ruby #
|
5
|
+
# #
|
6
|
+
# Copyright (C) 2007-2011 Jens Wille #
|
7
|
+
# #
|
8
|
+
# Authors: #
|
9
|
+
# Jens Wille <jens.wille@gmail.com> #
|
10
|
+
# #
|
11
|
+
# nuggets is free software; you can redistribute it and/or modify it under #
|
12
|
+
# the terms of the GNU Affero General Public License as published by the Free #
|
13
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
14
|
+
# any later version. #
|
15
|
+
# #
|
16
|
+
# nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
|
17
|
+
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
|
18
|
+
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
|
19
|
+
# more details. #
|
20
|
+
# #
|
21
|
+
# You should have received a copy of the GNU Affero General Public License #
|
22
|
+
# along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
23
|
+
# #
|
24
|
+
###############################################################################
|
25
|
+
#++
|
26
|
+
|
27
|
+
begin
|
28
|
+
require 'rubygems'
|
29
|
+
rescue ::LoadError
|
30
|
+
end
|
31
|
+
|
32
|
+
begin
|
33
|
+
require 'amatch'
|
34
|
+
rescue ::LoadError
|
35
|
+
warn "Couldn't load amatch..." if $VERBOSE
|
36
|
+
end
|
37
|
+
|
38
|
+
module Enumerable
|
39
|
+
|
40
|
+
# call-seq:
|
41
|
+
# enum.agrep(pattern[, distance]) -> anArray
|
42
|
+
# enum.agrep(pattern[, distance]) { |element| ... } -> enum
|
43
|
+
#
|
44
|
+
# Returns an array of all elements in _enum_ for which +pattern+ approximately
|
45
|
+
# matches +element+ (see Amatch::Levenshtein#search). If the optional +block+
|
46
|
+
# is supplied, each matching element is passed to it, and the _enum_ itself is
|
47
|
+
# returned.
|
48
|
+
#
|
49
|
+
# LIMITATIONS:
|
50
|
+
#
|
51
|
+
# - Only strings are allowed as +pattern+. Regular expressions are reverted
|
52
|
+
# to their respective source. (Equivalent to <tt>agrep -k</tt>)
|
53
|
+
# - Only works with string elements in _enum_. (Calls +to_s+ on each element)
|
54
|
+
# - The cost for individual error types (substitution, insertion, deletion)
|
55
|
+
# cannot be adjusted.
|
56
|
+
def agrep(pattern, distance = 0)
|
57
|
+
pattern = pattern.source if pattern.is_a?(::Regexp)
|
58
|
+
|
59
|
+
am = ::Amatch::Levenshtein.new(pattern)
|
60
|
+
ma = lambda { |i| am.search(i.to_s) <= distance }
|
61
|
+
|
62
|
+
block_given? ? each { |i| yield i if ma[i] } : select(&ma)
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
if $0 == __FILE__
|
68
|
+
e = %w[quux quuux quix quixx]
|
69
|
+
p e
|
70
|
+
|
71
|
+
p e.agrep(/quux/)
|
72
|
+
p e.agrep(/quux/, 1)
|
73
|
+
p e.agrep(/quux/, 2)
|
74
|
+
|
75
|
+
p e.grep(/qu.x/)
|
76
|
+
p e.agrep(/qu.x/)
|
77
|
+
|
78
|
+
#p [123, 124, 1233].agrep(/123/, 1)
|
79
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# nuggets -- Extending Ruby #
|
5
|
+
# #
|
6
|
+
# Copyright (C) 2007-2011 Jens Wille #
|
7
|
+
# #
|
8
|
+
# Authors: #
|
9
|
+
# Jens Wille <jens.wille@gmail.com> #
|
10
|
+
# #
|
11
|
+
# nuggets is free software; you can redistribute it and/or modify it under #
|
12
|
+
# the terms of the GNU Affero General Public License as published by the Free #
|
13
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
14
|
+
# any later version. #
|
15
|
+
# #
|
16
|
+
# nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
|
17
|
+
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
|
18
|
+
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
|
19
|
+
# more details. #
|
20
|
+
# #
|
21
|
+
# You should have received a copy of the GNU Affero General Public License #
|
22
|
+
# along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
23
|
+
# #
|
24
|
+
###############################################################################
|
25
|
+
#++
|
26
|
+
|
27
|
+
module Enumerable
|
28
|
+
|
29
|
+
alias_method :_nuggets_original_all?, :all?
|
30
|
+
alias_method :_nuggets_original_any?, :any?
|
31
|
+
|
32
|
+
# call-seq:
|
33
|
+
# enum.all?(obj[, operator]) => +true+ or +false+
|
34
|
+
# enum.all? { ... } => +true+ or +false+
|
35
|
+
#
|
36
|
+
# Adds the ability to pass an +object+ instead of a block, which will then
|
37
|
+
# be tested against each item in _enum_ according to +operator+, defaulting
|
38
|
+
# to <tt>:===</tt>.
|
39
|
+
def all?(object = default = true, operator = :===, &block)
|
40
|
+
_nuggets_original_all?(&_block_for_all_any_extended(object, default, operator, &block))
|
41
|
+
end
|
42
|
+
|
43
|
+
# call-seq:
|
44
|
+
# enum.any?(obj[, operator]) => +true+ or +false+
|
45
|
+
# enum.any? { ... } => +true+ or +false+
|
46
|
+
#
|
47
|
+
# Adds the ability to pass an +object+ instead of a block, which will then
|
48
|
+
# be tested against each item in _enum_ according to +operator+, defaulting
|
49
|
+
# to <tt>:===</tt>.
|
50
|
+
def any?(object = default = true, operator = :===, &block)
|
51
|
+
_nuggets_original_any?(&_block_for_all_any_extended(object, default, operator, &block))
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
# Common argument processing for extended versions of #all? and #any?.
|
57
|
+
def _block_for_all_any_extended(object, default, operator, &block)
|
58
|
+
unless default
|
59
|
+
raise ::ArgumentError, 'both block and object argument given', caller(1) if block
|
60
|
+
lambda { |*a| object.send(operator, *a) }
|
61
|
+
else
|
62
|
+
block
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# nuggets -- Extending Ruby #
|
5
|
+
# #
|
6
|
+
# Copyright (C) 2007-2011 Jens Wille #
|
7
|
+
# #
|
8
|
+
# Authors: #
|
9
|
+
# Jens Wille <jens.wille@gmail.com> #
|
10
|
+
# #
|
11
|
+
# nuggets is free software; you can redistribute it and/or modify it under #
|
12
|
+
# the terms of the GNU Affero General Public License as published by the Free #
|
13
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
14
|
+
# any later version. #
|
15
|
+
# #
|
16
|
+
# nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
|
17
|
+
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
|
18
|
+
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
|
19
|
+
# more details. #
|
20
|
+
# #
|
21
|
+
# You should have received a copy of the GNU Affero General Public License #
|
22
|
+
# along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
23
|
+
# #
|
24
|
+
###############################################################################
|
25
|
+
#++
|
26
|
+
|
27
|
+
module Enumerable
|
28
|
+
|
29
|
+
alias_method :_nuggets_original_minmax_by, :minmax_by if method_defined?(:minmax_by)
|
30
|
+
alias_method :_nuggets_original_min_by, :min_by if method_defined?(:min_by)
|
31
|
+
alias_method :_nuggets_original_max_by, :max_by if method_defined?(:max_by)
|
32
|
+
alias_method :_nuggets_original_minmax, :minmax if method_defined?(:minmax)
|
33
|
+
|
34
|
+
alias_method :_nuggets_original_max, :max
|
35
|
+
alias_method :_nuggets_original_min, :min
|
36
|
+
|
37
|
+
# call-seq:
|
38
|
+
# enum.minmax_by(meth, by) => aValue
|
39
|
+
#
|
40
|
+
# Finds the maximum/minimum (or whatever +meth+ is) value in _enum_ according
|
41
|
+
# to +by+ (which may be a symbol/string that is sent to each value, or a proc
|
42
|
+
# that receives each value as parameter).
|
43
|
+
def minmax_by(meth, by)
|
44
|
+
_by = by.is_a?(::Proc) ? by : lambda { |i| i.send(by) }
|
45
|
+
send(meth) { |a, b| _by[a] <=> _by[b] }
|
46
|
+
end
|
47
|
+
|
48
|
+
# call-seq:
|
49
|
+
# enum.max_by(by) => aValue
|
50
|
+
#
|
51
|
+
# Maximum #minmax_by.
|
52
|
+
def max_by(by)
|
53
|
+
minmax_by(:max, by)
|
54
|
+
end
|
55
|
+
|
56
|
+
# call-seq:
|
57
|
+
# enum.min_by(by) => aValue
|
58
|
+
#
|
59
|
+
# Minimum #minmax_by.
|
60
|
+
def min_by(by)
|
61
|
+
minmax_by(:min, by)
|
62
|
+
end
|
63
|
+
|
64
|
+
# call-seq:
|
65
|
+
# enum.minmax(meth, what) => anObject
|
66
|
+
#
|
67
|
+
# Finds the #minmax_by according to +what+ and returns that "what".
|
68
|
+
#
|
69
|
+
# Example:
|
70
|
+
# %w[a bcd ef].max(:length) #=> 3
|
71
|
+
def minmax(meth, what)
|
72
|
+
#m = minmax_by(meth, what)
|
73
|
+
#what.is_a?(Proc) ? what[m] : m.send(what)
|
74
|
+
|
75
|
+
_what = what.is_a?(::Proc) ? what : lambda { |i| i.send(what) }
|
76
|
+
map { |i| _what[i] }.send(meth)
|
77
|
+
|
78
|
+
# Benchmark (:max, :length; enum.size = 20, N = 100_000):
|
79
|
+
#
|
80
|
+
# max_by(:length).length 7.920000 0.890000 8.810000 ( 8.991915)
|
81
|
+
# map(:length).max 4.800000 0.600000 5.400000 ( 5.418114)
|
82
|
+
end
|
83
|
+
|
84
|
+
# call-seq:
|
85
|
+
# enum.max(what) => aValue
|
86
|
+
#
|
87
|
+
# Maximum #minmax. If +what+ is omitted, or +nil+, the original Enumerable#max
|
88
|
+
# is called.
|
89
|
+
def max(what = nil, &block)
|
90
|
+
what ? minmax(:max, what) : _nuggets_original_max(&block)
|
91
|
+
end
|
92
|
+
|
93
|
+
# call-seq:
|
94
|
+
# enum.min(what) => aValue
|
95
|
+
#
|
96
|
+
# Minimum #minmax. If +what+ is omitted, or +nil+, the original Enumerable#min
|
97
|
+
# is called.
|
98
|
+
def min(what = nil, &block)
|
99
|
+
what ? minmax(:min, what) : _nuggets_original_min(&block)
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# nuggets -- Extending Ruby #
|
5
|
+
# #
|
6
|
+
# Copyright (C) 2007-2011 Jens Wille #
|
7
|
+
# #
|
8
|
+
# Authors: #
|
9
|
+
# Jens Wille <jens.wille@gmail.com> #
|
10
|
+
# #
|
11
|
+
# nuggets is free software; you can redistribute it and/or modify it under #
|
12
|
+
# the terms of the GNU Affero General Public License as published by the Free #
|
13
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
14
|
+
# any later version. #
|
15
|
+
# #
|
16
|
+
# nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
|
17
|
+
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
|
18
|
+
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
|
19
|
+
# more details. #
|
20
|
+
# #
|
21
|
+
# You should have received a copy of the GNU Affero General Public License #
|
22
|
+
# along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
23
|
+
# #
|
24
|
+
###############################################################################
|
25
|
+
#++
|
26
|
+
|
27
|
+
module Nuggets
|
28
|
+
module Env
|
29
|
+
module SetMixin
|
30
|
+
|
31
|
+
# call-seq:
|
32
|
+
# ENV.set([env[, clear]]) => aHash
|
33
|
+
# ENV.set([env[, clear]]) { ... } => anObject
|
34
|
+
#
|
35
|
+
# Overrides ENV with +env+, clearing it beforehand if +clear+ is +true+. If a
|
36
|
+
# block is given, restores ENV to its original state afterwards and returns
|
37
|
+
# the result of the block; otherwise returns the original ENV as a hash.
|
38
|
+
def set(env = {}, clear = true)
|
39
|
+
old_env = to_hash
|
40
|
+
|
41
|
+
self.clear if clear
|
42
|
+
|
43
|
+
env.each { |key, value|
|
44
|
+
key = key.to_s.upcase unless key.is_a?(::String)
|
45
|
+
value = value.to_s unless value.is_a?(::String)
|
46
|
+
|
47
|
+
self[key] = value
|
48
|
+
}
|
49
|
+
|
50
|
+
block_given? ? yield : old_env
|
51
|
+
ensure
|
52
|
+
set(old_env) if old_env && block_given?
|
53
|
+
end
|
54
|
+
|
55
|
+
alias_method :without, :set
|
56
|
+
|
57
|
+
# call-seq:
|
58
|
+
# ENV.with([env[, clear]]) { ... } => anObject
|
59
|
+
#
|
60
|
+
# Temporarily overrides ENV with +env+ for the block execution. See #set.
|
61
|
+
def with(env = {}, clear = false)
|
62
|
+
set(env, clear) { yield }
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# nuggets -- Extending Ruby #
|
5
|
+
# #
|
6
|
+
# Copyright (C) 2007-2011 Jens Wille #
|
7
|
+
# #
|
8
|
+
# Authors: #
|
9
|
+
# Jens Wille <jens.wille@gmail.com> #
|
10
|
+
# #
|
11
|
+
# nuggets is free software; you can redistribute it and/or modify it under #
|
12
|
+
# the terms of the GNU Affero General Public License as published by the Free #
|
13
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
14
|
+
# any later version. #
|
15
|
+
# #
|
16
|
+
# nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
|
17
|
+
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
|
18
|
+
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
|
19
|
+
# more details. #
|
20
|
+
# #
|
21
|
+
# You should have received a copy of the GNU Affero General Public License #
|
22
|
+
# along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
23
|
+
# #
|
24
|
+
###############################################################################
|
25
|
+
#++
|
26
|
+
|
27
|
+
begin
|
28
|
+
require 'win32console'
|
29
|
+
rescue ::LoadError
|
30
|
+
end
|
31
|
+
|
32
|
+
module Nuggets
|
33
|
+
module Env
|
34
|
+
module UserEncodingMixin
|
35
|
+
|
36
|
+
# call-seq:
|
37
|
+
# ENV.user_encoding => aString
|
38
|
+
#
|
39
|
+
# Finds the user's selected encoding.
|
40
|
+
def user_encoding(default = 'UTF-8')
|
41
|
+
self['ENCODING'] || begin
|
42
|
+
lang = self['LANG']
|
43
|
+
lang[/\.(.*)/, 1] if lang
|
44
|
+
end || if defined?(::Win32::Console)
|
45
|
+
"CP#{::Win32::Console.InputCP}"
|
46
|
+
elsif ::File::ALT_SEPARATOR
|
47
|
+
cp = %x{chcp}[/:\s*(.*?)\./, 1]
|
48
|
+
"CP#{cp}" if cp
|
49
|
+
end || default
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|