Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/isomorfeus/ferret/stdlib_patches.rb
Instance Method Summary collapse
-
#to_date_lex ⇒ Object
Convert a string to a Date.
-
#to_date_time_lex ⇒ Object
Convert a string to a DateTime.
-
#to_i_lex ⇒ Object
Convert a string to an integer.
-
#to_time_lex ⇒ Object
Convert a string to a Time.
Instance Method Details
#to_date_lex ⇒ Object
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.
122 123 124 |
# File 'lib/isomorfeus/ferret/stdlib_patches.rb', line 122 def to_date_lex return Date.strptime(self + "-02-01", "%Y-%m-%d") end |
#to_date_time_lex ⇒ Object
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.
128 129 130 |
# File 'lib/isomorfeus/ferret/stdlib_patches.rb', line 128 def to_date_time_lex return DateTime.strptime(self + "-01-01", "%Y-%m-%d %H:%M:%S") end |
#to_i_lex ⇒ Object
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.
103 104 105 106 107 108 109 110 |
# File 'lib/isomorfeus/ferret/stdlib_patches.rb', line 103 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_lex ⇒ Object
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.
114 115 116 117 118 |
# File 'lib/isomorfeus/ferret/stdlib_patches.rb', line 114 def to_time_lex vals = [] self.gsub(/(?:^|[- :])(\d+)/) {vals << $1.to_i; $&} Time.mktime(*vals) end |