ruby-nuggets 0.9.8.1 → 0.9.9

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.
Files changed (129) hide show
  1. checksums.yaml +4 -4
  2. data/README +1 -1
  3. data/Rakefile +9 -3
  4. data/lib/nuggets/all.rb +1 -1
  5. data/lib/nuggets/array/combination.rb +2 -25
  6. data/lib/nuggets/array/flatten_once.rb +1 -12
  7. data/lib/nuggets/array/format.rb +2 -60
  8. data/lib/nuggets/array/in_order.rb +1 -12
  9. data/lib/nuggets/array/monotone.rb +10 -27
  10. data/lib/nuggets/array/only.rb +1 -16
  11. data/lib/nuggets/array/shuffle.rb +1 -1
  12. data/lib/nuggets/array/to_hash.rb +1 -22
  13. data/lib/nuggets/cli.rb +2 -2
  14. data/lib/nuggets/content_type.rb +3 -8
  15. data/lib/nuggets/dotted_decimal.rb +0 -18
  16. data/lib/nuggets/enumerable/all_any_extended.rb +0 -27
  17. data/lib/nuggets/enumerable/minmax.rb +4 -25
  18. data/lib/nuggets/hash/at.rb +2 -20
  19. data/lib/nuggets/hash/deep_fetch.rb +5 -0
  20. data/lib/nuggets/hash/deep_fetch_mixin.rb +75 -0
  21. data/lib/nuggets/hash/in_order.rb +0 -8
  22. data/lib/nuggets/hash/insert.rb +2 -13
  23. data/lib/nuggets/hash/only.rb +2 -18
  24. data/lib/nuggets/i18n.rb +1 -10
  25. data/lib/nuggets/integer/factorial.rb +0 -17
  26. data/lib/nuggets/integer/to_binary_s.rb +0 -8
  27. data/lib/nuggets/lsi.rb +2 -2
  28. data/lib/nuggets/midos.rb +2 -2
  29. data/lib/nuggets/mysql.rb +6 -207
  30. data/lib/nuggets/numeric/duration.rb +0 -8
  31. data/lib/nuggets/numeric/limit.rb +1 -8
  32. data/lib/nuggets/numeric/signum.rb +0 -7
  33. data/lib/nuggets/numeric/to_multiple.rb +0 -6
  34. data/lib/nuggets/rdf/compression.rb +3 -89
  35. data/lib/nuggets/rdf/prefix.rb +6 -32
  36. data/lib/nuggets/rdf/turtle/reader.rb +5 -70
  37. data/lib/nuggets/rdf/turtle.rb +6 -122
  38. data/lib/nuggets/rdf/uri.rb +5 -37
  39. data/lib/nuggets/ruby.rb +39 -147
  40. data/lib/nuggets/string/capitalize_first.rb +2 -18
  41. data/lib/nuggets/string/case.rb +0 -22
  42. data/lib/nuggets/string/msub.rb +0 -18
  43. data/lib/nuggets/string/nsub.rb +2 -16
  44. data/lib/nuggets/string/sub_with_md.rb +6 -25
  45. data/lib/nuggets/string/word_wrap.rb +1 -35
  46. data/lib/nuggets/util/ansicolor2css.rb +1 -1
  47. data/lib/nuggets/util/cli.rb +1 -1
  48. data/lib/nuggets/util/content_type.rb +1 -1
  49. data/lib/nuggets/util/dotted_decimal.rb +1 -2
  50. data/lib/nuggets/util/i18n.rb +1 -1
  51. data/lib/nuggets/util/lazy_attr.rb +1 -1
  52. data/lib/nuggets/util/log_parser/apache.rb +1 -1
  53. data/lib/nuggets/util/log_parser/rails.rb +1 -1
  54. data/lib/nuggets/util/log_parser.rb +1 -1
  55. data/lib/nuggets/util/midos.rb +1 -1
  56. data/lib/nuggets/util/mysql.rb +1 -1
  57. data/lib/nuggets/util/pluggable.rb +1 -1
  58. data/lib/nuggets/util/ruby.rb +1 -1
  59. data/lib/nuggets/version.rb +2 -2
  60. data/spec/nuggets/array/boost_spec.rb +1 -3
  61. data/spec/nuggets/array/combination_spec.rb +25 -0
  62. data/spec/nuggets/array/correlation_spec.rb +1 -3
  63. data/spec/nuggets/array/flatten_once_spec.rb +16 -0
  64. data/spec/nuggets/array/flush_spec.rb +1 -3
  65. data/spec/nuggets/array/format_spec.rb +52 -0
  66. data/spec/nuggets/array/hashify_spec.rb +1 -3
  67. data/spec/nuggets/array/histogram_spec.rb +1 -3
  68. data/spec/nuggets/array/in_order_spec.rb +13 -0
  69. data/spec/nuggets/array/limit_spec.rb +1 -3
  70. data/spec/nuggets/array/mean_spec.rb +3 -5
  71. data/spec/nuggets/array/median_spec.rb +3 -5
  72. data/spec/nuggets/array/mode_spec.rb +1 -3
  73. data/spec/nuggets/array/monotone_spec.rb +30 -0
  74. data/spec/nuggets/array/only_spec.rb +26 -0
  75. data/spec/nuggets/array/regression_spec.rb +1 -3
  76. data/spec/nuggets/array/runiq_spec.rb +1 -3
  77. data/spec/nuggets/array/standard_deviation_spec.rb +1 -3
  78. data/spec/nuggets/array/to_hash_spec.rb +28 -0
  79. data/spec/nuggets/array/variance_spec.rb +3 -5
  80. data/spec/nuggets/dotted_decimal_spec.rb +27 -0
  81. data/spec/nuggets/enumerable/all_any_extended_spec.rb +31 -0
  82. data/spec/nuggets/enumerable/minmax_spec.rb +21 -0
  83. data/spec/nuggets/env/set_spec.rb +1 -3
  84. data/spec/nuggets/env/user_encoding_spec.rb +1 -3
  85. data/spec/nuggets/env/user_home_spec.rb +12 -10
  86. data/spec/nuggets/file/ext_spec.rb +1 -3
  87. data/spec/nuggets/file/replace_spec.rb +1 -3
  88. data/spec/nuggets/file/sub_spec.rb +1 -3
  89. data/spec/nuggets/file/which_spec.rb +1 -3
  90. data/spec/nuggets/hash/at_spec.rb +19 -0
  91. data/spec/nuggets/hash/deep_fetch_spec.rb +159 -0
  92. data/spec/nuggets/hash/deep_merge_spec.rb +1 -3
  93. data/spec/nuggets/hash/in_order_spec.rb +12 -0
  94. data/spec/nuggets/hash/insert_spec.rb +13 -0
  95. data/spec/nuggets/hash/nest_spec.rb +1 -3
  96. data/spec/nuggets/hash/only_spec.rb +29 -0
  97. data/spec/nuggets/hash/seen_spec.rb +4 -6
  98. data/spec/nuggets/hash/unroll_spec.rb +2 -4
  99. data/spec/nuggets/i18n_spec.rb +13 -0
  100. data/spec/nuggets/integer/factorial_spec.rb +10 -0
  101. data/spec/nuggets/integer/length_spec.rb +1 -3
  102. data/spec/nuggets/integer/map_spec.rb +1 -3
  103. data/spec/nuggets/integer/to_binary_s_spec.rb +19 -0
  104. data/spec/nuggets/numeric/duration_spec.rb +25 -0
  105. data/spec/nuggets/numeric/limit_spec.rb +16 -0
  106. data/spec/nuggets/numeric/signum_spec.rb +16 -0
  107. data/spec/nuggets/numeric/to_multiple_spec.rb +16 -0
  108. data/spec/nuggets/object/blank_spec.rb +1 -2
  109. data/spec/nuggets/object/boolean_spec.rb +1 -3
  110. data/spec/nuggets/object/msend_spec.rb +1 -3
  111. data/spec/nuggets/object/silence_spec.rb +7 -4
  112. data/spec/nuggets/object/singleton_class_spec.rb +11 -4
  113. data/spec/nuggets/proc/bind_spec.rb +1 -3
  114. data/spec/nuggets/range/quantile_spec.rb +1 -3
  115. data/spec/nuggets/string/camelscore_spec.rb +11 -13
  116. data/spec/nuggets/string/capitalize_first_spec.rb +13 -0
  117. data/spec/nuggets/string/case_spec.rb +31 -0
  118. data/spec/nuggets/string/evaluate_spec.rb +1 -3
  119. data/spec/nuggets/string/msub_spec.rb +20 -0
  120. data/spec/nuggets/string/nsub_spec.rb +13 -0
  121. data/spec/nuggets/string/sub_with_md_spec.rb +25 -0
  122. data/spec/nuggets/string/wc_spec.rb +1 -3
  123. data/spec/nuggets/string/word_wrap_spec.rb +81 -0
  124. data/spec/nuggets/string/xor_spec.rb +1 -3
  125. data/spec/nuggets/uri/content_type_spec.rb +32 -27
  126. data/spec/nuggets/uri/exist_spec.rb +33 -9
  127. data/spec/spec_helper.rb +17 -0
  128. metadata +34 -5
  129. data/.rspec +0 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 20e3a6fc4cdbc194e6fae2ce281cba22be27d19b
4
- data.tar.gz: f96c5db576f411f940de1effc136ba022699a13f
3
+ metadata.gz: efd0fc2a1e207d02a5c1615f520546f04670a134
4
+ data.tar.gz: 34a92b46ae4b4201598feef5fb9ff329fba2f98d
5
5
  SHA512:
6
- metadata.gz: d6b4aa7f85cb7cd2ca92ffe24cac338dcb45dd7264b63e5beb855a7d62b45da42c4779111164a08f6bea6a109c71495e4363c92b2f57cbb3f8b15ef7e8856f78
7
- data.tar.gz: aff5162458b1fa163a0a8142ddf16720ec0a08c975535d3e2a1f0c312015e0979197a25451aba6807a08c44463bbfdf344f2b582498393a6d59e2e2bb6581546
6
+ metadata.gz: bdaa6f53862c08efa393837dbdd2dcabea561911141ceecdeec1221e0866ed531da04c15bc850d72d3ec0a82b4ace4066b9832bb3c2368fcddc002f5453a5f32
7
+ data.tar.gz: a9db082c3a292393906f1c1c8151aff57cf719abd9c36ebf698808363105dfcad662c506efd4b4aa498670329a9992bf342012a987cb92ca51b1cec61b12e6a6
data/README CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to ruby-nuggets version 0.9.8
5
+ This documentation refers to ruby-nuggets version 0.9.9
6
6
 
7
7
 
8
8
  == DESCRIPTION
data/Rakefile CHANGED
@@ -18,9 +18,6 @@ begin
18
18
  #'amatch', # enumerable/agrep
19
19
  'mime-types', # content_type
20
20
  'open4', # ruby
21
- #'rbzip2', # rdf/compression
22
- #'rdf', # rdf/{prefix,uri,compression}
23
- #'rdf-turtle', # rdf/turtle
24
21
  #'ruby-filemagic' # content_type
25
22
  ]
26
23
  }
@@ -39,3 +36,12 @@ task 'spec:isolated' do
39
36
  system($0, *ARGV)
40
37
  }
41
38
  end
39
+
40
+ if RUBY_PLATFORM == 'java'
41
+ task :spec => :enable_objspace
42
+
43
+ task :enable_objspace do
44
+ k, v = 'JRUBY_OPTS', '-X+O'
45
+ ENV[k] ? ENV[k] += " #{v}" : ENV[k] = v
46
+ end
47
+ end
data/lib/nuggets/all.rb CHANGED
@@ -29,7 +29,7 @@ base = ::File.dirname(__FILE__)
29
29
  base_re = ::Regexp.escape(base)
30
30
 
31
31
  ::Dir[::File.join(base, %w[* ** *.rb])].sort.each { |path|
32
- next if path =~ /_mixin\.rb\z/
32
+ next if path.include?('/util/') || path.end_with?('_mixin.rb')
33
33
 
34
34
  ext_re = ::Regexp.escape(::File.extname(path))
35
35
  require path.sub(/#{base_re}(.*)#{ext_re}/, 'nuggets\1')
@@ -38,14 +38,9 @@ class Array
38
38
  # If no sizes are given, produce all!
39
39
  sizes = (0..size).to_a.reverse if sizes.empty?
40
40
 
41
- # Container for our combinations
42
- combinations = []
43
-
44
- # Collect combinations and, optionally, yield to block.
45
- collect_and_yield = lambda { |combination|
41
+ combinations, collect_and_yield = [], lambda { |combination|
42
+ yield combination if block_given?
46
43
  combinations << combination
47
-
48
- yield(combination) if block_given?
49
44
  }
50
45
 
51
46
  sizes.each { |n|
@@ -62,25 +57,7 @@ class Array
62
57
  end
63
58
  }
64
59
 
65
- # Anyway, return what we've found...
66
60
  combinations
67
61
  end
68
62
 
69
63
  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
@@ -51,18 +51,7 @@ class Array
51
51
  #
52
52
  # Destructive version of #flatten_once.
53
53
  def flatten_once!
54
- replace flatten_once
54
+ replace(flatten_once)
55
55
  end
56
56
 
57
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
@@ -44,9 +44,7 @@ class Array
44
44
  opts = { :sep => ', ' }
45
45
  opts.update(pop) if last.is_a?(::Hash)
46
46
 
47
- default = lambda { |n|
48
- ['%s'] * n * opts[:sep]
49
- }
47
+ default = lambda { |n| ['%s'] * n * opts[:sep] }
50
48
 
51
49
  case args
52
50
  when ::String
@@ -56,9 +54,7 @@ class Array
56
54
  i = 0
57
55
  [*args].comb { |x|
58
56
  return (self[i] || default[x.size]) % x unless
59
- x.empty? || x.any? { |y|
60
- y.nil? || y.empty?
61
- }
57
+ x.empty? || x.any? { |y| y.nil? || y.empty? }
62
58
 
63
59
  i += 1
64
60
  }
@@ -68,57 +64,3 @@ class Array
68
64
  end
69
65
 
70
66
  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
@@ -45,18 +45,7 @@ class Array
45
45
  #
46
46
  # Destructive version of #in_order.
47
47
  def in_order!(*ordered)
48
- replace in_order(*ordered)
48
+ replace(in_order(*ordered))
49
49
  end
50
50
 
51
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
@@ -31,13 +31,17 @@ class Array
31
31
  # array.monotone?(operator) => +true+ or +false+
32
32
  #
33
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
- }
34
+ def monotone?(operator = nil)
35
+ if [nil, true, false].include?(operator)
36
+ ascending?(operator) || descending?(operator)
37
+ else
38
+ inject { |a, b|
39
+ return false unless a.send(operator, b)
40
+ b
41
+ }
39
42
 
40
- true
43
+ true
44
+ end
41
45
  end
42
46
  alias_method :monotonic?, :monotone?
43
47
 
@@ -78,24 +82,3 @@ class Array
78
82
  alias_method :strictly_decreasing?, :strictly_descending?
79
83
 
80
84
  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
@@ -36,22 +36,7 @@ class Array
36
36
  #
37
37
  # Idea stolen from Gavin Sinclair's Ruby Extensions Project.
38
38
  def only(relax = size == 1)
39
- raise ::IndexError, 'not a single-element array' unless relax
40
- first
39
+ relax ? first : raise(::IndexError, 'not a single-element array')
41
40
  end
42
41
 
43
42
  end
44
-
45
- if $0 == __FILE__
46
- [[5], [1, 2, 3], []].each { |a|
47
- p a
48
-
49
- begin
50
- p a.only
51
- rescue ::IndexError => err
52
- warn err
53
- end
54
-
55
- p a.only(true)
56
- }
57
- end
@@ -123,7 +123,7 @@ if $0 == __FILE__
123
123
  x2 = 0
124
124
  score.sort.each { |key, value|
125
125
  x = value - e
126
- y = x**2 / e
126
+ y = x ** 2 / e
127
127
  #puts '%s: %d (% .2f/%.2f)' % [key, value, x, y]
128
128
 
129
129
  x2 += y
@@ -59,28 +59,7 @@ class Array
59
59
 
60
60
  hash
61
61
  end
62
- alias_method :to_h, :to_hash
63
-
64
- end
65
62
 
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 }
63
+ alias_method :to_h, :to_hash
80
64
 
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
65
  end
data/lib/nuggets/cli.rb CHANGED
@@ -2,7 +2,7 @@ begin
2
2
  require 'cyclops'
3
3
  module Nuggets; CLI = ::Cyclops; end
4
4
 
5
- warn "#{__FILE__}: Nuggets::CLI is deprecated, use Cyclops instead."
5
+ warn "#{__FILE__}: Nuggets::CLI is deprecated, use Cyclops instead." unless ENV['NUGGETS_DEPRECATED_CLI']
6
6
  rescue LoadError => err
7
- warn "#{__FILE__}: Nuggets::CLI is no longer available; install `cyclops' instead. (#{err})"
7
+ warn "#{__FILE__}: Nuggets::CLI is no longer available; install `cyclops' instead. (#{err})" unless ENV['NUGGETS_DEPRECATED_CLI']
8
8
  end
@@ -25,14 +25,9 @@
25
25
  ###############################################################################
26
26
  #++
27
27
 
28
- begin
29
- require 'rubygems'
30
- rescue ::LoadError
31
- end
32
-
33
28
  begin
34
29
  require 'filemagic/ext'
35
- rescue ::LoadError
30
+ rescue LoadError
36
31
  def File.content_type(path) # :nodoc:
37
32
  nil
38
33
  end
@@ -40,7 +35,7 @@ end
40
35
 
41
36
  begin
42
37
  require 'nuggets/uri/content_type'
43
- rescue ::LoadError
38
+ rescue LoadError
44
39
  module URI
45
40
  def self.content_type(path) # :nodoc:
46
41
  nil
@@ -50,7 +45,7 @@ end
50
45
 
51
46
  begin
52
47
  require 'mime/types'
53
- rescue ::LoadError
48
+ rescue LoadError
54
49
  module MIME # :nodoc:
55
50
  class Types # :nodoc:
56
51
  def self.of(path)
@@ -58,21 +58,3 @@ class Array
58
58
  end
59
59
 
60
60
  end
61
-
62
- if $0 == __FILE__
63
- [2294967042, 4294967040].each { |i|
64
- p i.to_binary_s(32)
65
- p i.to_dotted_decimal
66
- }
67
-
68
- puts '#' * 34
69
-
70
- %w[77.47.161.3 196.101.53.1].each { |s|
71
- p s
72
- p s.from_dotted_decimal.to_binary_s(32)
73
- }
74
-
75
- a = %w[77.47.161.3 196.101.53.1 77.47.161.11]
76
- p a.sort
77
- p a.sort_by_dotted_decimal
78
- end
@@ -65,30 +65,3 @@ module Enumerable
65
65
  end
66
66
 
67
67
  end
68
-
69
- if $0 == __FILE__
70
- e = %w[quux quuux quix]
71
- p e
72
-
73
- p e.all?(::String)
74
- p e.any?(::Numeric)
75
-
76
- e = [:one, 'c', nil, 88]
77
- p e
78
-
79
- p e.all?(::Object)
80
- p e.any?(::NilClass)
81
-
82
- begin
83
- e.any?(::NilClass) { |i| i.nil? }
84
- rescue ::ArgumentError => err
85
- puts "#{err.backtrace.first}: #{err} (#{err.class})"
86
- end
87
-
88
- e = [0, 10, 20]
89
- p e
90
-
91
- p e.any?(9..99)
92
- p e.any?(9, :<)
93
- p e.any? { |i| i < 9 }
94
- end
@@ -87,9 +87,8 @@ module Enumerable
87
87
  #
88
88
  # Maximum #minmax. If +what+ is omitted, or +nil+, the original Enumerable#max
89
89
  # is called.
90
- def max(what = nil)
91
- what ? minmax(:max, what) : block_given? ?
92
- _nuggets_original_max { |*a| yield(*a) } : _nuggets_original_max
90
+ def max(what = nil, &block)
91
+ what ? minmax(:max, what) : _nuggets_original_max(&block)
93
92
  end
94
93
 
95
94
  # call-seq:
@@ -97,28 +96,8 @@ module Enumerable
97
96
  #
98
97
  # Minimum #minmax. If +what+ is omitted, or +nil+, the original Enumerable#min
99
98
  # is called.
100
- def min(what = nil)
101
- what ? minmax(:min, what) : block_given? ?
102
- _nuggets_original_min { |*a| yield(*a) } : _nuggets_original_min
99
+ def min(what = nil, &block)
100
+ what ? minmax(:min, what) : _nuggets_original_min(&block)
103
101
  end
104
102
 
105
103
  end
106
-
107
- if $0 == __FILE__
108
- e = %w[quux quuux quix]
109
- p e
110
-
111
- p e.max
112
- p e.max_by(:length)
113
- p e.max(:length)
114
-
115
- e = [3, 222, 45]
116
- p e
117
-
118
- # the last digit counts ;-)
119
- l = lambda { |i| i.to_s.split(//).last.to_i }
120
-
121
- p e.max
122
- p e.max_by(l)
123
- p e.max(l)
124
- end
@@ -34,15 +34,10 @@ class Hash
34
34
  #
35
35
  # Returns the key/value pair of _hash_ at key position +what+. Remember that
36
36
  # hashes might not have the intended (or expected) order in pre-1.9 Ruby.
37
- def at(what)
37
+ def at(what, &block)
38
38
  return {} if empty?
39
39
 
40
- key = case what
41
- when ::Integer
42
- keys[what]
43
- else
44
- block_given? ? keys.send(*what) { |*a| yield(*a) } : keys.send(*what)
45
- end
40
+ key = what.is_a?(::Integer) ? keys[what] : keys.send(*what, &block)
46
41
 
47
42
  { key => self[key] }
48
43
  end
@@ -72,16 +67,3 @@ class Hash
72
67
  end
73
68
 
74
69
  end
75
-
76
- if $0 == __FILE__
77
- h = { :a => 1, 2 => 3, nil => nil, 'foo' => %w[b a r]}
78
- p h
79
-
80
- p h.first
81
- p h.last
82
- p h.rand
83
-
84
- p h.at(0)
85
- p h.at(1)
86
- p h.at(-1)
87
- end
@@ -0,0 +1,5 @@
1
+ require 'nuggets/hash/deep_fetch_mixin'
2
+
3
+ class Hash
4
+ include Nuggets::Hash::DeepFetchMixin
5
+ end
@@ -0,0 +1,75 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of ruby-nuggets, some extensions to the Ruby programming #
5
+ # language. #
6
+ # #
7
+ # Copyright (C) 2007-2014 Jens Wille #
8
+ # #
9
+ # Authors: #
10
+ # Jens Wille <jens.wille@gmail.com> #
11
+ # #
12
+ # ruby-nuggets is free software; you can redistribute it and/or modify it #
13
+ # under the terms of the GNU Affero General Public License as published by #
14
+ # the Free Software Foundation; either version 3 of the License, or (at your #
15
+ # option) 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 Affero General Public License #
20
+ # for more details. #
21
+ # #
22
+ # You should have received a copy of the GNU Affero General Public License #
23
+ # along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
24
+ # #
25
+ ###############################################################################
26
+ #++
27
+
28
+ module Nuggets
29
+ class Hash
30
+ module DeepFetchMixin
31
+
32
+ # call-seq:
33
+ # hash.deep_fetch(path[, separator]) => anObject
34
+ # hash.deep_fetch(path[, separator]) { |key| ... } => anObject
35
+ #
36
+ # Recursively fetches keys in +path+, separated by +separator+
37
+ # if +path+ is not an array, from _hash_. Maps each individual
38
+ # key according to the block if provided.
39
+ #
40
+ # Examples:
41
+ #
42
+ # hash = { 'foo' => { 'bar' => { 'baz' => 42 }, 'bay' => 23 } }
43
+ # hash.deep_fetch('foo/bar/baz') #=> 42
44
+ # hash.deep_fetch('foo/bar/bax') #=> nil
45
+ # hash.deep_fetch('foo/bax/baz') #=> KeyError
46
+ # hash.deep_fetch('foo/bay/baz') #=> TypeError
47
+ # hash % 'foo/bar/baz' #=> 42
48
+ # hash % %w[foo bar baz] #=> 42
49
+ #
50
+ # hash = { foo: { bar: { baz: 42 } } }
51
+ # hash.deep_fetch('foo/bar/baz', &:to_sym) #=> 42
52
+ # hash % [:foo, :bar, :baz] #=> 42
53
+ def deep_fetch(path, separator = '/')
54
+ keys = path.is_a?(::Array) ? path : path.split(separator)
55
+ raise ::ArgumentError, 'no keys given' if keys.empty?
56
+
57
+ hash, klass = self, self.class
58
+
59
+ loop {
60
+ key = keys.shift
61
+ key = yield key if block_given?
62
+
63
+ return hash[key] if keys.empty?
64
+
65
+ unless (hash = hash.fetch(key)).is_a?(klass)
66
+ raise ::TypeError, '%p: %s expected, got %s' % [key, klass, hash.class]
67
+ end
68
+ }
69
+ end
70
+
71
+ alias_method :%, :deep_fetch
72
+
73
+ end
74
+ end
75
+ end
@@ -42,11 +42,3 @@ class Hash
42
42
  end
43
43
 
44
44
  end
45
-
46
- if $0 == __FILE__
47
- a = { :a => 1, :b => 2, :c => 3 }
48
- p a
49
-
50
- p a.in_order(:b, :c)
51
- p a.in_order(:b, :d)
52
- end
@@ -47,19 +47,8 @@ class Hash
47
47
  # hash.insert!(other) { |key, old_value, new_value| ... } => hash
48
48
  #
49
49
  # Destructive version of #insert.
50
- def insert!(other)
51
- replace block_given? ? insert(other) { |*a| yield(*a) } : insert(other)
50
+ def insert!(other, &block)
51
+ replace(insert(other, &block))
52
52
  end
53
53
 
54
54
  end
55
-
56
- if $0 == __FILE__
57
- h = { :a => 0, :b => { :b1 => 1, :b2 => 2 } }
58
- p h
59
-
60
- p h.insert(:a => -1)
61
- p h.insert(:b => { :b3 => 3 })
62
-
63
- h.insert!(:b => { :b0 => 0 })
64
- p h
65
- end
@@ -36,9 +36,8 @@ class Hash
36
36
  # Returns the only key/value pair of _hash_. Raises an IndexError if _hash_'s
37
37
  # size is not 1, unless parameter +true+ is passed.
38
38
  def only(relax = size == 1, split = false)
39
- raise ::IndexError, 'not a single-element hash' unless relax
40
-
41
- split ? Array(*first) : first
39
+ relax ? split ? Array(*first) : first :
40
+ raise(::IndexError, 'not a single-element hash')
42
41
  end
43
42
 
44
43
  # call-seq:
@@ -52,18 +51,3 @@ class Hash
52
51
  end
53
52
 
54
53
  end
55
-
56
- if $0 == __FILE__
57
- [{ :a => 5 }, { 1 => 2, 3 => 4 }, {}].each { |h|
58
- p h
59
-
60
- begin
61
- p h.only
62
- p h.only_pair
63
- rescue ::IndexError => err
64
- warn err
65
- end
66
-
67
- p h.only(true)
68
- }
69
- end
data/lib/nuggets/i18n.rb CHANGED
@@ -136,7 +136,7 @@ class String
136
136
  # But:
137
137
  # 'SÖS' => 'SOES' (not 'SOeS'!)
138
138
  if s.length > 1
139
- t = $'[0..0]
139
+ t = $'[0, 1]
140
140
  s[1..-1] = s[1..-1].downcase if t == t.downcase
141
141
  end
142
142
 
@@ -154,12 +154,3 @@ class String
154
154
  end
155
155
 
156
156
  end
157
-
158
- if $0 == __FILE__
159
- s = 'Äh, Rüby iß sö cüül, nö? SÖS!'
160
- p s
161
- p s.replace_diacritics
162
-
163
- s.replace_diacritics!
164
- p s
165
- end