Returns self.
Returns self.
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"]
Rounds num
to a given precision in decimal digits (default 0 digits).
Precision may be negative. Returns a floating point number when ndigits
is more than zero.
Numeric
implements this by converting itself 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 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) | | 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) | | 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.) | | (Q_ and Q! is available since Ruby 2.1.) | | 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.) | | (q_ and q! is available since Ruby 2.1.) | | S> L> Q> | Integer | same as the directives without ">" except s> l> q> | | big endian S!> I!> | | (available since Ruby 1.9.3) L!> Q!> | | "S>" is same as "n" s!> i!> | | "L>" is same as "N" l!> q!> | | | | S< L< Q< | Integer | same as the directives without "<" except s< l< q< | | little endian S!< I!< | | (available since Ruby 1.9.3) L!< Q!< | | "S<" is same as "v" s!< i!< | | "L<" is same as "V" l!< q!< | | | | 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
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. The operation is locale insensitive—only characters “A” to “Z” are affected. Note: case replacement is effective only in ASCII region.
"hEllO".downcase #=> "hello"
Downcases the contents of str, returning nil
if no changes were made. Note: case replacement is effective only in ASCII region.
Treats leading characters of str as a string of octal digits (with an optional sign) and returns the corresponding number. Returns 0 if the conversion fails.
"123".oct #=> 83 "-377".oct #=> -255 "bad".oct #=> 0 "0377bad".oct #=> 255
If str
starts with 0
, radix indicators are hornored. See Kernel#Integer
.
Returns true
if str contains the given string or character.
"hello".include? "lo" #=> true "hello".include? "ol" #=> false "hello".include? ?h #=> true
Each other_str
parameter defines a set of characters to count. The intersection of these sets defines the characters to count in str
. Any other_str
that starts with a caret ^
is negated. The sequence c1-c2
means all characters between c1 and c2. The backslash character \
can be used to escape ^
or -
and is otherwise ignored unless it appears at the end of a sequence or the end of a other_str
.
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
provides a unified clone
operation, for REXML::XPathParser
to use across multiple Object
types
Rounds float
to a given precision in decimal digits (default 0 digits).
Precision may be negative. Returns a floating point number when ndigits
is more than zero.
1.4.round #=> 1 1.5.round #=> 2 1.6.round #=> 2 (-1.5).round #=> -2 1.234567.round(2) #=> 1.23 1.234567.round(3) #=> 1.235 1.234567.round(4) #=> 1.2346 1.234567.round(5) #=> 1.23457 34567.89.round(-5) #=> 0 34567.89.round(-4) #=> 30000 34567.89.round(-3) #=> 35000 34567.89.round(-2) #=> 34600 34567.89.round(-1) #=> 34570 34567.89.round(0) #=> 34568 34567.89.round(1) #=> 34567.9 34567.89.round(2) #=> 34567.89 34567.89.round(3) #=> 34567.89
Returns true
if float
is greater than 0.
Returns true
if float
is less than 0.
Returns a string containing a representation of self. As well as a fixed or exponential form of the float
, the call may return NaN
, Infinity
, and -Infinity
.
Return a string describing this Dir
object.
Deletes the named directory. Raises a subclass of SystemCallError
if the directory isn’t empty.
Returns the last access time for the named file as a Time
object).
file_name can be an IO
object.
File.atime("testfile") #=> Wed Apr 09 08:51:48 CDT 2003
Returns the modification time for the named file as a Time
object.
file_name can be an IO
object.
File.mtime("testfile") #=> Tue Apr 08 12:58:04 CDT 2003
Returns the birth time for the named file.
file_name can be an IO
object.
File.birthtime("testfile") #=> Wed Apr 09 08:53:13 CDT 2003
If the platform doesn’t have birthtime, raises NotImplementedError
.
Sets the access and modification times of each named file to the first two arguments. Returns the number of file names in the argument list.