In Files

Class/Module Index [+]

Quicksearch

Enumerable

Public Instance Methods

argfind() click to toggle source

like find, except returns the value of the block rather than the element itself.

# File lib/sup/util.rb, line 417
def argfind
  ret = nil
  find { |e| ret ||= yield(e) }
  ret || nil # force
end
argmin() click to toggle source
# File lib/sup/util.rb, line 423
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
map_to_hash() click to toggle source
# File lib/sup/util.rb, line 409
def map_to_hash
  ret = {}
  each { |x| ret[x] = yield(x) }
  ret
end
map_with_index() click to toggle source
# File lib/sup/util.rb, line 401
def map_with_index
  ret = []
  each_with_index { |x, i| ret << yield(x, i) }
  ret
end
max_of() click to toggle source
# File lib/sup/util.rb, line 448
def max_of
  map { |e| yield e }.max
end
shared_prefix(caseless=false, exclude="") click to toggle source

returns the maximum shared prefix of an array of strings optinally excluding a prefix

# File lib/sup/util.rb, line 436
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() click to toggle source
# File lib/sup/util.rb, line 407
def sum; inject(0) { |x, y| x + y }; end

[Validate]

Generated with the Darkfish Rdoc Generator 2.