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"
Returns self
with only the first occurrence (not all occurrences) of the given pattern
replaced.
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!
.
Like String#chomp
, but modifies self
in place; returns nil
if no modification made, self
otherwise.
Returns a copy of self
with characters specified by selectors
removed (see Multiple Character Selectors):
"hello".delete "l","lo" #=> "heo" "hello".delete "lo" #=> "he" "hello".delete "aeiou", "^e" #=> "hell" "hello".delete "ej-m" #=> "ho"
Returns a copy of self
with characters specified by selectors
“squeezed” (see Multiple Character Selectors):
“Squeezed” means that each multiple-character run of a selected character is squeezed down to a single character; with no arguments given, squeezes all characters:
"yellow moon".squeeze #=> "yelow mon" " now is the".squeeze(" ") #=> " now is the" "putters shoot balls".squeeze("m-z") #=> "puters shot balls"
Returns the total number of characters in self
that are specified by the given selectors
(see Multiple Character Selectors):
a = "hello world" a.count "lo" #=> 5 a.count "lo", "o" #=> 2 a.count "hello", "^l" #=> 4 a.count "ej-m" #=> 4 "hello^world".count "\\^aeiou" #=> 4 "hello-world".count "a\\-eo" #=> 4 c = "hello world\\r\\n" c.count "\\" #=> 2 c.count "\\A" #=> 0 c.count "X-\\w" #=> 3
Like String#delete
, but modifies self
in place. Returns self
if any changes were made, nil
otherwise.
Like String#squeeze
, but modifies self
in place. Returns self
if any changes were made, nil
otherwise.
Returns a basic n
-bit checksum of the characters in self
; the checksum is the sum of the binary value of each byte in self
, modulo 2**n - 1
:
'hello'.sum # => 532 'hello'.sum(4) # => 4 'hello'.sum(64) # => 532 'тест'.sum # => 1405 'こんにちは'.sum # => 2582
This is not a particularly strong checksum.
Returns the substring of self
specified by the arguments. See examples at String Slices.
Removes and returns the substring of self
specified by the arguments. See String Slices.
A few examples:
string = "This is a string" string.slice!(2) #=> "i" string.slice!(3..6) #=> " is " string.slice!(/s.*t/) #=> "sa st" string.slice!("r") #=> "r" string #=> "Thing"
Returns a 3-element array of substrings of self
.
Matches a pattern against self
, scanning from the beginning. The pattern is:
string_or_regexp
itself, if it is a Regexp
.
Regexp.quote(string_or_regexp)
, if string_or_regexp
is a string.
If the pattern is matched, returns pre-match, first-match, post-match:
'hello'.partition('l') # => ["he", "l", "lo"] 'hello'.partition('ll') # => ["he", "ll", "o"] 'hello'.partition('h') # => ["", "h", "ello"] 'hello'.partition('o') # => ["hell", "o", ""] 'hello'.partition(/l+/) #=> ["he", "ll", "o"] 'hello'.partition('') # => ["", "", "hello"] 'тест'.partition('т') # => ["", "т", "ест"] 'こんにちは'.partition('に') # => ["こん", "に", "ちは"]
If the pattern is not matched, returns a copy of self
and two empty strings:
'hello'.partition('x') # => ["hello", "", ""]
Related: String#rpartition
, String#split
.
Returns a 3-element array of substrings of self
.
Matches a pattern against self
, scanning backwards from the end. The pattern is:
string_or_regexp
itself, if it is a Regexp
.
Regexp.quote(string_or_regexp)
, if string_or_regexp
is a string.
If the pattern is matched, returns pre-match, last-match, post-match:
'hello'.rpartition('l') # => ["hel", "l", "o"] 'hello'.rpartition('ll') # => ["he", "ll", "o"] 'hello'.rpartition('h') # => ["", "h", "ello"] 'hello'.rpartition('o') # => ["hell", "o", ""] 'hello'.rpartition(/l+/) # => ["hel", "l", "o"] 'hello'.rpartition('') # => ["hello", "", ""] 'тест'.rpartition('т') # => ["тес", "т", ""] 'こんにちは'.rpartition('に') # => ["こん", "に", "ちは"]
If the pattern is not matched, returns two empty strings and a copy of self
:
'hello'.rpartition('x') # => ["", "", "hello"]
Related: String#partition
, String#split
.
Returns a copy of self
that has ASCII-8BIT encoding; the underlying bytes are not modified:
s = "\x99" s.encoding # => #<Encoding:UTF-8> t = s.b # => "\x99" t.encoding # => #<Encoding:ASCII-8BIT> s = "\u4095" # => "䂕" s.encoding # => #<Encoding:UTF-8> s.bytes # => [228, 130, 149] t = s.b # => "\xE4\x82\x95" t.encoding # => #<Encoding:ASCII-8BIT> t.bytes # => [228, 130, 149]
Returns a frozen, possibly pre-existing copy of the string.
The returned String will be deduplicated as long as it does not have any instance variables set on it and is not a String
subclass.
Note that -string
variant is more convenient for defining constants:
FILENAME = -'config/database.yml'
while dedup
is better suitable for using the method in chains of calculations:
@url_list.concat(urls.map(&:dedup))