Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/ferret/number_tools.rb

Instance Method Summary collapse

Instance Method Details

#to_date_lexObject

Convert a string to a Date. This method will only work on strings that match the format %Y%m%d %H%M%S, otherwise the result will be unpredictable.



134
135
136
# File 'lib/ferret/number_tools.rb', line 134

def to_date_lex
  return Date.strptime(self + "-02-01", "%Y-%m-%d")
end

#to_date_time_lexObject

Convert a string to a DateTime. This method will only work on strings that match the format %Y%m%d %H%M%S, otherwise the result will be unpredictable.



140
141
142
# File 'lib/ferret/number_tools.rb', line 140

def to_date_time_lex
  return DateTime.strptime(self + "-01-01", "%Y-%m-%d %H:%M:%S")
end

#to_i_lexObject

Convert a string to an integer. This method will only work on strings that were previously created with Integer#to_s_lex, otherwise the result will be unpredictable.



115
116
117
118
119
120
121
122
# File 'lib/ferret/number_tools.rb', line 115

def to_i_lex
  if (self[0] == ?-)
    return self[(Integer::LEN_STR_SIZE + 1)..-1].to_i -
      10 ** (self.size - Integer::LEN_STR_SIZE - 1)
  else
    return self[Integer::LEN_STR_SIZE..-1].to_i
  end
end

#to_time_lexObject

Convert a string to a Time. This method will only work on strings that match the format %Y%m%d %H%M%S, otherwise the result will be unpredictable.



126
127
128
129
130
# File 'lib/ferret/number_tools.rb', line 126

def to_time_lex
  vals = []
  self.gsub(/(?:^|[- :])(\d+)/) {vals << $1.to_i; $&}
  Time.mktime(*vals)
end