module ActiveSupport::CoreExtensions::String::Access
def at(position)
"hello".at(4) # => "o"
"hello".at(0) # => "h"
Examples:
Returns the character at the +position+ treating the string as an array (where 0 is the first character).
def at(position) mb_chars[position, 1].to_s end
def at(position)
def at(position) self[position] end
def first(limit = 1)
"hello".first(2) # => "he"
"hello".first # => "h"
Examples:
Returns the first character of the string or the first +limit+ characters.
def first(limit = 1) if limit == 0 '' elsif limit >= size self else mb_chars[0...limit].to_s end end
def first(limit = 1)
def first(limit = 1) if limit == 0 '' elsif limit >= size self else to(limit - 1) end end
def from(position)
"hello".from(2) # => "llo"
"hello".from(0) # => "hello"
Examples:
Returns the remaining of the string from the +position+ treating the string as an array (where 0 is the first character).
def from(position) mb_chars[position..-1].to_s end
def from(position)
def from(position) self[position..-1] end
def last(limit = 1)
"hello".last(2) # => "lo"
"hello".last # => "o"
Examples:
Returns the last character of the string or the last +limit+ characters.
def last(limit = 1) if limit == 0 '' elsif limit >= size self else mb_chars[(-limit)..-1].to_s end end
def last(limit = 1)
def last(limit = 1) if limit == 0 '' elsif limit >= size self else from(-limit) end end
def to(position)
"hello".to(2) # => "hel"
"hello".to(0) # => "h"
Examples:
Returns the beginning of the string up to the +position+ treating the string as an array (where 0 is the first character).
def to(position) mb_chars[0..position].to_s end
def to(position)
def to(position) self[0..position] end