Sha256: 00d3d08ed5db67e1a0e44aee10e25e84ccddf699af941623eba3a075af3d142f

Contents?: true

Size: 1.99 KB

Versions: 10

Compression:

Stored size: 1.99 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

  # return a list of the subclasses of a class
  # from Matt Williams
  # http://matthewkwilliams.com/index.php/2008/08/22/rubys-objectspace-subclasses/
  def self.subclasses(direct = false)
    classes = []
    if direct
      ObjectSpace.each_object(Class) do |c|
        next unless c.superclass == self
        classes << c
      end
    else
      ObjectSpace.each_object(Class) do |c|
        next unless c.ancestors.include?(self) and (c != self)
        classes << c
      end
    end
    classes
  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

10 entries across 10 versions & 1 rubygems

Version Path
clevic-0.14.6 lib/clevic/extensions.rb
clevic-0.14.5 lib/clevic/extensions.rb
clevic-0.14.4 lib/clevic/extensions.rb
clevic-0.14.3 lib/clevic/extensions.rb
clevic-0.14.2 lib/clevic/extensions.rb
clevic-0.14.1 lib/clevic/extensions.rb
clevic-0.14.0 lib/clevic/extensions.rb
clevic-0.13.0.b12 lib/clevic/extensions.rb
clevic-0.13.0.b11 lib/clevic/extensions.rb
clevic-0.13.0.b10 lib/clevic/extensions.rb