Returns a copy of self
with each invalid byte sequence replaced by the given replacement_string
.
With no block given and no argument, replaces each invalid sequence with the default replacement string ("�"
for a Unicode encoding, '?'
otherwise):
s = "foo\x81\x81bar" s.scrub # => "foo��bar"
With no block given and argument replacement_string
given, replaces each invalid sequence with that string:
"foo\x81\x81bar".scrub('xyzzy') # => "fooxyzzyxyzzybar"
With a block given, replaces each invalid sequence with the value of the block:
"foo\x81\x81bar".scrub {|bytes| p bytes; 'XYZZY' } # => "fooXYZZYXYZZYbar"
Output:
"\x81" "\x81"
Like String#scrub
, except that any replacements are made in self
.
Returns self
if self
is not frozen and can be mutated without warning issuance.
Otherwise returns self.dup
, which is not frozen.
Related: see Freezing/Unfreezing.
Returns a frozen string equal to self
.
The returned string is self
if and only if all of the following are true:
self
is already frozen.
self
is an instance of String (rather than of a subclass of String)
self
has no instance variables set on it.
Otherwise, the returned string is a frozen copy of self
.
Returning self
, when possible, saves duplicating self
; see Data deduplication.
It may also save duplicating other, already-existing, strings:
s0 = 'foo' s1 = 'foo' s0.object_id == s1.object_id # => false (-s0).object_id == (-s1).object_id # => true
Note that method -@
is convenient for defining a constant:
FileName = -'config/database.yml'
While its alias dedup
is better suited for chaining:
'foo'.dedup.gsub!('o')
Related: see Freezing/Unfreezing.
Returns a printable version of self
, enclosed in double-quotes, with special characters escaped, and with non-printing characters replaced by hexadecimal notation:
"hello \n ''".dump # => "\"hello \\n ''\"" "\f\x00\xff\\\"".dump # => "\"\\f\\x00\\xFF\\\\\\\"\""
Related: String#undump
(inverse of String#dump
).
Returns an unescaped version of self
:
s_orig = "\f\x00\xff\\\"" # => "\f\u0000\xFF\\\"" s_dumped = s_orig.dump # => "\"\\f\\x00\\xFF\\\\\\\"\"" s_undumped = s_dumped.undump # => "\f\u0000\xFF\\\"" s_undumped == s_orig # => true
Related: String#dump
(inverse of String#undump
).
Returns a string containing the upcased characters in self
:
s = 'Hello World!' # => "Hello World!" s.upcase # => "HELLO WORLD!"
The casing may be affected by the given options
; see Case Mapping.
Related: String#upcase!
, String#downcase
, String#downcase!
.
Returns a string containing the downcased characters in self
:
s = 'Hello World!' # => "Hello World!" s.downcase # => "hello world!"
The casing may be affected by the given options
; see Case Mapping.
Related: String#downcase!
, String#upcase
, String#upcase!
.
Returns a string containing the characters in self
; the first character is upcased; the remaining characters are downcased:
s = 'hello World!' # => "hello World!" s.capitalize # => "Hello world!"
The casing may be affected by the given options
; see Case Mapping.
Related: String#capitalize!
.
Returns a string containing the characters in self
, with cases reversed; each uppercase character is downcased; each lowercase character is upcased:
s = 'Hello World!' # => "Hello World!" s.swapcase # => "hELLO wORLD!"
The casing may be affected by the given options
; see Case Mapping.
Related: String#swapcase!
.
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 and returns self
:
'abc def ghi'.split(' ') {|substring| p substring }
Output:
"abc" "def" "ghi" => "abc def ghi"
Note that the above example is functionally the same as calling each after split
and giving the same block. However, the above example has better performance because it avoids the creation of an intermediate array. Also, note the different return values.
'abc def ghi'.split(' ').each {|substring| p substring }
Output:
"abc" "def" "ghi" => ["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.
Appends a string representation of object
to self
; returns self
.
If object
is a string, appends it to self
:
s = 'foo' s << 'bar' # => "foobar" s # => "foobar"
If object
is an integer, its value is considered a codepoint; converts the value to a character before concatenating:
s = 'foo' s << 33 # => "foo!"
Additionally, if the codepoint is in range 0..0xff
and the encoding of self
is Encoding::US_ASCII, changes the encoding to Encoding::ASCII_8BIT:
s = 'foo'.encode(Encoding::US_ASCII) s.encoding # => #<Encoding:US-ASCII> s << 0xff # => "foo\xFF" s.encoding # => #<Encoding:BINARY (ASCII-8BIT)>
Raises RangeError
if that codepoint is not representable in the encoding of self
:
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)
Related: see Modifying.
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.