If the string is invalid byte sequence then replace invalid bytes with given replacement character, else returns self. If block is given, replace invalid bytes with returned value of the block.
"abc\u3042\x81".scrub #=> "abc\u3042\uFFFD" "abc\u3042\x81".scrub("*") #=> "abc\u3042*" "abc\u3042\xE3\x80".scrub{|bytes| '<'+bytes.unpack('H*')[0]+'>' } #=> "abc\u3042<e380>"
If the string is invalid byte sequence then replace invalid bytes with given replacement character, else returns self. If block is given, replace invalid bytes with returned value of the block.
"abc\u3042\x81".scrub! #=> "abc\u3042\uFFFD" "abc\u3042\x81".scrub!("*") #=> "abc\u3042*" "abc\u3042\xE3\x80".scrub!{|bytes| '<'+bytes.unpack('H*')[0]+'>' } #=> "abc\u3042<e380>"
If the string is frozen, then return duplicated mutable string.
If the string is not frozen, then return the string itself.
If the string is frozen, then return the string itself.
If the string is not frozen, then duplicate the string freeze it and return it.
Produces a version of str
with all non-printing characters replaced by \nnn
notation and all special characters escaped.
"hello \n ''".dump #=> "\"hello \\n ''\"
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.
"hEllO".upcase #=> "HELLO"
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.
"hEllO".downcase #=> "hello"
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.
"hello".capitalize #=> "Hello" "HELLO".capitalize #=> "Hello" "123ABC".capitalize #=> "123abc"
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.
"Hello".swapcase #=> "hELLO" "cYbEr_PuNk11".swapcase #=> "CyBeR_pUnK11"
Upcases the contents of str, returning nil
if no changes were made. Note: case replacement is effective only in ASCII region.
Downcases the contents of str, returning nil
if no changes were made. Note: case replacement is effective only in ASCII region.
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.
a = "hello" a.capitalize! #=> "Hello" a #=> "Hello" a.capitalize! #=> nil
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.
Treats leading characters from str as a string of hexadecimal digits (with an optional sign and an optional 0x
) and returns the corresponding number. Zero is returned on error.
"0x0a".hex #=> 10 "-1234".hex #=> -4660 "0".hex #=> 0 "wombat".hex #=> 0
Treats leading characters of str as a string of octal digits (with an optional sign) and returns the corresponding number. Returns 0 if the conversion fails.
"123".oct #=> 83 "-377".oct #=> -255 "bad".oct #=> 0 "0377bad".oct #=> 255
If str
starts with 0
, radix indicators are hornored. See Kernel#Integer
.
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 nil
, 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 split substrings will be returned (captured groups will be returned as well, but are not counted towards the limit). 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.
When the input str
is empty an empty Array is returned as the string is considered to have no fields to split.
" now's the time".split #=> ["now's", "the", "time"] " now's the time".split(' ') #=> ["now's", "the", "time"] " now's the time".split(/ /) #=> ["", "now's", "", "the", "time"] "1, 2.34,56, 7".split(%r{,\s*}) #=> ["1", "2.34", "56", "7"] "hello".split(//) #=> ["h", "e", "l", "l", "o"] "hello".split(//, 3) #=> ["h", "e", "llo"] "hi mom".split(%r{\s*}) #=> ["h", "i", "m", "o", "m"] "mellow yellow".split("ello") #=> ["m", "w y", "w"] "1,2,,3,4,,".split(',') #=> ["1", "2", "", "3", "4"] "1,2,,3,4,,".split(',', 4) #=> ["1", "2", "", "3,4,,"] "1,2,,3,4,,".split(',', -4) #=> ["1", "2", "", "3", "4", "", ""] "1:2:3".split(/(:)()()/, 2) #=> ["1", ":", "", "", "2:3"] "".split(',', -1) #=> []
Returns an array of bytes in str. This is a shorthand for str.each_byte.to_a
.
If a block is given, which is a deprecated form, works the same as each_byte
.
Returns an array of characters in str. This is a shorthand for str.each_char.to_a
.
If a block is given, which is a deprecated form, works the same as each_char
.
Returns a new string with the characters from str in reverse order.
"stressed".reverse #=> "desserts"
Reverses str in place.
Append—Concatenates the given object to str. If the object is a Integer
, it is considered as a codepoint, and is converted to a character before concatenation.
a = "hello " a << "world" #=> "hello world" a.concat(33) #=> "hello world!"
Append—Concatenates the given object to str. If the object is a Integer
, it is considered as a codepoint, and is converted to a character before concatenation.
a = "hello " a << "world" #=> "hello world" a.concat(33) #=> "hello world!"
Prepend—Prepend the given string to str.
a = "world" a.prepend("hello ") #=> "hello world" a #=> "hello world"
Applies a one-way cryptographic hash to str by invoking the standard library function crypt(3)
with the given salt string. While the format and the result are system and implementation dependent, using a salt matching the regular expression \A[a-zA-Z0-9./]{2}
should be valid and safe on any platform, in which only the first two characters are significant.
This method is for use in system specific scripts, so if you want a cross-platform hash function consider using Digest
or OpenSSL
instead.