module Ellipses::Support

Public Instance Methods

deflate_path(path, rootdir = nil) click to toggle source
# File lib/ellipses/support/deflate_path.rb, line 9
def deflate_path(path, rootdir = nil)
  base = Pathname.new(rootdir || '.').expand_path
  Pathname.new(path).cleanpath.expand_path.relative_path_from(base).to_s
end
digest(*args) click to toggle source
# File lib/ellipses/support/digest.rb, line 9
def digest(*args)
  ::Digest::SHA256.hexdigest args.map(&:to_s).join
end
entropy(string) click to toggle source

rosettacode.org/wiki/Entropy#Ruby

# File lib/ellipses/support/entropy.rb, line 8
def entropy(string)
  string
    .each_char
    .group_by(&:to_s)
    .values
    .map { |x| x.length / string.length.to_f }
    .reduce(0) { |e, x| e - x * Math.log2(x) }
end
expand_path(path, rootdir = nil) click to toggle source
# File lib/ellipses/support/expand_path.rb, line 9
def expand_path(path, rootdir = nil)
  Pathname.new(::File.join(rootdir || '.', path)).cleanpath.to_s
end
intersperse_arrays(arrays, intersperse) click to toggle source
# File lib/ellipses/support/intersperse_arrays.rb, line 7
def intersperse_arrays(arrays, intersperse)
  [].tap do |interspersed|
    arrays.each { |array| interspersed.append(array, intersperse) }
    interspersed.pop
  end
end
prefixize_non_blank(string, prefix, excludes: nil) click to toggle source
# File lib/ellipses/support/prefixize_non_blank.rb, line 7
def prefixize_non_blank(string, prefix, excludes: nil)
  stripped = string.strip

  return string if prefix.empty? || stripped.empty?
  return string if excludes && [*excludes].include?(stripped)

  "#{prefix}#{string}"
end
search_path(patterns, starting_dir = '.') click to toggle source
# File lib/ellipses/support/search_path.rb, line 9
def search_path(patterns, starting_dir = '.')
  Pathname.new(starting_dir).expand_path.ascend.each do |dir|
    [*patterns].each do |pattern|
      next unless ::File.exist?(file = ::File.join(dir, pattern))

      return [dir.to_s, file]
    end
  end
end
to_range(index_or_range) click to toggle source
# File lib/ellipses/support/to_range.rb, line 7
def to_range(index_or_range)
  case index_or_range
  when Range then index_or_range
  when Integer then Range.new(index_or_range, index_or_range)
  else raise ArgumentError, "Integer or a Range expected where found: #{index_or_range.class}"
  end
end
updatelines(file, lines) { |status| ... } click to toggle source
# File lib/ellipses/support/updatelines.rb, line 7
def updatelines(file, lines)
  old = ::File.exist?(file) ? ::File.read(file) : nil
  new = "#{[*lines].join.chomp}\n"

  if old && old == new
    false
  else
    ::File.write(file, new)
    true
  end.tap { |status| yield(status) if block_given? }
end
writelines(file, lines) click to toggle source
# File lib/ellipses/support/writelines.rb, line 7
def writelines(file, lines)
  ::File.write(file, "#{[*lines].join.chomp}\n")
end