Returns a string containing a human-readable representation of obj. The default inspect
shows the object’s class name, an encoding of the object id, and a list of the instance variables and their values (by calling inspect
on each of them). User defined classes should override this method to provide a better representation of obj. When overriding this method, it should return a string whose encoding is compatible with the default external encoding.
[ 1, 2, 3..4, 'five' ].inspect #=> "[1, 2, 3..4, \"five\"]" Time.new.inspect #=> "2008-03-08 19:43:39 +0900" class Foo end Foo.new.inspect #=> "#<Foo:0x0300c868>" class Bar def initialize @bar = 1 end end Bar.new.inspect #=> "#<Bar:0x0300c868 @bar=1>"
provides a unified clone
operation, for REXML::XPathParser
to use across multiple Object
types
Iterates the given block int
times, passing in values from zero to int - 1
.
If no block is given, an Enumerator
is returned instead.
5.times {|i| print i, " " } #=> 0 1 2 3 4
Returns int
rounded to the nearest value with a precision of ndigits
decimal digits (default: 0).
When the precision is negative, the returned value is an integer with at least ndigits.abs
trailing zeros.
Returns self
when ndigits
is zero or positive.
1.round #=> 1 1.round(2) #=> 1 15.round(-1) #=> 20 (-15).round(-1) #=> -20
The optional half
keyword argument is available similar to Float#round
.
25.round(-1, half: :up) #=> 30 25.round(-1, half: :down) #=> 20 25.round(-1, half: :even) #=> 20 35.round(-1, half: :up) #=> 40 35.round(-1, half: :down) #=> 30 35.round(-1, half: :even) #=> 40 (-25).round(-1, half: :up) #=> -30 (-25).round(-1, half: :down) #=> -20 (-25).round(-1, half: :even) #=> -20
Returns a string containing the place-value representation of int
with radix base
(between 2 and 36).
12345.to_s #=> "12345" 12345.to_s(2) #=> "11000000111001" 12345.to_s(8) #=> "30071" 12345.to_s(10) #=> "12345" 12345.to_s(16) #=> "3039" 12345.to_s(36) #=> "9ix" 78546939656932.to_s(36) #=> "rubyrules"
Returns a complex object which denotes the given rectangular form.
Complex.rectangular(1, 2) #=> (1+2i)
Returns a complex object which denotes the given rectangular form.
Complex.rectangular(1, 2) #=> (1+2i)
Returns the value as a string for inspection.
Complex(2).inspect #=> "(2+0i)" Complex('-8/6').inspect #=> "((-4/3)+0i)" Complex('1/2i').inspect #=> "(0+(1/2)*i)" Complex(0, Float::INFINITY).inspect #=> "(0+Infinity*i)" Complex(Float::NAN, Float::NAN).inspect #=> "(NaN+NaN*i)"
Always returns the string “nil”.
Returns an array; [num, 0].
Returns self.
Returns self.
Returns the receiver. freeze
cannot be false
.
Returns self
if num
is not zero, nil
otherwise.
This behavior is useful when chaining comparisons:
a = %w( z Bb bB bb BB a aA Aa AA A ) b = a.sort {|a,b| (a.downcase <=> b.downcase).nonzero? || a <=> b } b #=> ["A", "a", "AA", "Aa", "aA", "BB", "Bb", "bB", "bb", "z"]
Returns num
rounded to the nearest value with a precision of ndigits
decimal digits (default: 0).
Numeric
implements this by converting its value to a Float
and invoking Float#round
.
Returns true
if num
is greater than 0.
Returns true
if num
is less than 0.
Decodes str (which may contain binary data) according to the format string, returning an array of each value extracted. The format string consists of a sequence of single-character directives, summarized in the table at the end of this entry. Each directive may be followed by a number, indicating the number of times to repeat with this directive. An asterisk (“*
”) will use up all remaining elements. The directives sSiIlL
may each be followed by an underscore (“_
”) or exclamation mark (“!
”) to use the underlying platform’s native size for the specified type; otherwise, it uses a platform-independent consistent size. Spaces are ignored in the format string. See also String#unpack1
, Array#pack
.
"abc \0\0abc \0\0".unpack('A6Z6') #=> ["abc", "abc "] "abc \0\0".unpack('a3a3') #=> ["abc", " \000\000"] "abc \0abc \0".unpack('Z*Z*') #=> ["abc ", "abc "] "aa".unpack('b8B8') #=> ["10000110", "01100001"] "aaa".unpack('h2H2c') #=> ["16", "61", 97] "\xfe\xff\xfe\xff".unpack('sS') #=> [-2, 65534] "now=20is".unpack('M*') #=> ["now is"] "whole".unpack('xax2aX2aX1aX2a') #=> ["h", "e", "l", "l", "o"]
This table summarizes the various formats and the Ruby classes returned by each.
Integer | | Directive | Returns | Meaning ------------------------------------------------------------------ C | Integer | 8-bit unsigned (unsigned char) S | Integer | 16-bit unsigned, native endian (uint16_t) L | Integer | 32-bit unsigned, native endian (uint32_t) Q | Integer | 64-bit unsigned, native endian (uint64_t) J | Integer | pointer width unsigned, native endian (uintptr_t) | | c | Integer | 8-bit signed (signed char) s | Integer | 16-bit signed, native endian (int16_t) l | Integer | 32-bit signed, native endian (int32_t) q | Integer | 64-bit signed, native endian (int64_t) j | Integer | pointer width signed, native endian (intptr_t) | | S_ S! | Integer | unsigned short, native endian I I_ I! | Integer | unsigned int, native endian L_ L! | Integer | unsigned long, native endian Q_ Q! | Integer | unsigned long long, native endian (ArgumentError | | if the platform has no long long type.) J! | Integer | uintptr_t, native endian (same with J) | | s_ s! | Integer | signed short, native endian i i_ i! | Integer | signed int, native endian l_ l! | Integer | signed long, native endian q_ q! | Integer | signed long long, native endian (ArgumentError | | if the platform has no long long type.) j! | Integer | intptr_t, native endian (same with j) | | S> s> S!> s!> | Integer | same as the directives without ">" except L> l> L!> l!> | | big endian I!> i!> | | Q> q> Q!> q!> | | "S>" is same as "n" J> j> J!> j!> | | "L>" is same as "N" | | S< s< S!< s!< | Integer | same as the directives without "<" except L< l< L!< l!< | | little endian I!< i!< | | Q< q< Q!< q!< | | "S<" is same as "v" J< j< J!< j!< | | "L<" is same as "V" | | n | Integer | 16-bit unsigned, network (big-endian) byte order N | Integer | 32-bit unsigned, network (big-endian) byte order v | Integer | 16-bit unsigned, VAX (little-endian) byte order V | Integer | 32-bit unsigned, VAX (little-endian) byte order | | U | Integer | UTF-8 character w | Integer | BER-compressed integer (see Array.pack) Float | | Directive | Returns | Meaning ----------------------------------------------------------------- D d | Float | double-precision, native format F f | Float | single-precision, native format E | Float | double-precision, little-endian byte order e | Float | single-precision, little-endian byte order G | Float | double-precision, network (big-endian) byte order g | Float | single-precision, network (big-endian) byte order String | | Directive | Returns | Meaning ----------------------------------------------------------------- A | String | arbitrary binary string (remove trailing nulls and ASCII spaces) a | String | arbitrary binary string Z | String | null-terminated string B | String | bit string (MSB first) b | String | bit string (LSB first) H | String | hex string (high nibble first) h | String | hex string (low nibble first) u | String | UU-encoded string M | String | quoted-printable, MIME encoding (see RFC2045) m | String | base64 encoded string (RFC 2045) (default) | | base64 encoded string (RFC 4648) if followed by 0 P | String | pointer to a structure (fixed-length string) p | String | pointer to a null-terminated string Misc. | | Directive | Returns | Meaning ----------------------------------------------------------------- @ | --- | skip to the offset given by the length argument X | --- | skip backward one byte x | --- | skip forward one byte
HISTORY
J, J! j, and j! are available since Ruby 2.3.
Q_, Q!, q_, and q! are available since Ruby 2.1.
I!<, i!<, I!>, and i!> are available since Ruby 1.9.3.
Decodes str (which may contain binary data) according to the format string, returning the first value extracted. See also String#unpack
, Array#pack
.
Returns a printable version of str, surrounded by quote marks, with special characters escaped.
str = "hello" str[3] = "\b" str.inspect #=> "\"hel\\bo\""
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, Aserbaijani,…). 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 currenty 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"
Downcases the contents of str, returning nil
if no changes were made.
See String#downcase
for meaning of options
and use with different encodings.