module Ki::KiEnumerable

Public Instance Methods

any_matches?(value) click to toggle source
# File lib/util/ruby_extensions.rb, line 36
def any_matches?(value)
  each do |pattern|
    if value.match(pattern)
      return pattern
    end
  end
  false
end
find_first(count=1, &block) click to toggle source
# File lib/util/ruby_extensions.rb, line 45
def find_first(count=1, &block)
  ret = []
  each do |item|
    if block.nil? || block.call(item)
      ret << item
      if ret.size == count
        break
      end
    end
  end
  if count==1
    ret.at(0)
  else
    ret
  end
end
size!(*args) click to toggle source
# File lib/util/ruby_extensions.rb, line 19
def size!(*args)
  args.each do |valid_size|
    if valid_size.kind_of?(Range)
      if valid_size.include?(size)
        return self
      end
    elsif valid_size.respond_to?(:to_i)
      if Integer(valid_size) == size
        return self
      end
    else
      raise "'#{valid_size.inspect}' not supported, needs to be either Range or have .to_i method"
    end
  end
  raise "size #{size} does not match '#{args.map { |s| s.to_s }.join("', '")}'"
end
to_h(separator=nil, &block) click to toggle source
# File lib/util/ruby_extensions.rb, line 62
def to_h(separator=nil, &block)
  ret = {}
  each do |item|
    if separator
      key, *values = item.split(separator)
      if values.size > 0 || item.include?(separator)
        ret[key]=values.join(separator)
      else
        ret[key]=true
      end
    elsif block
      key, value = block.call(item)
      ret[key]=value
    end
  end
  ret
end