Module: Knj::ArrayExt
- Defined in:
- lib/knj/arrayext.rb
Class Method Summary collapse
-
.array_hash(arr) ⇒ Object
Returns a hash based on the string-values of an array.
-
.array_hash_find(args) ⇒ Object
Returns a given hash in an array by mathing the contents up against another hash.
-
.array_to_hash(arr) ⇒ Object
Converts an array to a hash with the index a string-numbers.
-
.clone_encode(hash, encoding, args = {}) ⇒ Object
Forces all strings in an array or a hash to be encoded to a specific encoding recursively.
- .dict(arr) ⇒ Object
-
.divide(args, &block) ⇒ Object
Divides an array based on callback.
-
.force_no_cols(args) ⇒ Object
Forces an array to have a certain amount of columns.
-
.hash_diff?(h1, h2, args = {}) ⇒ Boolean
Compares the keys and values of two hashes and returns true if they are different.
-
.hash_keys_hash(hash) ⇒ Object
Returns a hash based on the string-keys in a hash.
-
.hash_md5(hash) ⇒ Object
Returns a hash based on the keys and values of a hash.
-
.hash_numeric_keys?(hash) ⇒ Boolean
Returns true if all keys in a hash are numeric objects.
-
.hash_sort(hash, &block) ⇒ Object
Sorts the hash without being a retard…
-
.hash_str(hash_given) ⇒ Object
Converts all keys in the given hash to strings.
-
.hash_sym(hash_given) ⇒ Object
Converts all keys in the given hash to symbols.
-
.hash_values_hash(hash) ⇒ Object
Returns hash based on the string-values in a hash.
- .join(args = {}, key = nil, sep = nil) ⇒ Object
-
.powerset(args) ⇒ Object
Returns a powerset of the given array.
-
.validate_hash(h, args) ⇒ Object
Validates a hash of data.
Class Method Details
.array_hash(arr) ⇒ Object
Returns a hash based on the string-values of an array.
181 182 183 184 185 186 187 188 |
# File 'lib/knj/arrayext.rb', line 181 def self.array_hash(arr) hashes = [] arr.each do |ele| hashes << Digest::MD5.hexdigest(ele.to_s) end return Digest::MD5.hexdigest(hashes.join("_")) end |
.array_hash_find(args) ⇒ Object
Returns a given hash in an array by mathing the contents up against another hash.
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/knj/arrayext.rb', line 341 def self.array_hash_find(args) args[:arr].each do |h| found = true args[:args].each do |key, val| if h[key] != val found = false break end end return h if found end return nil end |
.array_to_hash(arr) ⇒ Object
Converts an array to a hash with the index a string-numbers.
49 50 51 52 53 54 55 56 |
# File 'lib/knj/arrayext.rb', line 49 def self.array_to_hash(arr) ret = {} arr.each do |item| ret[ret.length.to_s] = item end return ret end |
.clone_encode(hash, encoding, args = {}) ⇒ Object
Forces all strings in an array or a hash to be encoded to a specific encoding recursively.
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/knj/arrayext.rb', line 212 def self.clone_encode(hash, encoding, args = {}) return hash if !hash require "php4r" if !Kernel.const_defined?(:Php4r) hash = hash.clone Php4r.foreach(hash) do |key, val| if val.is_a?(String) begin hash[key] = Php4r.utf8_encode(val) rescue Encoding::UndefinedConversionError => e if args["ignore_encoding_errors"] next else raise e end end elsif val.is_a?(Array) or val.is_a?(Hash) hash[key] = Knj::ArrayExt.clone_encode(val, encoding, args) end end return hash end |
.dict(arr) ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lib/knj/arrayext.rb', line 104 def self.dict(arr) ret = Dictionary.new arr.each do |item| ret[ret.length.to_s] = item end return ret end |
.divide(args, &block) ⇒ Object
Divides an array based on callback.
Examples
arr = [1, 2, 3, 4, 6, 7, 8, 9, 15, 16, 17, 18] res = Knj::ArrayExt.divide(:arr => arr) do |a, b|
if (b - a) > 1
false
else
true
end
end
res.length #=> 3
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/knj/arrayext.rb', line 292 def self.divide(args, &block) prev_ele = args[:arr].shift chunk = [prev_ele] if !args[:evaluate] callback_eval = block elsif !args[:callback] and args[:evaluate] callback_res = block callback_eval = args[:evaluate] end ret = [] if !args[:callback] args[:arr].each do |ele| if !chunk chunk = [ele] prev_ele = ele next end if callback_eval.call(prev_ele, ele) chunk << ele elsif callback_res callback_res.call(chunk) chunk = nil else ret << chunk chunk = nil end prev_ele = ele end if chunk and !chunk.empty? if callback_res callback_res.call(chunk) else ret << chunk end end if args[:callback] return nil else return ret end end |
.force_no_cols(args) ⇒ Object
Forces an array to have a certain amount of columns.
Examples
arr = [1, 2, 3, 4, 5] Knj::ArrayExt.force_no_cols(:arr => arr, :no => 4) #=> [1, 2, 3, 4]
240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/knj/arrayext.rb', line 240 def self.force_no_cols(args) while args[:arr].length > args[:no] args[:arr].slice!(-1) end while args[:arr].length < args[:no] args[:arr] << args[:empty] end return nil end |
.hash_diff?(h1, h2, args = {}) ⇒ Boolean
Compares the keys and values of two hashes and returns true if they are different.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/knj/arrayext.rb', line 128 def self.hash_diff?(h1, h2, args = {}) if !args.key?("h1_to_h2") or args["h1_to_h2"] h1.each do |key, val| return true if !h2.key?(key) hash_val = h2[key].to_s hash_val = hash_val.force_encoding("UTF-8") if hash_val.respond_to?(:force_encoding) val = val.to_s val = val.force_encoding("UTF-8") if val.respond_to?(:force_encoding) return true if hash_val != val end end if !args.key?("h2_to_h1") or args["h2_to_h1"] h2.each do |key, val| return true if !h1.key?(key) hash_val = h1[key].to_s hash_val = hash_val.force_encoding("UTF-8") if hash_val.respond_to?(:force_encoding) val = val.to_s val = val.force_encoding("UTF-8") if val.respond_to?(:force_encoding) return true if hash_val != val end end return false end |
.hash_keys_hash(hash) ⇒ Object
Returns a hash based on the string-keys in a hash.
161 162 163 164 165 166 167 168 |
# File 'lib/knj/arrayext.rb', line 161 def self.hash_keys_hash(hash) hashes = [] hash.keys.sort.each do |key| hashes << Digest::MD5.hexdigest(key.to_s) end return Digest::MD5.hexdigest(hashes.join("_")) end |
.hash_md5(hash) ⇒ Object
Returns a hash based on the keys and values of a hash.
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/knj/arrayext.rb', line 114 def self.hash_md5(hash) combined_val = "" hash.each do |key, val| if combined_val.length > 0 combined_val << ";" end combined_val << "#{key}:#{val}" end return Digest::MD5.hexdigest(combined_val) end |
.hash_numeric_keys?(hash) ⇒ Boolean
Returns true if all keys in a hash are numeric objects.
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/knj/arrayext.rb', line 59 def self.hash_numeric_keys?(hash) all_num = true hash.each do |key, val| if !(Float(key) rescue false) all_num = false break end end return all_num end |
.hash_sort(hash, &block) ⇒ Object
Sorts the hash without being a retard…
200 201 202 203 204 205 206 207 208 209 |
# File 'lib/knj/arrayext.rb', line 200 def self.hash_sort(hash, &block) sorted = hash.sort(&block) ret = {} sorted.each do |ele| ret[ele[0]] = ele[1] end return ret end |
.hash_str(hash_given) ⇒ Object
Converts all keys in the given hash to strings.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/knj/arrayext.rb', line 89 def self.hash_str(hash_given) adds = {} hash_given.each do |key, val| if !key.is_a?(String) adds[key.to_s] = val hash_given.delete(key) end end hash_given.merge!(adds) return hash_given end |
.hash_sym(hash_given) ⇒ Object
Converts all keys in the given hash to symbols.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/knj/arrayext.rb', line 72 def self.hash_sym(hash_given) raise "Invalid argument-class: '#{hash_given.class.name}'." if !hash_given or !hash_given.respond_to?(:each) adds = {} hash_given.each do |key, value| if !key.is_a?(Symbol) adds[key.to_sym] = value hash_given.delete(key) end end hash_given.merge!(adds) return hash_given end |
.hash_values_hash(hash) ⇒ Object
Returns hash based on the string-values in a hash.
171 172 173 174 175 176 177 178 |
# File 'lib/knj/arrayext.rb', line 171 def self.hash_values_hash(hash) hashes = [] hash.keys.sort.each do |key| hashes << Digest::MD5.hexdigest(hash[key].to_s) end return Digest::MD5.hexdigest(hashes.join("_")) end |
.join(args = {}, key = nil, sep = nil) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/knj/arrayext.rb', line 2 def self.join(args = {}, key = nil, sep = nil) if args.is_a?(Array) and sep args = { :arr => args, :sep => sep, :key => key } end raise "No seperator given." if !args[:sep] str = "" first = true require "php4r" if !Kernel.const_defined?(:Php4r) Php4r.foreach(args[:arr]) do |key, value| if first first = false else str << args[:sep] end if args[:key] value = key else value = value end if args[:callback] if args[:callback].is_a?(Proc) or args[:callback].is_a?(Method) value = args[:callback].call(value) else value = Php4r.call_user_func(args[:callback], value) if args[:callback] end end raise "Callback returned nothing." if args[:callback] and !value str << args[:surr] if args[:surr] str << value.to_s str << args[:surr] if args[:surr] end return str end |
.powerset(args) ⇒ Object
Returns a powerset of the given array. Copied from ‘mikeburnscoder.wordpress.com/2009/05/30/powerset-in-ruby-using-the-list-monad/’.
Examples
ps = Knj::ArrayExt.powerset(:arr => [1, 2 , 3, 4]) ps.length #=> 16
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/knj/arrayext.rb', line 256 def self.powerset(args) arr = args[:arr] raise "No array was given." if !arr if block_given? if arr.length == 0 yield [] else Knj::ArrayExt.powerset(:arr => arr[0..-2]) do |set| yield set yield set + [arr[-1]] end end arr else Enumerator.new do |sets| Knj::ArrayExt.powerset(:arr => arr) do |set| sets << set end end end end |
.validate_hash(h, args) ⇒ Object
Validates a hash of data.
191 192 193 194 195 196 197 |
# File 'lib/knj/arrayext.rb', line 191 def self.validate_hash(h, args) h.each do |key, val| if args.key?(:not_empty) and args[:not_empty].index(key) != nil and val.to_s.strip.length <= 0 raise ArgumentError, sprintf(args[:not_empty_error], key) end end end |