Module: Enumerable
- Included in:
- Redwood::Thread
- Defined in:
- lib/sup/util.rb
Instance Method Summary collapse
-
#argfind ⇒ Object
like find, except returns the value of the block rather than the element itself.
- #argmin ⇒ Object
-
#between(startline, endline) ⇒ Object
returns all the entries which are equal to startline up to endline.
- #map_to_hash ⇒ Object
- #map_with_index ⇒ Object
- #max_of ⇒ Object
-
#shared_prefix(caseless = false, exclude = "") ⇒ Object
returns the maximum shared prefix of an array of strings optinally excluding a prefix.
- #sum ⇒ Object
Instance Method Details
#argfind ⇒ Object
like find, except returns the value of the block rather than the element itself.
561 562 563 564 565 |
# File 'lib/sup/util.rb', line 561 def argfind ret = nil find { |e| ret ||= yield(e) } ret || nil # force end |
#argmin ⇒ Object
567 568 569 570 571 572 573 574 575 576 |
# File 'lib/sup/util.rb', line 567 def argmin best, bestval = nil, nil each do |e| val = yield e if bestval.nil? || val < bestval best, bestval = e, val end end best end |
#between(startline, endline) ⇒ Object
returns all the entries which are equal to startline up to endline
597 598 599 |
# File 'lib/sup/util.rb', line 597 def between startline, endline select { |l| true if l == startline .. l == endline } end |
#map_to_hash ⇒ Object
553 554 555 556 557 |
# File 'lib/sup/util.rb', line 553 def map_to_hash ret = {} each { |x| ret[x] = yield(x) } ret end |
#map_with_index ⇒ Object
545 546 547 548 549 |
# File 'lib/sup/util.rb', line 545 def map_with_index ret = [] each_with_index { |x, i| ret << yield(x, i) } ret end |
#max_of ⇒ Object
592 593 594 |
# File 'lib/sup/util.rb', line 592 def max_of map { |e| yield e }.max end |
#shared_prefix(caseless = false, exclude = "") ⇒ Object
returns the maximum shared prefix of an array of strings optinally excluding a prefix
580 581 582 583 584 585 586 587 588 589 590 |
# File 'lib/sup/util.rb', line 580 def shared_prefix caseless=false, exclude="" return "" if empty? prefix = "" (0 ... first.length).each do |i| c = (caseless ? first.downcase : first)[i] break unless all? { |s| (caseless ? s.downcase : s)[i] == c } next if exclude[i] == c prefix += first[i].chr end prefix end |
#sum ⇒ Object
551 |
# File 'lib/sup/util.rb', line 551 def sum; inject(0) { |x, y| x + y }; end |