1 module ActiveSupport
#:nodoc:
2 module CoreExtensions
#:nodoc:
4 unless '1.9'.respond_to
?(:force_encoding)
5 # Makes it easier to access parts of a string, such as specific characters and substrings.
7 # Returns the character at the +position+ treating the string as an array (where 0 is the first character).
10 # "hello".at(0) # => "h"
11 # "hello".at(4) # => "o"
12 # "hello".at(10) # => nil
14 mb_chars
[position
, 1].to_s
17 # Returns the remaining of the string from the +position+ treating the string as an array (where 0 is the first character).
20 # "hello".from(0) # => "hello"
21 # "hello".from(2) # => "llo"
22 # "hello".from(10) # => nil
24 mb_chars
[position
..-1].to_s
27 # Returns the beginning of the string up to the +position+ treating the string as an array (where 0 is the first character).
30 # "hello".to(0) # => "h"
31 # "hello".to(2) # => "hel"
32 # "hello".to(10) # => "hello"
34 mb_chars
[0..position
].to_s
37 # Returns the first character of the string or the first +limit+ characters.
40 # "hello".first # => "h"
41 # "hello".first(2) # => "he"
42 # "hello".first(10) # => "hello"
44 mb_chars
[0..(limit
- 1)].to_s
47 # Returns the last character of the string or the last +limit+ characters.
50 # "hello".last # => "o"
51 # "hello".last(2) # => "lo"
52 # "hello".last(10) # => "hello"
54 (mb_chars
[(-limit
)..-1] || self).to_s
58 module Access
#:nodoc: