module SuperDiff::Helpers

Public Instance Methods

jruby?() click to toggle source
# File lib/super_diff/helpers.rb, line 34
def jruby?
  defined?(JRUBY_VERSION)
end
object_address_for(object) click to toggle source
# File lib/super_diff/helpers.rb, line 45
def object_address_for(object)
  # Source: <https://github.com/jruby/jruby/blob/master/core/src/main/java/org/jruby/RubyBasicObject.java>
  "0x%x" % object.hash
end
plural_type_for(value) click to toggle source
# File lib/super_diff/helpers.rb, line 25
def plural_type_for(value)
  case value
  when Numeric then "numbers"
  when String then "strings"
  when Symbol then "symbols"
  else "objects"
  end
end
ruby_version_matches?(version_string) click to toggle source
# File lib/super_diff/helpers.rb, line 38
def ruby_version_matches?(version_string)
  Gem::Requirement.new(version_string).satisfied_by?(
    Gem::Version.new(RUBY_VERSION),
  )
end
style(*args, color_enabled: true, **opts, &block) click to toggle source

TODO: Simplify this

# File lib/super_diff/helpers.rb, line 6
def style(*args, color_enabled: true, **opts, &block)
  klass =
    if color_enabled && Csi.color_enabled?
      Csi::ColorizedDocument
    else
      Csi::UncolorizedDocument
    end

  document = klass.new.extend(ColorizedDocumentExtensions)

  if block
    document.__send__(:evaluate_block, &block)
  else
    document.colorize(*args, **opts)
  end

  document
end
with_slice_of_array_replaced(array, range, replacement) click to toggle source
# File lib/super_diff/helpers.rb, line 64
def with_slice_of_array_replaced(array, range, replacement)
  beginning =
    if range.begin > 0
      array[Range.new(0, range.begin - 1)]
    else
      []
    end

  ending =
    if range.end <= array.length - 1
      array[Range.new(range.end + 1, array.length - 1)]
    else
      []
    end

  beginning + [replacement] + ending
end