Module: Seda
- Defined in:
- lib/seda.rb,
lib/seda/version.rb
Constant Summary collapse
- VERSION =
"0.2.0"
Class Method Summary collapse
- .contains(collection, target) ⇒ Object
- .defaults(*obj) ⇒ Object
- .delay(some_method, wait, *some_method_args) ⇒ Object
- .difference(*arrays) ⇒ Object
- .every(collection, some_method) ⇒ Object
- .extend(*obj) ⇒ Object
- .filter(collection, some_method) ⇒ Object
- .first(arr, n_first_elements) ⇒ Object
- .flatten(nested_array, result = []) ⇒ Object
- .identity(value) ⇒ Object
- .index_of(arr, target) ⇒ Object
- .intersection(*arrays) ⇒ Object
- .last(arr, n) ⇒ Object
- .map(collection, some_method) ⇒ Object
- .memoize(some_method, *some_method_args) ⇒ Object
- .pluck(collection, key) ⇒ Object
- .reduce(collection, some_method) ⇒ Object
- .reject(collection, some_method) ⇒ Object
- .shuffle(arr) ⇒ Object
- .some(collection, some_method) ⇒ Object
- .sort_by_max(collection) ⇒ Object
- .sort_by_min(collection) ⇒ Object
Class Method Details
.contains(collection, target) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/seda.rb', line 29 def self.contains(collection, target) collection.each do |element| if element == target return true end end false end |
.defaults(*obj) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/seda.rb', line 39 def self.defaults(*obj) final_object = (eval(local_variables[0].to_s))[0] iterables = (eval(local_variables[0].to_s))[1..-1] if final_object.class == Hash && iterables.class == Array iterables.each do |element| if element.class == Hash for k in element if final_object[k[0].to_sym] == nil final_object[k[0].to_sym] = k[1] end end elsif element.class == Array element.each do |item| if final_object[item.to_sym] == nil final_object[item.to_sym] = item end end else if final_object[element.to_sym] == nil final_object[element.to_sym] = element end end end else raise ArgumentError.new('Must pass in the destination Hash as first argument, and additional argument objects to extend values from') end return final_object end |
.delay(some_method, wait, *some_method_args) ⇒ Object
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/seda.rb', line 383 def self.delay(some_method, wait, *some_method_args) args = eval(local_variables[2].to_s) if some_method.class == String some_method = some_method.to_sym some_method = method(some_method) elsif some_method.class == Symbol some_method = method(some_method) end if some_method.class == Method sleep (wait.to_f/1000) some_method.call(*args) else raise ArgumentError.new('First argument must reference a method') end end |
.difference(*arrays) ⇒ Object
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/seda.rb', line 359 def self.difference(*arrays) checker = (eval(local_variables[0].to_s))[0] iterables = (eval(local_variables[0].to_s))[1..-1] arr = [] seen = {} checker.each do |element| seen[element] = element arr << element end iterables.each do |inner_arr| inner_arr.each do |element| if seen[element] arr = arr.reject{|el| el == element} end end end arr end |
.every(collection, some_method) ⇒ Object
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/seda.rb', line 402 def self.every(collection, some_method) collection.each do |item| if some_method.class == Method if !some_method.call(item) return false end elsif some_method.class == Symbol if !method(some_method).call(item) return false end elsif some_method.class == String if !method(some_method.to_sym).call(item) return false end else raise ArgumentError.new('First argument must reference a method') end end return true end |
.extend(*obj) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/seda.rb', line 71 def self.extend(*obj) initial_obj = (eval(local_variables[0].to_s))[0] iterables = (eval(local_variables[0].to_s))[1..-1] final_object = {} if initial_obj.class == Hash initial_obj.each do |k, v| final_object[k.to_s] = v end if iterables.class == Array iterables.each do |element| if element.class == Hash for k in element final_object[k[0].to_s] = k[1] end elsif element.class == Array element.each do |item| final_object[item.to_s] = item end else final_object[element.to_s] = element end end end else raise ArgumentError.new('Must pass in the destination Hash as first argument, and additional argument objects to extend values from') end return final_object end |
.filter(collection, some_method) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/seda.rb', line 148 def self.filter(collection, some_method) arr = [] result_obj = {} if collection.class == Array collection.each_with_index do |element, idx| if some_method.class == String if method(some_method.to_sym).call(collection, idx) arr << element end elsif some_method.class == Symbol if method(some_method).call(collection, idx) arr << element end elsif some_method.class == Method if some_method.call(collection, idx) arr << element end else raise ArgumentError.new('The second argument must be a method. You could pass in the actual method, a string of the method name, or a symbol of the method name. Both arguments are needed.') end end return arr elsif collection.class == Hash for k in collection if some_method.class == String if method(some_method.to_sym).call(k[1]) result_obj[k[0]] = k[1] end elsif some_method.class == Symbol if method(some_method).call(k[1]) result_obj[k[0]] = k[1] end elsif some_method.class == Method if some_method.call(k[1]) result_obj[k[0]] = k[1] end else raise ArgumentError.new('The second argument must be a method. You could pass in the actual method, a string of the method name, or a symbol of the method name. Both arguments are needed.') end end return result_obj else raise ArgumentError.new('Must provide an Array, or a Hash as the first Argument.') end end |
.first(arr, n_first_elements) ⇒ Object
9 10 11 |
# File 'lib/seda.rb', line 9 def self.first(arr, n_first_elements) n_first_elements == nil ? arr[0] : arr.slice(0, n_first_elements) end |
.flatten(nested_array, result = []) ⇒ Object
424 425 426 427 428 429 430 431 432 433 434 |
# File 'lib/seda.rb', line 424 def self.flatten(nested_array, result = []) nested_array.each do |element| if element.class == Array self.flatten(element, result) else result << element end end result end |
.identity(value) ⇒ Object
5 6 7 |
# File 'lib/seda.rb', line 5 def self.identity(value) value end |
.index_of(arr, target) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/seda.rb', line 13 def self.index_of(arr, target) if(arr == nil) return arr end result = -1 arr.each_with_index do |element, i| if(element == target && result == -1) result = i end end result end |
.intersection(*arrays) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/seda.rb', line 103 def self.intersection(*arrays) result = [] seen = {} iterables = (eval(local_variables[0].to_s))[0..-1] iterables.flatten.each do |element| seen[element] == nil ? seen[element] = 1 : seen[element] += 1 end for k in seen if k[1] == iterables.length result << k[0] end end result end |
.last(arr, n) ⇒ Object
123 124 125 126 127 128 129 |
# File 'lib/seda.rb', line 123 def self.last(arr, n) if(n > arr.length) arr else n == nil ? arr[arr.length - 1] : arr.slice(arr.length - n, arr.length) end end |
.map(collection, some_method) ⇒ Object
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
# File 'lib/seda.rb', line 437 def self.map(collection, some_method) arr = [] if collection.class == Array collection.each_with_index do |element, idx| if some_method.class == String arr << method(some_method.to_sym).call(element, idx, collection) elsif some_method.class == Symbol arr << method(some_method).call(element, idx, collection) elsif some_method.class == Method arr << some_method.call(element, idx, collection) else raise ArgumentError.new('The second argument must be a method. You could pass in the actual method, a string of the method name, or a symbol of the method name. Both arguments are needed.') end end elsif collection.class == Hash for k in collection if some_method.class == String arr << method(some_method.to_sym).call(k[1], k[0], collection) elsif some_method.class == Symbol arr << method(some_method).call(k[1], k[0], collection) elsif some_method.class == Method arr << some_method.call(k[1], k[0], collection) else raise ArgumentError.new('The second argument must be a method. You could pass in the actual method, a string of the method name, or a symbol of the method name. Both arguments are needed.') end end else raise ArgumentError.new('Must provide an Array, or a Hash as the first Argument.') end arr end |
.memoize(some_method, *some_method_args) ⇒ Object
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 |
# File 'lib/seda.rb', line 472 def self.memoize(some_method, *some_method_args) storage = {} result = '' b = eval(local_variables[1].to_s) if some_method.class == String some_method = method(some_method.to_sym) elsif some_method.class == Symbol some_method = method(some_method) end result = lambda{|a = b, arg = b.to_s| if !storage[arg] storage[arg] = some_method.call(*a) end return storage[arg] } return result.call(b) end |
.pluck(collection, key) ⇒ Object
132 133 134 |
# File 'lib/seda.rb', line 132 def self.pluck(collection, key) collection.map{|item| item[key]} end |
.reduce(collection, some_method) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/seda.rb', line 245 def self.reduce(collection, some_method) accumulator = '' cloned_collection = Marshal.load(Marshal.dump(collection)) counter = 0 optional_result_object = cloned_collection.class.new() if cloned_collection.class == Array accumulator = cloned_collection[0] optional_result_object << accumulator cloned_collection.each_with_index do |element, idx| if counter == 0 counter = 1 next end if some_method.class == String args = method(some_method).parameters.map { |arg| arg[1].to_s } if args.length < 3 accumulator = method(some_method.to_sym).call(accumulator, element) optional_result_object << accumulator else accumulator = method(some_method.to_sym).call(accumulator, element, idx) optional_result_object << accumulator end elsif some_method.class == Symbol args = method(some_method).parameters.map { |arg| arg[1].to_s } if args.length < 3 accumulator = method(some_method).call(accumulator, element) optional_result_object << accumulator else accumulator = method(some_method).call(accumulator, element, idx) optional_result_object << accumulator end elsif some_method.class == Method args = some_method.parameters.map { |arg| arg[1].to_s } if args.length < 3 accumulator = some_method.call(accumulator, element) optional_result_object << accumulator else accumulator = some_method.call(accumulator, element, idx) optional_result_object << accumulator end end end elsif cloned_collection.class == Hash accumulator = cloned_collection.values[0] for k in cloned_collection if counter == 0 optional_result_object[k[0]] = accumulator counter = 1 next else if some_method.class == String args = some_method.parameters.map { |arg| arg[1].to_s } if args.length < 3 accumulator = method(some_method.to_sym).call(accumulator, k[1]) optional_result_object[k[0]] = accumulator else accumulator = method(some_method.to_sym).call(accumulator, k[1], counter) optional_result_object[k[0]] = accumulator end elsif some_method.class == Symbol args = some_method.parameters.map { |arg| arg[1].to_s } if args.length < 3 accumulator = method(some_method).call(accumulator, k[1]) optional_result_object[k[0]] = accumulator else accumulator = method(some_method).call(accumulator, k[1], counter) optional_result_object[k[0]] = accumulator end elsif some_method.class == Method args = some_method.parameters.map { |arg| arg[1].to_s } if args.length < 3 accumulator = some_method.call(accumulator, k[1]) optional_result_object[k[0]] = accumulator else accumulator = some_method.call(accumulator, k[1], counter) optional_result_object[k[0]] = accumulator end end end counter = counter + 1 end else return cloned_collection end if accumulator.class == Float || accumulator.class == Integer accumulator else cloned_collection end end |
.reject(collection, some_method) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/seda.rb', line 196 def self.reject(collection, some_method) arr = [] result_obj = {} if collection.class == Array collection.each_with_index do |element, idx| if some_method.class == String if !method(some_method.to_sym).call(collection, idx) arr << element end elsif some_method.class == Symbol if !method(some_method).call(collection, idx) arr << element end elsif some_method.class == Method if !some_method.call(collection, idx) arr << element end else raise ArgumentError.new('The second argument must be a method. You could pass in the actual method, a string of the method name, or a symbol of the method name. Both arguments are needed.') end end return arr elsif collection.class == Hash for k in collection if some_method.class == String if !method(some_method.to_sym).call(k[1]) result_obj[k[0]] = k[1] end elsif some_method.class == Symbol if !method(some_method).call(k[1]) result_obj[k[0]] = k[1] end elsif some_method.class == Method if !some_method.call(k[1]) result_obj[k[0]] = k[1] end else raise ArgumentError.new('The second argument must be a method. You could pass in the actual method, a string of the method name, or a symbol of the method name. Both arguments are needed.') end end return result_obj else raise ArgumentError.new('Must provide an Array, or a Hash as the first Argument.') end end |
.shuffle(arr) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/seda.rb', line 136 def self.shuffle(arr) counter = arr.length - 1 while counter > 0 random_index = rand(counter) arr[counter], arr[random_index] = arr[random_index], arr[counter] counter -= 1 end arr end |
.some(collection, some_method) ⇒ Object
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/seda.rb', line 495 def self.some(collection, some_method) result = true collection.each do |item| if some_method.class == Method result = some_method.call(item) elsif some_method.class == Symbol result = method(some_method).call(item) elsif some_method.class == String result = method(some_method.to_sym).call(item) else raise ArgumentError.new('First argument must reference a method') end end return result end |
.sort_by_max(collection) ⇒ Object
513 514 515 516 517 518 519 520 521 522 523 524 525 |
# File 'lib/seda.rb', line 513 def self.sort_by_max(collection) if collection.class == Hash result = {} arr = collection.sort{|a, b| b <=> a } arr.flatten.uniq.inject({}) do |memo, item| result[item] = item end return result elsif collection.class == Array collection.sort!{|a, b| b <=> a } end end |
.sort_by_min(collection) ⇒ Object
527 528 529 530 531 532 533 534 535 536 537 538 539 |
# File 'lib/seda.rb', line 527 def self.sort_by_min(collection) if collection.class == Hash result = {} arr = collection.sort{|a, b| a <=> b } arr.flatten.uniq.inject({}) do |memo, item| result[item] = item end return result elsif collection.class == Array collection.sort!{|a, b| a <=> b } end end |