Module: Tins::Minimize

### Home Classes Methods

### In Files

- lib/tins/minimize.rb

### Methods

- #minimize
- #minimize!
- #unminimize
- #unminimize!

### Class/Module Index

Quicksearch

- Tins
- Tins
- Tins
- Tins::Annotate
- Tins::AskAndSend
- Tins::Attempt
- Tins::Bijection
- Tins::Blank
- Tins::Blank::Array
- Tins::Blank::FalseClass
- Tins::Blank::Hash
- Tins::Blank::NilClass
- Tins::Blank::Numeric
- Tins::Blank::Object
- Tins::Blank::String
- Tins::Blank::TrueClass
- Tins::BlankSlate
- Tins::BlockSelf
- Tins::CasePredicate
- Tins::ClassMethod
- Tins::Concern
- Tins::Concern::ModuleMixin
- Tins::Constant
- Tins::ConstantMaker
- Tins::CountBy
- Tins::DSLAccessor
- Tins::DateDummy
- Tins::DateTimeDummy
- Tins::DeepConstGet
- Tins::DeepDup
- Tins::Deflect
- Tins::Deflect::DeflectError
- Tins::Deflect::Deflector
- Tins::Deflect::DeflectorCollection
- Tins::Delegate
- Tins::DynamicScope
- Tins::DynamicScope::Context
- Tins::Eigenclass
- Tins::ExtractLastArgumentOptions
- Tins::FileBinary
- Tins::FileBinary::ClassMethods
- Tins::FileBinary::Constants
- Tins::Find
- Tins::Find::Finder
- Tins::Find::Finder::PathExtension
- Tins::FromModule
- Tins::Full
- Tins::GO
- Tins::GO::EnumerableExtension
- Tins::Generator
- Tins::HashSymbolizeKeysRecursive
- Tins::HashUnion
- Tins::IfPredicate
- Tins::InstanceExec
- Tins::Interpreter
- Tins::Limited
- Tins::LinesFile
- Tins::LinesFile::LineExtension
- Tins::Memoize
- Tins::Memoize::CacheMethods
- Tins::MethodDescription
- Tins::MethodMissingDelegator
- Tins::MethodMissingDelegator::DelegatorClass
- Tins::MethodMissingDelegator::DelegatorModule
- Tins::Minimize
- Tins::ModuleGroup
- Tins::NamedSet
- Tins::Null
- Tins::Null::Kernel
- Tins::NullClass
- Tins::NullPlus
- Tins::Once
- Tins::P
- Tins::ParameterizedModule
- Tins::PartialApplication
- Tins::ProcCompose
- Tins::ProcPrelude
- Tins::RangePlus
- Tins::RequireMaybe
- Tins::Responding
- Tins::Rotate
- Tins::Scope
- Tins::SecureWrite
- Tins::SexySingleton
- Tins::SexySingleton::SingletonClassMethods
- Tins::Shuffle
- Tins::StringByteOrderMark
- Tins::StringCamelize
- Tins::StringUnderscore
- Tins::StringVersion
- Tins::StringVersion::Version
- Tins::Subhash
- Tins::SymbolMaker
- Tins::Terminal
- Tins::ThreadGlobal
- Tins::ThreadLocal
- Tins::TimeDummy
- Tins::TimeFreezer
- Tins::To
- Tins::ToProc
- Tins::Token
- Tins::UniqBy
- Tins::Write
- Object
- Object::DSLKit
- Object::Spruz
- Array
- Date
- DateTime
- Enumerable
- File
- Hash
- IO
- IRB
- Method
- Module
- Proc
- Range
- String
- Symbol
- Time
- UnboundMethod

No matching classes.

# Tins::Minimize

This module can be mixed into all classes, whose instances respond to the

  and size-methods, like for example Array. The returned elements from []

should respond to the succ method.

### Public Instance Methods

minimize() click to toggle source

Returns a minimized version of this object, that is successive elements are substituted with ranges a..b. In the situation ..., x, y,... and y != x.succ a range x..x is created, to make it easier to iterate over all the ranges in one run. A small example:

    [ 'A', 'B', 'C', 'G', 'K', 'L', 'M' ].minimize # => [ 'A'..'C', 'G'..'G', 'K'..'M' ]

If the order of the original elements doesn't matter, it's a good idea to first sort them and then minimize:

    [ 5, 1, 4, 2 ].sort.minimize # => [ 1..2, 4..5 ]

    # File lib/tins/minimize.rb, line 15
    def minimize
      result = []
      last_index = size - 1
      size.times do |i|
        result << [ self[0] ] if i == 0
        if self[i].succ != self[i + 1] or i == last_index
          result[-1] << self[i]
          result << [ self[i + 1] ] unless i == last_index
        end
      end
      result.map! { |a, b| a..b }
    end

minimize!() click to toggle source

First minimizes this object, then calls the replace method with the result.

    # File lib/tins/minimize.rb, line 30
    def minimize!
      replace minimize
    end

unminimize() click to toggle source

Invert a minimized version of an object. Some small examples:

    [ 'A'..'C', 'G'..'G', 'K'..'M' ].unminimize # => [ 'A', 'B', 'C', 'G', 'K', 'L', 'M' ]

and

    [ 1..2, 4..5 ].unminimize # => [ 1, 2, 4, 5 ]

    # File lib/tins/minimize.rb, line 38
    def unminimize
      result = []
      for range in self
        for e in range
          result << e
        end
      end
      result
    end

unminimize!() click to toggle source

Invert a minimized version of this object in place.

    # File lib/tins/minimize.rb, line 49
    def unminimize!
      replace unminimize
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.