Upcases the characters in self
; returns self
if any changes were made, nil
otherwise:
s = 'Hello World!' # => "Hello World!" s.upcase! # => "HELLO WORLD!" s # => "HELLO WORLD!" s.upcase! # => nil
The casing may be affected by the given options
; see Case Mapping.
Related: String#upcase
, String#downcase
, String#downcase!
.
Downcases the characters in self
; returns self
if any changes were made, nil
otherwise:
s = 'Hello World!' # => "Hello World!" s.downcase! # => "hello world!" s # => "hello world!" s.downcase! # => nil
The casing may be affected by the given options
; see Case Mapping.
Related: String#downcase
, String#upcase
, String#upcase!
.
Upcases the first character in self
; downcases the remaining characters; returns self
if any changes were made, nil
otherwise:
s = 'hello World!' # => "hello World!" s.capitalize! # => "Hello world!" s # => "Hello world!" s.capitalize! # => nil
The casing may be affected by the given options
; see Case Mapping.
Related: String#capitalize
.
Upcases each lowercase character in self
; downcases uppercase character; returns self
if any changes were made, nil
otherwise:
s = 'Hello World!' # => "Hello World!" s.swapcase! # => "hELLO wORLD!" s # => "hELLO wORLD!" ''.swapcase! # => nil
The casing may be affected by the given options
; see Case Mapping.
Related: String#swapcase
.
Interprets the leading substring of self
as a string of hexadecimal digits (with an optional sign and an optional 0x
) and returns the corresponding number; returns zero if there is no such leading substring:
'0x0a'.hex # => 10 '-1234'.hex # => -4660 '0'.hex # => 0 'non-numeric'.hex # => 0
Related: String#oct
.
Interprets the leading substring of self
as a string of octal digits (with an optional sign) and returns the corresponding number; returns zero if there is no such leading substring:
'123'.oct # => 83 '-377'.oct # => -255 '0377non-numeric'.oct # => 255 'non-numeric'.oct # => 0
If self
starts with 0
, radix indicators are honored; see Kernel#Integer
.
Related: String#hex
.
Returns an array of substrings of self
that are the result of splitting self
at each occurrence of the given field separator field_sep
.
When field_sep
is $;
:
If $;
is nil
(its default value), the split occurs just as if field_sep
were given as a space character (see below).
If $;
is a string, the split occurs just as if field_sep
were given as that string (see below).
When field_sep
is ' '
and limit
is 0
(its default value), the split occurs at each sequence of whitespace:
'abc def ghi'.split(' ') => ["abc", "def", "ghi"] "abc \n\tdef\t\n ghi".split(' ') # => ["abc", "def", "ghi"] 'abc def ghi'.split(' ') => ["abc", "def", "ghi"] ''.split(' ') => []
When field_sep
is a string different from ' '
and limit
is 0
, the split occurs at each occurrence of field_sep
; trailing empty substrings are not returned:
'abracadabra'.split('ab') => ["", "racad", "ra"] 'aaabcdaaa'.split('a') => ["", "", "", "bcd"] ''.split('a') => [] '3.14159'.split('1') => ["3.", "4", "59"] '!@#$%^$&*($)_+'.split('$') # => ["!@#", "%^", "&*(", ")_+"] 'тест'.split('т') => ["", "ес"] 'こんにちは'.split('に') => ["こん", "ちは"]
When field_sep
is a Regexp
and limit
is 0
, the split occurs at each occurrence of a match; trailing empty substrings are not returned:
'abracadabra'.split(/ab/) # => ["", "racad", "ra"] 'aaabcdaaa'.split(/a/) => ["", "", "", "bcd"] 'aaabcdaaa'.split(//) => ["a", "a", "a", "b", "c", "d", "a", "a", "a"] '1 + 1 == 2'.split(/\W+/) # => ["1", "1", "2"]
If the Regexp contains groups, their matches are also included in the returned array:
'1:2:3'.split(/(:)()()/, 2) # => ["1", ":", "", "", "2:3"]
As seen above, if limit
is 0
, trailing empty substrings are not returned:
'aaabcdaaa'.split('a') => ["", "", "", "bcd"]
If limit
is positive integer n
, no more than n - 1-
splits occur, so that at most n
substrings are returned, and trailing empty substrings are included:
'aaabcdaaa'.split('a', 1) # => ["aaabcdaaa"] 'aaabcdaaa'.split('a', 2) # => ["", "aabcdaaa"] 'aaabcdaaa'.split('a', 5) # => ["", "", "", "bcd", "aa"] 'aaabcdaaa'.split('a', 7) # => ["", "", "", "bcd", "", "", ""] 'aaabcdaaa'.split('a', 8) # => ["", "", "", "bcd", "", "", ""]
Note that if field_sep
is a Regexp containing groups, their matches are in the returned array, but do not count toward the limit.
If limit
is negative, it behaves the same as if limit
was zero, meaning that there is no limit, and trailing empty substrings are included:
'aaabcdaaa'.split('a', -1) # => ["", "", "", "bcd", "", "", ""]
If a block is given, it is called with each substring:
'abc def ghi'.split(' ') {|substring| p substring }
Output:
"abc" "def" "ghi"
Related: String#partition
, String#rpartition
.
Returns an array of the bytes in self
:
'hello'.bytes # => [104, 101, 108, 108, 111] 'тест'.bytes # => [209, 130, 208, 181, 209, 129, 209, 130] 'こんにちは'.bytes # => [227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]
Returns an array of the characters in self
:
'hello'.chars # => ["h", "e", "l", "l", "o"] 'тест'.chars # => ["т", "е", "с", "т"] 'こんにちは'.chars # => ["こ", "ん", "に", "ち", "は"]
Returns a new string with the characters from self
in reverse order.
'stressed'.reverse # => "desserts"
Returns self
with its characters reversed:
s = 'stressed' s.reverse! # => "desserts" s # => "desserts"
Concatenates each object in objects
to self
and returns self
:
s = 'foo' s.concat('bar', 'baz') # => "foobarbaz" s # => "foobarbaz"
For each given object object
that is an Integer
, the value is considered a codepoint and converted to a character before concatenation:
s = 'foo' s.concat(32, 'bar', 32, 'baz') # => "foo bar baz"
Related: String#<<
, which takes a single argument.
Concatenates object
to self
and returns self
:
s = 'foo' s << 'bar' # => "foobar" s # => "foobar"
If object
is an Integer
, the value is considered a codepoint and converted to a character before concatenation:
s = 'foo' s << 33 # => "foo!"
If that codepoint is not representable in the encoding of string, RangeError
is raised.
s = 'foo' s.encoding # => <Encoding:UTF-8> s << 0x00110000 # 1114112 out of char range (RangeError) s = 'foo'.encode(Encoding::EUC_JP) s << 0x00800080 # invalid codepoint 0x800080 in EUC-JP (RangeError)
If the encoding is US-ASCII and the codepoint is 0..0xff, string is automatically promoted to ASCII-8BIT.
s = 'foo'.encode(Encoding::US_ASCII) s << 0xff s.encoding # => #<Encoding:BINARY (ASCII-8BIT)>
Related: String#concat
, which takes multiple arguments.
Prepends each string in other_strings
to self
and returns self
:
s = 'foo' s.prepend('bar', 'baz') # => "barbazfoo" s # => "barbazfoo"
Related: String#concat
.
Returns the string generated by calling crypt(3)
standard library function with str
and salt_str
, in this order, as its arguments. Please do not use this method any longer. It is legacy; provided only for backward compatibility with ruby scripts in earlier days. It is bad to use in contemporary programs for several reasons:
Behaviour of C’s crypt(3)
depends on the OS it is run. The generated string lacks data portability.
On some OSes such as Mac OS, crypt(3)
never fails (i.e. silently ends up in unexpected results).
On some OSes such as Mac OS, crypt(3)
is not thread safe.
So-called “traditional” usage of crypt(3)
is very very very weak. According to its manpage, Linux’s traditional crypt(3)
output has only 2**56 variations; too easy to brute force today. And this is the default behaviour.
In order to make things robust some OSes implement so-called “modular” usage. To go through, you have to do a complex build-up of the salt_str
parameter, by hand. Failure in generation of a proper salt string tends not to yield any errors; typos in parameters are normally not detectable.
For instance, in the following example, the second invocation of String#crypt
is wrong; it has a typo in “round=” (lacks “s”). However the call does not fail and something unexpected is generated.
"foo".crypt("$5$rounds=1000$salt$") # OK, proper usage "foo".crypt("$5$round=1000$salt$") # Typo not detected
Even in the “modular” mode, some hash functions are considered archaic and no longer recommended at all; for instance module $1$
is officially abandoned by its author: see phk.freebsd.dk/sagas/md5crypt_eol/ . For another instance module $3$
is considered completely broken: see the manpage of FreeBSD.
On some OS such as Mac OS, there is no modular mode. Yet, as written above, crypt(3)
on Mac OS never fails. This means even if you build up a proper salt string it generates a traditional DES hash anyways, and there is no way for you to be aware of.
"foo".crypt("$5$rounds=1000$salt$") # => "$5fNPQMxC5j6."
If for some reason you cannot migrate to other secure contemporary password hashing algorithms, install the string-crypt gem and require 'string/crypt'
to continue using it.
Returns the integer ordinal of the first character of self
:
'h'.ord # => 104 'hello'.ord # => 104 'тест'.ord # => 1090 'こんにちは'.ord # => 12371
Matches a pattern against self
; the pattern is:
string_or_regexp
itself, if it is a Regexp
.
Regexp.quote(string_or_regexp)
, if string_or_regexp
is a string.
Iterates through self
, generating a collection of matching results:
If the pattern contains no groups, each result is the matched string, $&
.
If the pattern contains groups, each result is an array containing one entry per group.
With no block given, returns an array of the results:
s = 'cruel world' s.scan(/\w+/) # => ["cruel", "world"] s.scan(/.../) # => ["cru", "el ", "wor"] s.scan(/(...)/) # => [["cru"], ["el "], ["wor"]] s.scan(/(..)(..)/) # => [["cr", "ue"], ["l ", "wo"]]
With a block given, calls the block with each result; returns self
:
s.scan(/\w+/) {|w| print "<<#{w}>> " } print "\n" s.scan(/(.)(.)/) {|x,y| print y, x } print "\n"
Output:
<<cruel>> <<world>> rceu lowlr
Returns a centered copy of self
.
If integer argument size
is greater than the size (in characters) of self
, returns a new string of length size
that is a copy of self
, centered and padded on both ends with pad_string
:
'hello'.center(10) # => " hello " ' hello'.center(10) # => " hello " 'hello'.center(10, 'ab') # => "abhelloaba" 'тест'.center(10) # => " тест " 'こんにちは'.center(10) # => " こんにちは "
If size
is not greater than the size of self
, returns a copy of self
:
'hello'.center(5) # => "hello" 'hello'.center(1) # => "hello"
Related: String#ljust
, String#rjust
.
Returns a copy of self
with only the first occurrence (not all occurrences) of the given pattern
replaced.
See Substitution Methods.
Related: String#sub!
, String#gsub
, String#gsub!
.
Returns a copy of self
with all occurrences of the given pattern
replaced.
See Substitution Methods.
Returns an Enumerator
if no replacement
and no block given.
Related: String#sub
, String#sub!
, String#gsub!
.
Returns a new string copied from self
, with trailing characters possibly removed.
Removes "\r\n"
if those are the last two characters.
"abc\r\n".chop # => "abc" "тест\r\n".chop # => "тест" "こんにちは\r\n".chop # => "こんにちは"
Otherwise removes the last character if it exists.
'abcd'.chop # => "abc" 'тест'.chop # => "тес" 'こんにちは'.chop # => "こんにち" ''.chop # => ""
If you only need to remove the newline separator at the end of the string, String#chomp
is a better alternative.
Returns a new string copied from self
, with trailing characters possibly removed:
When line_sep
is "\n"
, removes the last one or two characters if they are "\r"
, "\n"
, or "\r\n"
(but not "\n\r"
):
$/ # => "\n" "abc\r".chomp # => "abc" "abc\n".chomp # => "abc" "abc\r\n".chomp # => "abc" "abc\n\r".chomp # => "abc\n" "тест\r\n".chomp # => "тест" "こんにちは\r\n".chomp # => "こんにちは"
When line_sep
is ''
(an empty string), removes multiple trailing occurrences of "\n"
or "\r\n"
(but not "\r"
or "\n\r"
):
"abc\n\n\n".chomp('') # => "abc" "abc\r\n\r\n\r\n".chomp('') # => "abc" "abc\n\n\r\n\r\n\n\n".chomp('') # => "abc" "abc\n\r\n\r\n\r".chomp('') # => "abc\n\r\n\r\n\r" "abc\r\r\r".chomp('') # => "abc\r\r\r"
When line_sep
is neither "\n"
nor ''
, removes a single trailing line separator if there is one:
'abcd'.chomp('d') # => "abc" 'abcdd'.chomp('d') # => "abcd"
Replaces the first occurrence (not all occurrences) of the given pattern
on self
; returns self
if a replacement occurred, nil
otherwise.
See Substitution Methods.
Related: String#sub
, String#gsub
, String#gsub!
.
Performs the specified substring replacement(s) on self
; returns self
if any replacement occurred, nil
otherwise.
See Substitution Methods.
Returns an Enumerator
if no replacement
and no block given.
Related: String#sub
, String#gsub
, String#sub!
.
Like String#chop
, but modifies self
in place; returns nil
if self
is empty, self
otherwise.
Related: String#chomp!
.