Method: String#to

Defined in:
lib/jun/active_support/core_ext/string/access.rb

#to(position) ⇒ Object

Returns a substring from the beginning of the string to the given position (index). If the position is negative, the ending point is counted from the end of the string.

string = "smoki"
string.to(0)  # => "s"
string.to(3)  # => "smok"
string.to(-2) # => "smok"


36
37
38
39
40
41
# File 'lib/jun/active_support/core_ext/string/access.rb', line 36

def to(position)
  position += size if position.negative?
  position = -1 if position.negative?

  self[0, position + 1]
end