Sha256: 1e2b6471b1bfbe300990144b67be2fb148c1623aa3b9de574bb734b2c684b25a

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

# extensions specific to clevic

class Object
  # recursively calls each entry in path_ary
  # will return nil if any entry in path_ary
  # results in a nil value.
  def evaluate_path( path_ary )
    path_ary.inject( self ) do |value, att|
      value.nil? ? nil : value.send( att )
    end
  end
  
  # pass self to the block and return the results of the block.
  def with( &block )
    yield( self )
  end
end

class String
  # just grab the character code of the last character in the string
  # TODO this won't work in unicode or utf-8
  def to_char
    if RUBY_VERSION <= '1.8.6'
      self[0]
    else
      bytes.first
    end
  end
end

class Array
  def sparse_hash
    Hash[ *(first..last).map do |index|
      [index, include?( index ) ]
    end.flatten ]
  end

  def sparse
    (first..last).map do |index|
      index if include?( index )
    end
  end
  
  def section
    return [] if empty?
    rv = [first]
    self[1..-1].each_with_index do |next_value, index|
      break if rv.last.succ != next_value
      rv << next_value
    end
    rv
  end
  
  # group by ascending values
  def group
    parts = []
    next_section = section
    if next_section.empty?
      parts
    else
      parts << section
      parts + self[section.size..-1].group
    end
  end
  
  def range
    first..last
  end
end

def Range
  def distance
    last - first
  end
end

begin
  Date.new.freeze.to_s
rescue TypeError
  # Workaround for the date freeze issue, if it exists.
  class Date
    def freeze
      self
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
clevic-0.13.0.b6 lib/clevic/extensions.rb