Returns the numerator.
Returns whether self
‘s encoding is EUC-JP or not.
Case-insensitive version of String#<=>
. Currently, case-insensitivity only works on characters A-Z/a-z, not all of Unicode. This is different from String#casecmp?
.
"aBcDeF".casecmp("abcde") #=> 1 "aBcDeF".casecmp("abcdef") #=> 0 "aBcDeF".casecmp("abcdefg") #=> -1 "abcdef".casecmp("ABCDEF") #=> 0
nil
is returned if the two strings have incompatible encodings, or if other_str
is not a string.
"foo".casecmp(2) #=> nil "\u{e4 f6 fc}".encode("ISO-8859-1").casecmp("\u{c4 d6 dc}") #=> nil
Returns true
if str
and other_str
are equal after Unicode case folding, false
if they are not equal.
"aBcDeF".casecmp?("abcde") #=> false "aBcDeF".casecmp?("abcdef") #=> true "aBcDeF".casecmp?("abcdefg") #=> false "abcdef".casecmp?("ABCDEF") #=> true "\u{e4 f6 fc}".casecmp?("\u{c4 d6 dc}") #=> true
nil
is returned if the two strings have incompatible encodings, or if other_str
is not a string.
"foo".casecmp?(2) #=> nil "\u{e4 f6 fc}".encode("ISO-8859-1").casecmp?("\u{c4 d6 dc}") #=> nil
Returns true
if str has a length of zero.
"hello".empty? #=> false " ".empty? #=> false "".empty? #=> true
Iterates through successive values, starting at str and ending at other_str inclusive, passing each value in turn to the block. The String#succ
method is used to generate each value. If optional second argument exclusive is omitted or is false, the last value will be included; otherwise it will be excluded.
If no block is given, an enumerator is returned instead.
"a8".upto("b6") {|s| print s, ' ' } for s in "a8".."b6" print s, ' ' end
produces:
a8 a9 b0 b1 b2 b3 b4 b5 b6 a8 a9 b0 b1 b2 b3 b4 b5 b6
If str and other_str contains only ascii numeric characters, both are recognized as decimal numbers. In addition, the width of string (e.g. leading zeros) is handled appropriately.
"9".upto("11").to_a #=> ["9", "10", "11"] "25".upto("5").to_a #=> [] "07".upto("11").to_a #=> ["07", "08", "09", "10", "11"]
modifies the indexth byte as integer.
Returns a copy of str with all lowercase letters replaced with their uppercase counterparts.
See String#downcase
for meaning of options
and use with different encodings.
"hEllO".upcase #=> "HELLO"
Returns a copy of str with all uppercase letters replaced with their lowercase counterparts. Which letters exactly are replaced, and by which other letters, depends on the presence or absence of options, and on the encoding
of the string.
The meaning of the options
is as follows:
Full Unicode case mapping, suitable for most languages (see :turkic and :lithuanian options below for exceptions). Context-dependent case mapping as described in Table 3-14 of the Unicode standard is currently not supported.
Only the ASCII region, i.e. the characters “A” to “Z” and “a” to “z”, are affected. This option cannot be combined with any other option.
Full Unicode case mapping, adapted for Turkic languages (Turkish, Azerbaijani, …). This means that upper case I is mapped to lower case dotless i, and so on.
Currently, just full Unicode case mapping. In the future, full Unicode case mapping adapted for Lithuanian (keeping the dot on the lower case i even if there is an accent on top).
Only available on downcase
and downcase!
. Unicode case folding, which is more far-reaching than Unicode case mapping. This option currently cannot be combined with any other option (i.e. there is currently no variant for turkic languages).
Please note that several assumptions that are valid for ASCII-only case conversions do not hold for more general case conversions. For example, the length of the result may not be the same as the length of the input (neither in characters nor in bytes), some roundtrip assumptions (e.g. str.downcase == str.upcase.downcase) may not apply, and Unicode normalization (i.e. String#unicode_normalize
) is not necessarily maintained by case mapping operations.
Non-ASCII case mapping/folding is currently supported for UTF-8, UTF-16BE/LE, UTF-32BE/LE, and ISO-8859-1~16 Strings/Symbols. This support will be extended to other encodings.
"hEllO".downcase #=> "hello"
Returns a copy of str with uppercase alphabetic characters converted to lowercase and lowercase characters converted to uppercase.
See String#downcase
for meaning of options
and use with different encodings.
"Hello".swapcase #=> "hELLO" "cYbEr_PuNk11".swapcase #=> "CyBeR_pUnK11"
Upcases the contents of str, returning nil
if no changes were made.
See String#downcase
for meaning of options
and use with different encodings.
Downcases the contents of str, returning nil
if no changes were made.
See String#downcase
for meaning of options
and use with different encodings.
Equivalent to String#swapcase
, but modifies the receiver in place, returning str, or nil
if no changes were made.
See String#downcase
for meaning of options
and use with different encodings.
Concatenates the given object(s) to str. If an object is an Integer
, it is considered a codepoint and converted to a character before concatenation.
concat
can take multiple arguments, and all the arguments are concatenated in order.
a = "hello " a.concat("world", 33) #=> "hello world!" a #=> "hello world!" b = "sn" b.concat("_", b, "_", b) #=> "sn_sn_sn"
See also String#<<
, which takes a single argument.
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.html . 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 Symbol
corresponding to str, creating the symbol if it did not previously exist. See Symbol#id2name
.
"Koala".intern #=> :Koala s = 'cat'.to_sym #=> :cat s == :cat #=> true s = '@cat'.to_sym #=> :@cat s == :@cat #=> true
This can also be used to create symbols that cannot be represented using the :xxx
notation.
'cat and dog'.to_sym #=> :"cat and dog"
Centers str
in width
. If width
is greater than the length of str
, returns a new String
of length width
with str
centered and padded with padstr
; otherwise, returns str
.
"hello".center(4) #=> "hello" "hello".center(20) #=> " hello " "hello".center(20, '123') #=> "1231231hello12312312"
Returns a new String
with the last character removed. If the string ends with \r\n
, both characters are removed. Applying chop
to an empty string returns an empty string. String#chomp
is often a safer alternative, as it leaves the string unchanged if it doesn’t end in a record separator.
"string\r\n".chop #=> "string" "string\n\r".chop #=> "string\n" "string\n".chop #=> "string" "string".chop #=> "strin" "x".chop.chop #=> ""
Returns a copy of the receiver with trailing whitespace removed. See also String#lstrip
and String#strip
.
Refer to String#strip
for the definition of whitespace.
" hello ".rstrip #=> " hello" "hello".rstrip #=> "hello"
Processes str as for String#chop
, returning str, or nil
if str is the empty string. See also String#chomp!
.
Removes trailing whitespace from the receiver. Returns the altered receiver, or nil
if no change was made. See also String#lstrip!
and String#strip!
.
Refer to String#strip
for the definition of whitespace.
" hello ".rstrip! #=> " hello" " hello".rstrip! #=> nil "hello".rstrip! #=> nil
Returns 0 if the value is positive, pi otherwise.
Returns 0 if the value is positive, pi otherwise.