Class: Integer

### Home Classes Methods

### In Files

- lib/ferret/number_tools.rb

### Parent

Object

### Methods

- #to_s_lex
- #to_s_pad

### Files

- MIT-LICENSE
- README
- TODO
- TUTORIAL

### Class/Module Index

Quicksearch

- Ferret
- Ferret::BlankSlate
- Ferret::BoostMixin
- Ferret::Browser
- Ferret::Browser::Controller
- Ferret::Browser::Delegator
- Ferret::Browser::DocumentController
- Ferret::Browser::ErrorController
- Ferret::Browser::HelpController
- Ferret::Browser::HomeController
- Ferret::Browser::TermController
- Ferret::Browser::TermVectorController
- Ferret::Browser::ViewHelper
- Ferret::Document
- Ferret::Field
- Ferret::FieldSymbol
- Ferret::FieldSymbolMethods
- Ferret::FileNotFoundError
- Ferret::Index
- Ferret::Index::FieldInfo
- Ferret::Index::FieldInfos
- Ferret::Index::Index
- Ferret::Index::IndexReader
- Ferret::Index::IndexWriter
- Ferret::Index::LazyDoc
- Ferret::Index::LazyDoc::LazyDocData
- Ferret::Index::TermDocEnum
- Ferret::Index::TermEnum
- Ferret::Index::TermVector
- Ferret::Index::TermVector::TVOffsets
- Ferret::Index::TermVector::TVTerm
- Ferret::ParseError
- Ferret::QueryParser
- Ferret::QueryParser::QueryParseException
- Ferret::Search
- Ferret::Search::BooleanQuery
- Ferret::Search::BooleanQuery::BooleanClause
- Ferret::Search::ConstantScoreQuery
- Ferret::Search::Explanation
- Ferret::Search::Filter
- Ferret::Search::FilteredQuery
- Ferret::Search::FuzzyQuery
- Ferret::Search::Hit
- Ferret::Search::MatchAllQuery
- Ferret::Search::MultiSearcher
- Ferret::Search::MultiTermQuery
- Ferret::Search::PhraseQuery
- Ferret::Search::PrefixQuery
- Ferret::Search::Query
- Ferret::Search::QueryFilter
- Ferret::Search::RangeFilter
- Ferret::Search::RangeQuery
- Ferret::Search::Searcher
- Ferret::Search::Sort
- Ferret::Search::SortField
- Ferret::Search::Spans
- Ferret::Search::Spans::SpanFirstQuery
- Ferret::Search::Spans::SpanMultiTermQuery
- Ferret::Search::Spans::SpanNearQuery
- Ferret::Search::Spans::SpanNotQuery
- Ferret::Search::Spans::SpanOrQuery
- Ferret::Search::Spans::SpanPrefixQuery
- Ferret::Search::Spans::SpanTermQuery
- Ferret::Search::TermQuery
- Ferret::Search::TopDocs
- Ferret::Search::TypedRangeFilter
- Ferret::Search::TypedRangeQuery
- Ferret::Search::WildcardQuery
- Ferret::StateError
- Ferret::Store
- Ferret::Store::Directory
- Ferret::Store::FSDirectory
- Ferret::Store::Lock
- Ferret::Store::Lock::LockError
- Ferret::Store::RAMDirectory
- WEBrick
- WEBrick::FerretBrowserHandler
- Date
- DateTime
- FileUtils
- Float
- Integer
- String
- Symbol
- Time

No matching classes.

# Integer

Provides support for converting integers to Strings, and back again. The strings are structured so that lexicographic sorting order is preserved.

That is, if integer1 is less than integer2 for any two integers integer1 and integer2, then integer1.to_s_lex is lexicographically less than integer2.to_s_lex. (Similarly for "greater than" and "equals".)

This class handles numbers between - 10 ** 10,000 and 10 ** 10,000 which should cover all practical numbers. If you need bigger numbers, increase Integer::LEN_STR_SIZE.

### Constants

LEN_STR_SIZE

  LEN_SIZE of 4 should handle most numbers that can practically be held in memory.

LEN_STR_TEMPLATE
NEG_LEN_MASK

### Public Instance Methods

to_s_lex() click to toggle source

Convert the number to a lexicographically sortable string. This string will use printable characters only but will not be human readable.

    # File lib/ferret/number_tools.rb, line 35
    def to_s_lex
      if (self >= 0)
        num_str = self.to_s
        len_str = LEN_STR_TEMPLATE % num_str.size
        return len_str + num_str
      else
        num = self * -1
        num_str = num.to_s
        num_len = num_str.size
        len_str = LEN_STR_TEMPLATE % (NEG_LEN_MASK - num_len)
        num = (10 ** num_str.size) - num
        return "-#{len_str}%0#{num_len}d" % num
      end
    end

to_s_pad(width = 10) click to toggle source

Convert the number to a lexicographically sortable string by padding with 0s. You should make sure that you set the width to a number large enough to accommodate all possible values. Also note that this method will not work with negative numbers. That is negative numbers will sort in the opposite direction as positive numbers. If you have very large numbers or a mix of positive and negative numbers you should use the Integer#to_s_lex method

width | number of characters in the string returned. Default is 10. So 123.to_s_pad(5) => 00123 and -123.to_s_pad(5) => -0123
return | padding string representation of the number.

    # File lib/ferret/number_tools.rb, line 60
    def to_s_pad(width = 10)
      "%#{width}d" % self
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.