String
Strings can be written with double or single quotes, and the two differ in how they treat escapes.
A double-quoted string processes escape sequences: \" for a quote,
\n for a newline, \t for a tab, \r for a carriage return.
puts("test\"string") // test"string
puts("a\tb") // a<tab>b
A single-quoted string is raw: nothing is escaped and a backslash is an ordinary character. This makes it convenient for text containing double quotes.
puts('test "string"') // test "string"
puts('a\tb') // a\tb, a literal backslash and t
Because a single-quoted string performs no escaping, it cannot contain a
single quote at all -- 'test \'string' is a parse error. Use a
double-quoted string when you need one.
a = "test_string";
b = "test" + "_string";
is_true = "test" == "test";
is_false = "test" == "string";
s = "abcdef"
puts(s[2])
puts(s[-2])
puts(s[:2])
puts(s[:-2])
puts(s[2:])
puts(s[-2:])
puts(s[1:-2])
s[2] = "C"
s[-2] = "E"
puts(s)
// should output
"c"
"e"
"ab"
"abcd"
"cdef"
"ef"
"bcd"
"abCdEf"
// you can also use single quotes
'test "string" with doublequotes'
// and you can scape a double quote in a double quote string
"te\"st" == 'te"st'
Literal Specific Methods​
ascii()​
Returns
ARRAY
Returns the character codes of the string, always as an array with one entry per character. A single-character string gives a one-element array and an empty string gives an empty array, so the result never has to be checked for its type before use.
"".ascii()
"a".ascii()
"abc".ascii()
[]
[97]
[97, 98, 99]
capitalize()​
Returns
STRING
Returns a copy with the first character upcased and every following character downcased, as Ruby's capitalize does. A capital in the middle of the string is therefore lost.
a = "hello World!"
a.capitalize()
a
"hello World!"
"Hello world!"
"hello World!"
capitalize!()​
Returns
STRING
Upcases the first character and downcases the rest in place, and returns the string, so calls can be chained.
a = "hello World!"
a.capitalize!()
a
"hello World!"
"Hello world!"
"Hello world!"
chomp([STRING])​
Returns
STRING
Returns a copy with one trailing line ending removed: \r\n, \n or \r. Given a string it removes one trailing occurrence of that string instead. Given "" it removes every trailing \n and \r\n, which is the way to drop blank lines at the end of a file.
a = "line\n"
a.chomp()
a.chomp() == "line"
"abcdd".chomp("d")
"a\n\n\n".chomp("")
"line\n"
"line"
true
"abcd"
"a"
chomp!([STRING])​
Returns
STRING
Removes one trailing line ending in place and returns the string, so calls can be chained. Takes the same optional separator as chomp.
a = "line\n"
a.chomp!()
a
"line\n"
"line"
"line"
chop()​
Returns
STRING
Returns a copy with the last character removed. A trailing \r\n is removed as a unit so a line ending is never left half there. Chopping an empty string gives an empty string rather than an error.
a = "abcd"
a.chop()
a
"".chop()
"abcd"
"abc"
"abcd"
""
chop!()​
Returns
STRING
Removes the last character in place and returns the string, so calls can be chained.
a = "abcd"
a.chop!()
a
"abcd"
"abc"
"abc"
count(STRING)​
Returns
INTEGER
Counts how often a given substring occurs in the string.
"test".count("t")
2
downcase()​
Returns
STRING
Returns a copy with all uppercase letters replaced by their lowercase counterparts.
a = "TEST"
a.downcase()
a
"TEST"
"test"
"TEST"
downcase!()​
Returns
STRING
Replaces uppercase characters with their lowercase counterparts in place and returns the string, so calls can be chained.
a = "TEST"
a.downcase!()
a
"TEST"
"test"
"test"
empty?()​
Returns
BOOLEAN
Returns true when the string has no characters.
"".empty?()
" ".empty?()
true
false
end_with?(STRING...)​
Returns
BOOLEAN
Returns true when the string ends with any of the given strings.
"test.rl".end_with?(".rl")
"test.rl".end_with?(".go")
"test.rl".end_with?(".go", ".rl")
true
false
true
find(STRING)​
Returns
INTEGER
Returns the character index of a given string if found. Otherwise returns -1
"test".find("e")
1
format(ANY...)​
Returns
STRING
Formats according to a format specifier and returns the resulting string
"%s is %d".format("a", 1)
"a is 1"
include?(STRING)​
Returns
BOOLEAN
Returns true when the string contains the given substring.
"test".include?("es")
"test".include?("xy")
true
false
lines()​
Returns
ARRAY
Splits the string at newline escape sequence and return all chunks in an array. Shorthand for string.split("\n").
"a\nb".lines()
["a", "b"]
lstrip()​
Returns
STRING
Returns a copy with leading whitespace removed. See rstrip for the trailing end and strip for both.
a = " test "
a.lstrip()
a
" test "
"test "
" test "
lstrip!()​
Returns
STRING
Removes leading whitespace in place and returns the string, so calls can be chained.
a = " test "
a.lstrip!()
a
" test "
"test "
"test "
replace(STRING, STRING)​
Returns
STRING
Returns a copy with every occurrence of the first string replaced by the second. This is Ruby's gsub with plain strings rather than Ruby's replace.
a = "test"
a.replace("t", "f")
a
"test"
"fesf"
"test"
replace!(STRING, STRING)​
Returns
STRING
Replaces every occurrence of the first string with the second in place and returns the string, so calls can be chained.
a = "test"
a.replace!("t", "f")
a
"test"
"fesf"
"fesf"
reverse()​
Returns
STRING
Returns a copy of the string with all characters reversed.
a = "stressed"
a.reverse()
a
"stressed"
"desserts"
"stressed"
reverse!()​
Returns
STRING
Reverses the characters in place and returns the string, so calls can be chained.
a = "stressed"
a.reverse!()
a
"stressed"
"desserts"
"desserts"
rstrip()​
Returns
STRING
Returns a copy with trailing whitespace removed. See lstrip for the leading end and strip for both.
a = " test "
a.rstrip()
a
" test "
" test"
" test "
rstrip!()​
Returns
STRING
Removes trailing whitespace in place and returns the string, so calls can be chained.
a = " test "
a.rstrip!()
a
" test "
" test"
" test"
size()​
Returns
INTEGER
Returns the amount of characters in the string.
"test".size()
4
split([STRING])​
Returns
ARRAY
Splits the string on a given seperator and returns all the chunks in an array. Default seperator is " "
"a b".split()
"a-b".split("-")
["a", "b"]
["a", "b"]
start_with?(STRING...)​
Returns
BOOLEAN
Returns true when the string starts with any of the given strings.
"test.rl".start_with?("test")
"test.rl".start_with?("prod")
"test.rl".start_with?("prod", "test")
true
false
true
strip()​
Returns
STRING
Returns a copy with leading and trailing whitespace removed.
a = " test "
a.strip()
a
" test "
"test"
" test "
strip!()​
Returns
STRING
Removes leading and trailing whitespace in place and returns the string, so calls can be chained.
a = " test "
a.strip!()
a
" test "
"test"
"test"
swapcase()​
Returns
STRING
Returns a copy with every uppercase character downcased and every lowercase character upcased.
a = "Hello World"
a.swapcase()
a
"Hello World"
"hELLO wORLD"
"Hello World"
swapcase!()​
Returns
STRING
Swaps the case of every character in place and returns the string, so calls can be chained.
a = "Hello World"
a.swapcase!()
a
"Hello World"
"hELLO wORLD"
"hELLO wORLD"
upcase()​
Returns
STRING
Returns a copy with all lowercase letters replaced by their uppercase counterparts.
a = "test"
a.upcase()
a
"test"
"TEST"
"test"
upcase!()​
Returns
STRING
Replaces lowercase characters with their uppercase counterparts in place and returns the string, so calls can be chained.
a = "test"
a.upcase!()
a
"test"
"TEST"
"TEST"
Generic Literal Methods​
is_a?(STRING)​
Returns
BOOLEAN|ERROR
Returns true when the value is of the given type or belongs to the given type group, so one question covers both. The name has to be one that exists: anything else is an error rather than a false, because a typo would otherwise read as a real answer. See Types and type groups.
nil.is_a?("NIL")
"a".is_a?("HASHABLE")
nil.is_a?("HASHABLE")
true.is_a?("INTEGERABLE")
"a".is_a?("INTEGER")
true
true
false
true
false
methods()​
Returns
ARRAY
Returns the names of the methods specific to this literal type, not including the generic methods listed on this page. The names are sorted, so the result is the same on every run. A type with no methods of its own returns an empty array.
1.0.methods().include?("round")
true.methods()
true
[]
nil?()​
Returns
BOOLEAN
Returns true only for nil. Reads better than comparing against nil at the end of a chain, and every type answers it.
nil.nil?()
1.nil?()
"".nil?()
[].first().nil?()
true
false
false
true
to_f()​
Returns
FLOAT|NIL
Converts an object to its float representation, or nil when it cannot. A nil result is what distinguishes a failed conversion from a genuine 0.0.
1.to_f()
"1.4".to_f()
"abc".to_f()
nil.to_f()
1.0
1.4
nil
nil
to_i()​
Returns
INTEGER|NIL
Converts an object to its integer representation, or nil when it cannot. A nil result is what distinguishes a failed conversion from a genuine 0. For strings a 0b, 0o or 0x prefix selects binary, octal or hexadecimal and is matched case insensitively, a leading zero followed only by octal digits is octal, and anything else is decimal. The resulting integer keeps the base it was parsed with, and integers of differing bases cannot be combined directly.
true.to_i()
false.to_i()
1234.to_i()
"4".to_i()
"0".to_i()
"0125".to_i()
"0x2322".to_i()
"0b1010".to_i()
"test".to_i()
1
0
1234
4
0
0o125
0x2322
0b1010
nil
to_json()​
Returns
STRING|ERROR
Returns the object as json notation.
a = {"test": 1234}
a.to_json()
{"test": 1234}
"{\"test\":1234}"
to_s()​
Returns
STRING
Converts an object to its string representation, or the empty string when it has none. Takes no arguments; an integer renders in its own base, so use to_base first to change it.
true.to_s()
1234.to_s()
"test".to_s()
1.4.to_s()
nil.to_s()
"0b1010".to_i().to_s()
"true"
"1234"
"test"
"1.4"
""
"0b1010"
type()​
Returns
STRING
Returns the type of the object.
"test".type()
"STRING"
type_groups()​
Returns
ARRAY
Returns the type groups the value belongs to, sorted. ANY is not listed: every value belongs to it, so it would say nothing while prefixing every answer. It exists for signatures, where push(ANY) means the argument accepts anything, and is_a?("ANY") still answers true. See Types and type groups for what each group means.
1.type_groups()
nil.type_groups()
def() end.type_groups()
puts.type_groups()
["COMPARABLE", "HASHABLE", "INTEGERABLE", "NUMERIC", "STRINGABLE"]
["STRINGABLE"]
["CALLABLE"]
["CALLABLE"]
wat()​
Returns
NIL
Prints the type's literal-specific methods with their argument and return types, sorted by name, one per line. It returns nil rather than the listing: this exists to be read, and the REPL echoes a returned value through its escaped representation, which would put the whole thing on one line. Use methods when the names are wanted as data. A type with no methods of its own prints only the heading.
true.wat()
1.0.wat()
BOOLEAN supports the following methods:
nil
FLOAT supports the following methods:
abs()
ceil([INTEGER])
divmod(FLOAT)
finite?()
floor([INTEGER])
infinite?()
nan?()
negative?()
positive?()
round([INTEGER])
truncate([INTEGER])
zero?()
nil