Class: RubyJS.String

Defined in: lib/corelib/string.coffee
Inherits: RubyJS.Object

Class Method Summary

Instance Method Summary

Inherited Method Summary

Methods inherited from RubyJS.Object

.include, .__add_default_aliases__, #send, #respond_to, #to_enum, #tap

Class Method Details

+ (void) fromCharCode(obj)

+ (void) new(str = '')

TODO: make .new subclasseable
TODO: follow

+ (void) isString(obj)

+ (void) try_convert(obj)

+ (void) string_native(obj)

Constructor Details

- (void) constructor(primitive = "")

Instance Method Details

- (void) is_string()

- (void) to_native()

- (void) valueOf()

- (void) toString()

- (void) unbox()

- (void) initialize_copy()

- (void) clone()

- (void) '%'(num)

- (void) '*'(num)

- (void) '+'(other)

'-instance'> - (void) '<=>'(other)

- (void) '=='(other)

- (void) '<<'(other)

- (void) '=~'(args...)

- (void) capitalize()

Note: Differs from JS toUpperCase in that it does not uppercase special characters ä, ö, etc

Returns a copy of str with the first character converted to uppercase and the remainder to lowercase. Note: case conversion is effective only in ASCII region.

Examples:

  R("hello").capitalize()    # => "Hello"
  R("HELLO").capitalize()    # => "Hello"
  R("äöü").capitalize()      # => "äöü"
  R("123ABC").capitalize()   # => "123abc"

- (void) capitalize_bang()

Modifies str by converting the first character to uppercase and the remainder to lowercase. Returns nil if no changes are made. Note: case conversion is effective only in ASCII region.

Examples:

 str = R('hello')
 str.capitalize_bang()         # => "Hello"
 str                           # => "Hello"

Already capitalized

 str = R("Already capitals")
 str.capitalize_bang()         # => null
 str                           # => "Already capitals"

- (void) casecmp(other)

Case-insensitive version of String#<=>.

Examples:

  R("abcdef").casecmp("abcde")     #=> 1
  R("aBcDeF").casecmp("abcdef")    #=> 0
  R("abcdef").casecmp("abcdefg")   #=> -1
  R("abcdef").casecmp("ABCDEF")    #=> 0

- (void) center(length, padString = ' ')

If integer is greater than the length of str, returns a new String of length integer with str centered and padded with padstr; otherwise, returns str.

Examples:

  R("hello").center(4)         # => "hello"
  R("hello").center(20)        # => "       hello        "
  R("hello").center(20, '123') # => "1231231hello12312312"

- (void) chars(block)

Note: Does not typecast chars to R.Strings.

Passes each character in str to the given block, or returns an enumerator if no block is given.

Examples:

  R("hello").each_char (c) -> R.puts(c + ' ')
  # out: h e l l o

- (void) chomp(sep = null)

Returns a new String with the given record separator removed from the end of str (if present). If $/ has not been changed from the default Ruby record separator, then chomp also removes carriage return characters (that is it will remove \n, \r, and \r\n).

Examples:

  R("hello").chomp()            # => "hello"
  R("hello\n").chomp()          # => "hello"
  R("hello\r\n").chomp()        # => "hello"
  R("hello\n\r").chomp()        # => "hello\n"
  R("hello\r").chomp()          # => "hello"
  R("hello \n there").chomp()   # => "hello \n there"
  R("hello").chomp("llo")       # => "he"

- (void) chomp_bang(sep = null)

TODO: finish specs

Modifies str in place as described for String#chomp, returning str, or nil if no modifications were made. #

- (void) chop()

Returns a new String with the last character removed. If the string ends with \r\n, both characters are removed. Applying chop to an empty string returns an empty string. String#chomp is often a safer alternative, as it leaves the string unchanged if it doesn’t end in a record separator.

Examples:

  R("string\r\n").chop()   # => "string"
  R("string\n\r").chop()   # => "string\n"
  R("string\n").chop()     # => "string"
  R("string").chop()       # => "strin"
  R("x").chop().chop()     # => ""

- (void) chr()

Returns a one-character string at the beginning of the string.

Examples:

  a = R("hello")
  a.chr()                  # => 'h'

- (void) clear()

Makes string empty.

Examples:

  a = "abcde"
  a.clear()    #=> ""

- (void) count(args...)

TODO: expect( s.count("A-a")).toEqual s.count("A-Z[\]^_`a")

Each other_str parameter defines a set of characters to count. The intersection of these sets defines the characters to count in str. Any other_str that starts with a caret (^) is negated. The sequence c1–c2 means all characters between c1 and c2.

Examples:

  a = R("hello world")
  a.count "lo"            #=> 5
  a.count "lo", "o"       #=> 2
  a.count "hello", "^l"   #=> 4
  a.count "ej-m"          #=> 4

- (void) delete(args...)

TODO: expect( R("ABCabc[]").delete("A-a") ).toEqual R("bc")

Returns a copy of str with all characters in the intersection of its arguments deleted. Uses the same rules for building the set of characters as String#count.

Examples:

  R("hello").delete "l","lo"        #=> "heo"
  R("hello").delete "lo"            #=> "he"
  R("hello").delete "aeiou", "^e"   #=> "hell"
  R("hello").delete "ej-m"          #=> "ho"

- (void) delete_bang(args...)

Performs a delete operation in place, returning str, or nil if str was not modified.

- (void) downcase()

Returns a copy of str with all uppercase letters replaced with their lowercase counterparts. The operation is locale insensitive—only characters “A” to “Z” are affected. Note: case replacement is effective only in ASCII region.

Examples:

  R("hEllO").downcase()   #=> "hello"

- (void) downcase_bang()

Note: case replacement is effective only in ASCII region.

Downcases the contents of str, returning nil if no changes were made.

- (void) dump()

Produces a version of str with all nonprinting characters replaced by \nnn notation and all special characters escaped.

- (void) dup()

- (void) each_line(args...)

Splits str using the supplied parameter as the record separator ($/ by default), passing each substring in turn to the supplied block. If a zero- length record separator is supplied, the string is split into paragraphs delimited by multiple successive newlines.

If no block is given, an enumerator is returned instead.

Examples:

 print "Example one\n"
 "hello\nworld".each_line {|s| p s}
 print "Example two\n"
 "hello\nworld".each_line('l') {|s| p s}
 print "Example three\n"
 "hello\n\n\nworld".each_line('') {|s| p s}
 # produces:
 # Example one
 # "hello\n"
 # "world"
 # Example two
 # "hel"
 # "l"
 # "o\nworl"
 # "d"
 # Example three
 # "hello\n\n\n"
 # "world"

- (void) empty()

Returns true if str has a length of zero.

Examples:

  "hello".empty()   #=> false
  "".empty()        #=> true
  " ".empty()       #=> true

- (void) end_with(needles...)

Returns true if str ends with one of the suffixes given.

- (?) eql(other)

Two strings are equal if they have the same length and content.

Examples:


  R("foo").eql('foo')      # => true
  R("foo").eql('FOO')      # => false
  R("foo").eql(R('foo'))   # => true

Returns:

  • (?) — true/false

- (void) get()

- (void) set(idx, other)

TODO: idx with count. set(0, 2, "a"), aka [0,2] = 'a'
TODO: idx as Regexp

- (void) gsub(pattern, replacement)

Returns a copy of str with the all occurrences of pattern substituted for the second argument. The pattern is typically a Regexp; if given as a String, any regular expression metacharacters it contains will be interpreted literally, e.g. '\d' will match a backlash followed by ‘d’, instead of a digit.

If replacement is a String it will be substituted for the matched text. It may contain back-references to the pattern’s capture groups of the form \d, where d is a group number, or \k, where n is a group name. If it is a double-quoted string, both back-references must be preceded by an additional backslash. However, within replacement the special match variables, such as &$, will not refer to the current match.

If the second argument is a Hash, and the matched text is one of its keys, the corresponding value is the replacement string.

In the block form, the current match string is passed in as a parameter, and variables such as $1, $2, $`, $&, and $' will be set appropriately. The value returned by the block will be substituted for the match on each call.

The result inherits any tainting in the original string or any supplied replacement string.

When neither a block nor a second argument is supplied, an Enumerator is returned.

TODO: add hash syntax, properly document supported features

Examples:

  R("hello").gsub(%r[aeiou]/, '*')                  #=> "h*ll*"
  R("hello").gsub(%r([aeiou])/, '<\1>')             #=> "h<e>ll<o>"
  R("hello").gsub(%r./) {|s| s.ord.to_s + ' '}      #=> "104 101 108 108 111 "
  # R("hello").gsub(%r(?<foo>[aeiou])/, '{\k<foo>}')  #=> "h{e}ll{o}"
  R('hello').gsub(%r[eo]/, 'e' => 3, 'o' => '*')    #=> "h3ll*"

- (void) include(other)

Returns true if str contains the given string or character.

Examples:

  R("hello").include "lo"   #=> true
  R("hello").include "ol"   #=> false
  R("hello").include hh     #=> true

- (void) index(needle, offset)

TODO:

index(regexp)

Returns the index of the first occurrence of the given substring or pattern (regexp) in str. Returns nil if not found. If the second parameter is present, it specifies the position in the string to begin the search.

Examples:

  R("hello").index('e')             #=> 1
  R("hello").index('lo')            #=> 3
  R("hello").index('a')             #=> nil
  R("hello").index(ee)              #=> 1
  R("hello").index(/[aeiou]/, -3)   #=> 4

- (void) insert(idx, other)

Inserts other_str before the character at the given index, modifying str. Negative indices count from the end of the string, and insert after the given character. The intent is insert aString so that it starts at the given index.

Examples:

  R("abcd").insert(0, 'X')    # => "Xabcd"
  R("abcd").insert(3, 'X')    # => "abcXd"
  R("abcd").insert(4, 'X')    # => "abcdX"

inserts after with negative counts

  R("abcd").insert(-3, 'X')   # => "abXcd"
  R("abcd").insert(-1, 'X')   # => "abcdX"

- (void) inspect()

TODO: For now just an alias to dump

Returns a printable version of str, surrounded by quote marks, with special characters escaped.

Examples:

  str = "hello"
  str[3] = "\b"
  str.inspect       #=> "\"hel\\bo\""

- (void) ljust(width, padString = " ")

If integer is greater than the length of str, returns a new String of length integer with str left justified and padded with padstr; otherwise, returns str.

Examples:

  R("hello").ljust(4)            #=> "hello"
  R("hello").ljust(20)           #=> "hello               "
  R("hello").ljust(20, '1234')   #=> "hello123412341234123"

- (void) lstrip()

Returns a copy of str with leading whitespace removed. See also String#rstrip and String#strip.

Examples:

  R("  hello  ").lstrip()   #=> "hello  "
  R("hello").lstrip()       #=> "hello"

- (void) lstrip_bang()

Removes leading whitespace from str, returning nil if no change was made. See also String#rstrip! and String#strip!.

Examples:

  R("  hello  ").lstrip_bang()  #=> "hello  "
  R("hello").lstrip_bang()      #=> nil

- (void) match(args...)

Converts pattern to a Regexp (if it isn’t already one), then invokes its match method on str. If the second parameter is present, it specifies the position in the string to begin the search.

If a block is given, invoke the block with MatchData if match succeed, so that you can write

The return value is a value from block execution in this case.

Examples:

  R('hello').match('(.)\1')      #=> #<MatchData "ll" 1:"l">
  R('hello').match('(.)\1')[0]   #=> "ll"
  R('hello').match(/(.)\1/)[0]   #=> "ll"
  R('hello').match('xx')         #=> nil

with block:

  str.match(pat) {|m| ...}
  # instead of:
  if m = str.match(pat)
    ...
  end

- (void) partition(pattern)

TODO: does not yet accept regexp as pattern
TODO: does not yet affect R['$~']

Searches sep or pattern (regexp) in the string and returns the part before it, the match, and the part after it. If it is not found, returns two empty strings and str.

Examples:

  R("hello").partition("l")         # => ["he", "l", "lo"]
  R("hello").partition("x")         # => ["hello", "", ""]
  R("hello").partition(/.l/)        # => ["h", "el", "lo"]

headers

  partition(sep) -> [head, sep, tail]
  partition(regexp) -> [head, match, tail]

- (void) prepend(other)

Prepend the given string to str.

Examples:

  a = R(“world” )
  a.prepend(“hello ”) #=> “hello world”
  a #=> “hello world”

- (void) replace(val)

TODO: Does not copy taintedness

Replaces the contents and taintedness of str with the corresponding values in other_str.

Examples:


  s = R("hello")            #=> "hello"
  s.replace "world"         #=> "world"
  s.replace R("world")      #=> "world"

- (void) reverse()

Returns a new string with the characters from str in reverse order.

Examples:

  R("stressed").reverse()   #=> "desserts"

- (void) reverse_bang()

Reverses str in place.

- (void) rindex(needle, offset)

TODO:

rindex(/.../) does not add matches to R['$~'] as it should

TODO:

rindex(needle, offset) does not use respond_to?(:to_int) for offset

Returns the index of the last occurrence of the given substring or pattern (regexp) in str. Returns nil if not found. If the second parameter is present, it specifies the position in the string to end the search—characters beyond this point will not be considered.

  to convert it to_int.

Examples:

  R("hello").rindex('e')             #=> 1
  R("hello").rindex('l')             #=> 3
  R("hello").rindex('a')             #=> nil
  R("hello").rindex(ee)              #=> 1
  R("hello").rindex(/[aeiou]/, -2)   #=> 1

- (void) rjust(width, padString = " ")

If integer is greater than the length of str, returns a new String of length integer with str right justified and padded with padstr; otherwise, returns str.

Examples:

  R("hello").rjust(4)            #=> "hello"
  R("hello").rjust(20)           #=> "               hello"
  R("hello").rjust(20, '1234')   #=> "123412341234123hello"

- (void) rpartition(pattern)

TODO: does not yet accept regexp as pattern
TODO: does not yet affect R['$~']

Searches sep or pattern (regexp) in the string from the end of the string, and returns the part before it, the match, and the part after it. If it is not found, returns two empty strings and str.

Examples:

  R("hello").rpartition("l")         #=> ["hel", "l", "o"]
  R("hello").rpartition("x")         #=> ["", "", "hello"]

edge cases

  R("hello").rpartition("x")         #=> ["", "", "hello"]
  R("hello").rpartition("hello")     #=> ["", "hello", ""]

todo:

  R("hello").rpartition(/.l/)        #=> ["he", "ll", "o"]

- (void) rstrip()

Returns a copy of str with trailing whitespace removed. See also String#lstrip and String#strip.

Examples:

  R("  hello  ").rstrip()   #=> "  hello"
  R("hello").rstrip()       #=> "hello"

- (void) rstrip_bang()

Removes trailing whitespace from str, returning nil if no change was made. See also String#lstrip! and String#strip!.

Examples:

  R("  hello  ").rstrip_bang()  #=> "  hello"
  R("hello").rstrip_bang()      #=> nil

- (void) scan(pattern, block = null)

TODO: some untested specs

Both forms iterate through str, matching the pattern (which may be a Regexp or a String). For each match, a result is generated and either added to the result array or passed to the block. If the pattern contains no groups, each individual result consists of the matched string, $&. If the pattern contains groups, each individual result is itself an array containing one entry per group.

Examples:

  a = R("cruel world")
  a.scan(/\w+/)        #=> ["cruel", "world"]
  a.scan(/.../)        #=> ["cru", "el ", "wor"]
  a.scan(/(...)/)      #=> [["cru"], ["el "], ["wor"]]
  a.scan(/(..)(..)/)   #=> [["cr", "ue"], ["l ", "wo"]]
  #And the block form:
  a.scan(/\w+/, (w) -> R.puts "<<#{w}>> ")
  print "\n"
  a.scan(/(.)(.)/, (x,y) -> R.puts y, x )
  print "\n"
  # produces:
  # <<cruel>> <<world>>
  # rceu lowlr

- (void) size()

- (void) slice(index, other)

TODO: Implement support for ranges slice(R.Range.new(...))
TODO: regexp

Element Reference—If passed a single Fixnum, returns a substring of one character at that position. If passed two Fixnum objects, returns a substring starting at the offset given by the first, and with a length given by the second. If passed a range, its beginning and end are interpreted as offsets delimiting the substring to be returned. In all three cases, if an offset is negative, it is counted from the end of str. Returns nil if the initial offset falls outside the string or the length is negative.

If a Regexp is supplied, the matching portion of str is returned. If a numeric or name parameter follows the regular expression, that component of the MatchData is returned instead. If a String is given, that string is returned if it occurs in str. In both cases, nil is returned if there is no match.

Examples:

  a = R("hello there")
  a.slice(1)                      #=> "e"
  a.slice(2, 3)                   #=> "llo"
  a.slice(R.rng(2, 3))            #=> "ll"
  a.slice(-3, 2)                  #=> "er"
  a.slice(R.rng(7, -2))           #=> "her"
  a.slice(R.rng(-4, -2))          #=> "her"
  a.slice(R.rng(-2, -4))          #=> ""
  a.slice(R.rng(12, -1))          #=> null
  a.slice(/[aeiou](.)\11//)       #=> "ell"
  a.slice(/[aeiou](.)\11//, 0)    #=> "ell"
  a.slice(/[aeiou](.)\11//, 1)    #=> "l"
  a.slice(/[aeiou](.)\11//, 2)    #=> null
  a.slice("lo")                   #=> "lo"
  a.slice("bye")                  #=> null

- (void) split(pattern = " ", limit)

TODO: limit is not yet supported

Divides str into substrings based on a delimiter, returning an array of these substrings.

If pattern is a String, then its contents are used as the delimiter when splitting str. If pattern is a single space, str is split on whitespace, with leading whitespace and runs of contiguous whitespace characters ignored.

If pattern is a Regexp, str is divided where the pattern matches. Whenever the pattern matches a zero-length string, str is split into individual characters. If pattern contains groups, the respective matches will be returned in the array as well.

If pattern is omitted, the value of $; is used. If $; is nil (which is the default), str is split on whitespace as if ` ‘ were specified.

If the limit parameter is omitted, trailing null fields are suppressed. If limit is a positive number, at most that number of fields will be returned (if limit is 1, the entire string is returned as the only entry in an array). If negative, there is no limit to the number of fields returned, and trailing null fields are not suppressed.

split(pattern=$;, [limit]) → anArray

Examples:

  R(" now's  the time").split()      #=> ["now's", "the", "time"]
  R(" now's  the time").split(' ')   #=> ["now's", "the", "time"]
  R(" now's  the time").split(/ /)   #=> ["", "now's", "", "the", "time"]
  R("1, 2.34,56, 7").split(%r{,\s*}) #=> ["1", "2.34", "56", "7"]
  R("hello").split(//)               #=> ["h", "e", "l", "l", "o"]
  # R("hello").split(//, 3)            #=> ["h", "e", "llo"]
  R("hi mom").split(%r{\s*})         #=> ["h", "i", "m", "o", "m"]

  R("mellow yellow").split("ello")   #=> ["m", "w y", "w"]
  R("1,2,,3,4,,").split(',')         #=> ["1", "2", "", "3", "4"]
  # R("1,2,,3,4,,").split(',', 4)      #=> ["1", "2", "", "3,4,,"]
  # R("1,2,,3,4,,").split(',', -4)     #=> ["1", "2", "", "3", "4", "", ""]

- (void) squeeze_bang()

TODO: Not yet implemented

- (void) squeeze(pattern...)

TODO: Fix A-a bug

Builds a set of characters from the other_str parameter(s) using the procedure described for String#count. Returns a new string where runs of the same character that occur in this set are replaced by a single character. If no arguments are given, all runs of identical characters are replaced by a single character.

Examples:

  R("yellow moon").squeeze()                #=> "yelow mon"
  R("  now   is  the").squeeze(" ")         #=> " now is the"
  R("putters shoot balls").squeeze("m-z")   #=> "puters shot balls"

- (void) start_with(needles...)

Returns true if str starts with one of the prefixes given.

Examples:

  R("hello").start_with("hell")               #=> true
  # returns true if one of the prefixes matches.
  R("hello").start_with("heaven", "hell")     #=> true
  R("hello").start_with("heaven", "paradise") #=> false

- (void) strip()

Returns a copy of str with leading and trailing whitespace removed.

Examples:

  R("    hello    ").strip()   #=> "hello"
  R("\tgoodbye\r\n").strip()   #=> "goodbye"

- (?) strip_bang()

Removes leading and trailing whitespace from str. Returns nil if str was not altered.

Returns:

  • (?) — str or null

- (void) sub(pattern, replacement)

TODO:

sub does not yet add matches to R['$~'] as it should

TODO: String#sub with pattern and block

Returns a copy of str with the first occurrence of pattern substituted for the second argument. The pattern is typically a Regexp; if given as a String, any regular expression metacharacters it contains will be interpreted literally, e.g. '\d' will match a backlash followed by ‘d’, instead of a digit.

If replacement is a String it will be substituted for the matched text. It may contain back-references to the pattern’s capture groups of the form \d, where d is a group number, or \k, where n is a group name. If it is a double-quoted string, both back-references must be preceded by an additional backslash. However, within replacement the special match variables, such as &$, will not refer to the current match.

If the second argument is a Hash, and the matched text is one of its keys, the corresponding value is the replacement string.

In the block form, the current match string is passed in as a parameter, and variables such as $1, $2, $`, $&, and $' will be set appropriately. The value returned by the block will be substituted for the match on each call.

The result inherits any tainting in the original string or any supplied replacement string.

Examples:

  R("hello").sub(/[aeiou]/, '*')                  #=> "h*llo"
  R("hello").sub(/([aeiou])/, '<\1>')             #=> "h<e>llo"
  R("hello").sub(/./, (s) -> s.ord.to_s() + ' ' ) #=> "104 ello"
  # R("hello").sub(/(?<foo>[aeiou])/, '*\k<foo>*')  #=> "h*e*llo"

- (void) sub_bang(pattern, replacement)

Performs the substitutions of String#sub in place, returning str, or nil if no substitutions were performed.

- (void) succ()

Returns the successor to str. The successor is calculated by incrementing characters starting from the rightmost alphanumeric (or the rightmost character if there are no alphanumerics) in the string. Incrementing a digit always results in another digit, and incrementing a letter results in another letter of the same case. Incrementing nonalphanumerics uses the underlying character set’s collating sequence.

If the increment generates a “carry,” the character to the left of it is incremented. This process repeats until there is no carry, adding an additional character if necessary.

@alias #next

Examples:

  R("abcd").succ()        #=> "abce"
  R("THX1138").succ()     #=> "THX1139"
  R("<<koala>>").succ()   #=> "<<koalb>>"
  R("1999zzz").succ()     #=> "2000aaa"
  R("ZZZ9999").succ()     #=> "AAAA0000"
  R("***").succ()         #=> "**+"

- (void) succ_bang()

Equivalent to String#succ, but modifies the receiver in place.

@alias #next_bang

- (void) swapcase()

Returns a copy of str with uppercase alphabetic characters converted to lowercase and lowercase characters converted to uppercase. Note: case conversion is effective only in ASCII region.

Examples:

  R("Hello").swapcase()          #=> "hELLO"
  R("cYbEr_PuNk11").swapcase()   #=> "CyBeR_pUnK11"

- (void) swapcase_bang()

Equivalent to String#swapcase, but modifies the receiver in place, returning str, or nil if no changes were made. Note: case conversion is effective only in ASCII region.

- (void) to_a()

- (void) to_f()

TODO: Some exotic formats not yet fully supported.

Returns the result of interpreting leading characters in str as a floating point number. Extraneous characters past the end of a valid number are ignored. If there is not a valid number at the start of str, 0.0 is returned. This method never raises an exception.

Examples:

  R("123.45e1").to_f()        #=> 1234.5
  R("45.67 degrees").to_f()   #=> 45.67
  R("thx1138").to_f()         #=> 0.0

- (void) to_i(base)

TODO:

to_i(base) does not remove invalid characters:

TODO:

to_i(0) does not auto-detect base

Returns the result of interpreting leading characters in str as an integer base base (between 2 and 36). Extraneous characters past the end of a valid number are ignored. If there is not a valid number at the start of str, 0 is returned. This method never raises an exception when base is valid.

  - e.g: R("1012").to_i(2) should return 5, but due to 2 being invalid it retunrs 0 now.

Examples:

  R("12345").to_i()           #=> 12345
  R("1_23_45").to_i()           #=> 12345
  R("99 red balloons").to_i() #=> 99
  R("0a").to_i()              #=> 0
  R("0a").to_i(16)            #=> 10
  R("hello").to_i()           #=> 0
  R("1100101").to_i(2)        #=> 101
  R("1100101").to_i(8)        #=> 294977
  R("1100101").to_i(10)       #=> 1100101
  R("1100101").to_i(16)       #=> 17826049
  # but:
  R("_12345").to_i()          #=> 0
  # TODO:
  R("0b10101").to_i(0)        #=> 21
  R("0b1010134").to_i(2)      #=> 21

- (void) to_s()

Returns the receiver.

- (void) tr(from_str, to_str)

Returns a copy of str with the characters in from_str replaced by the corresponding characters in #to_str. If #to_str is shorter than from_str, it is padded with its last character in order to maintain the correspondence.

R("hello").tr('el', 'ip')      #=> "hippo"
R("hello").tr('aeiou', '*')    #=> "h*ll*"

Both strings may use the c1-c2 notation to denote ranges of characters, and from_str may start with a ^, which denotes all characters except those listed.

R("hello").tr('a-y', 'b-z')    #=> "ifmmp"
R("hello").tr('^aeiou', '*')   #=> "*e**o"

- (void) tr_bang(from_str, to_str)

Translates str in place, using the same rules as String#tr. Returns str, or nil if no changes were made.

- (void) tr_s()

TODO: Implement

Processes a copy of str as described under String#tr, then removes duplicate characters in regions that were affected by the translation.

Examples:

  R("hello").tr_s('l', 'r')     #=> "hero"
  R("hello").tr_s('el', '*')    #=> "h*o"
  R("hello").tr_s('el', 'hx')   #=> "hhxo"

- (void) upcase()

Returns a copy of str with all lowercase letters replaced with their uppercase counterparts. The operation is locale insensitive—only characters “a” to “z” are affected. Note: case replacement is effective only in ASCII region.

Examples:

  R("hEllO").upcase()   #=> "HELLO"

- (void) upcase_bang()

Upcases the contents of str, returning nil if no changes were made. Note: case replacement is effective only in ASCII region.

- (void) upto(stop, exclusive, block)

TODO: R('a').upto('c').to_a() should return ['a', 'b', 'c'] (include 'c')

Iterates through successive values, starting at str and ending at other_str inclusive, passing each value in turn to the block. The String#succ method is used to generate each value. If optional second argument exclusive is omitted or is false, the last value will be included; otherwise it will be excluded.

If no block is given, an enumerator is returned instead.

If str and other_str contains only ascii numeric characters, both are recognized as decimal numbers. In addition, the width of string (e.g. leading zeros) is handled appropriately.

Examples:

  R("a8").upto("b6", (s) -> R.puts(s, ''))
  # out: a8 a9 b0 b1 b2 b3 b4 b5 b6

  R("9").upto("11").to_a()   #=> ["9", "10", "11"]
  R("25").upto("5").to_a()   #=> []
  R("07").upto("11").to_a()  #=> ["07", "08", "09", "10", "11"]