Returns random elements from self
.
When no arguments are given, returns a random element from self
:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a.sample # => 3 a.sample # => 8
If self
is empty, returns nil
.
When argument n
is given, returns a new Array
containing n
random elements from self
:
a.sample(3) # => [8, 9, 2] a.sample(6) # => [9, 6, 10, 3, 1, 4]
Returns no more than a.size
elements (because no new duplicates are introduced):
a.sample(a.size * 2) # => [6, 4, 1, 8, 5, 9, 10, 2, 3, 7]
But self
may contain duplicates:
a = [1, 1, 1, 2, 2, 3] a.sample(a.size * 2) # => [1, 1, 3, 2, 1, 2]
The argument n
must be a non-negative numeric value. The order of the result array is unrelated to the order of self
. Returns a new empty Array
if self
is empty.
The optional random
argument will be used as the random number generator:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a.sample(random: Random.new(1)) #=> 6 a.sample(4, random: Random.new(1)) #=> [6, 10, 9, 2]
Formats each element in self
into a binary string; returns that string. See Packed Data.
Returns a Hash
containing implementation-dependent counters inside the VM.
This hash includes information about method/constant caches:
{ :constant_cache_invalidations=>2, :constant_cache_misses=>14, :global_cvar_state=>27 }
If USE_DEBUG_COUNTER
is enabled, debug counters will be included.
The contents of the hash are implementation specific and may be changed in the future.
This method is only expected to work on C Ruby.
Returns self
truncated (toward zero) to a precision of ndigits
decimal digits.
When ndigits
is negative, the returned value has at least ndigits.abs
trailing zeros:
555.truncate(-1) # => 550 555.truncate(-2) # => 500 -555.truncate(-2) # => -500
Returns self
when ndigits
is zero or positive.
555.truncate # => 555 555.truncate(50) # => 555
Related: Integer#round
.
Calls the given block self
times with each integer in (0..self-1)
:
a = [] 5.times {|i| a.push(i) } # => 5 a # => [0, 1, 2, 3, 4]
With no block given, returns an Enumerator
.
Returns the imaginary value for self
:
Complex.rect(7).imag # => 0 Complex.rect(9, -4).imag # => -4
If self
was created with polar coordinates, the returned value is computed, and may be inexact:
Complex.polar(1, Math::PI/4).imag # => 0.7071067811865476 # Square root of 2.
Returns the conjugate of self
, Complex.rect(self.imag, self.real)
:
Complex.rect(1, 2).conj # => (1-2i)
Returns self
truncated (toward zero) to a precision of digits
decimal digits.
Numeric implements this by converting self
to a Float
and invoking Float#truncate
.
Returns true
if self
is less than 0, false
otherwise.
Returns zero.
Returns self
.
Extracts data from self
, forming objects that become the elements of a new array; returns that array. See Packed Data.
Like String#unpack
, but unpacks and returns only the first extracted object. See Packed Data.
Returns the count of characters (not bytes) in self
:
'foo'.length # => 3 'тест'.length # => 4 'こんにちは'.length # => 5
Contrast with String#bytesize
:
'foo'.bytesize # => 3 'тест'.bytesize # => 8 'こんにちは'.bytesize # => 15
Returns a MatchData
object (or nil
) based on self
and the given pattern
.
Note: also updates Global Variables at Regexp
.
Computes regexp
by converting pattern
(if not already a Regexp
).
regexp = Regexp.new(pattern)
Computes matchdata
, which will be either a MatchData
object or nil
(see Regexp#match
):
matchdata = <tt>regexp.match(self)
With no block given, returns the computed matchdata
:
'foo'.match('f') # => #<MatchData "f"> 'foo'.match('o') # => #<MatchData "o"> 'foo'.match('x') # => nil
If Integer
argument offset
is given, the search begins at index offset
:
'foo'.match('f', 1) # => nil 'foo'.match('o', 1) # => #<MatchData "o">
With a block given, calls the block with the computed matchdata
and returns the block’s return value:
'foo'.match(/o/) {|matchdata| matchdata } # => #<MatchData "o"> 'foo'.match(/x/) {|matchdata| matchdata } # => nil 'foo'.match(/f/, 1) {|matchdata| matchdata } # => nil
Returns true
or false
based on whether a match is found for self
and pattern
.
Note: does not update Global Variables at Regexp
.
Computes regexp
by converting pattern
(if not already a Regexp
).
regexp = Regexp.new(pattern)
Returns true
if self+.match(regexp)
returns a MatchData
object, false
otherwise:
'foo'.match?(/o/) # => true 'foo'.match?('o') # => true 'foo'.match?(/x/) # => false
If Integer
argument offset
is given, the search begins at index offset
:
'foo'.match?('f', 1) # => false 'foo'.match?('o', 1) # => true
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.
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 self
truncated (toward zero) to a precision of ndigits
decimal digits.
When ndigits
is positive, returns a float with ndigits
digits after the decimal point (as available):
f = 12345.6789 f.truncate(1) # => 12345.6 f.truncate(3) # => 12345.678 f = -12345.6789 f.truncate(1) # => -12345.6 f.truncate(3) # => -12345.678
When ndigits
is negative, returns an integer with at least ndigits.abs
trailing zeros:
f = 12345.6789 f.truncate(0) # => 12345 f.truncate(-3) # => 12000 f = -12345.6789 f.truncate(0) # => -12345 f.truncate(-3) # => -12000
Note that the limited precision of floating-point arithmetic may lead to surprising results:
(0.3 / 0.1).truncate #=> 2 (!)
Related: Float#round
.
Returns true
if self
is a NaN, false
otherwise.
f = -1.0 #=> -1.0 f.nan? #=> false f = 0.0/0.0 #=> NaN f.nan? #=> true
Returns true
if self
is less than 0, false
otherwise.
Resumes the fiber from the point at which the last Fiber.yield
was called, or starts running it if it is the first call to resume
. Arguments passed to resume will be the value of the Fiber.yield
expression or will be passed as block parameters to the fiber’s block if this is the first resume
.
Alternatively, when resume is called it evaluates to the arguments passed to the next Fiber.yield
statement inside the fiber’s block or to the block value if it runs to completion without any Fiber.yield
Returns the home directory path of the user specified with user_name
if it is not nil
, or the current login user:
Dir.home # => "/home/me" Dir.home('root') # => "/root"
Raises ArgumentError
if user_name
is not a user name.
Returns true if path
matches against pattern
. The pattern is not a regular expression; instead it follows rules similar to shell filename globbing. It may contain the following metacharacters:
*
Matches any file. Can be restricted by other values in the glob. Equivalent to /.*/x
in regexp.
*
Matches all regular files
c*
Matches all files beginning with c
*c
Matches all files ending with c
*c*
Matches all files that have c
in them (including at the beginning or end).
To match hidden files (that start with a .
) set the File::FNM_DOTMATCH flag.
**
Matches directories recursively or files expansively.
?
Matches any one character. Equivalent to /.{1}/
in regexp.
[set]
Matches any one character in set
. Behaves exactly like character sets in Regexp
, including set negation ([^a-z]
).
\
Escapes the next metacharacter.
{a,b}
Matches pattern a and pattern b if File::FNM_EXTGLOB flag is enabled. Behaves like a Regexp
union ((?:a|b)
).
flags
is a bitwise OR of the FNM_XXX
constants. The same glob pattern and flags are used by Dir::glob
.
Examples:
File.fnmatch('cat', 'cat') #=> true # match entire string File.fnmatch('cat', 'category') #=> false # only match partial string File.fnmatch('c{at,ub}s', 'cats') #=> false # { } isn't supported by default File.fnmatch('c{at,ub}s', 'cats', File::FNM_EXTGLOB) #=> true # { } is supported on FNM_EXTGLOB File.fnmatch('c?t', 'cat') #=> true # '?' match only 1 character File.fnmatch('c??t', 'cat') #=> false # ditto File.fnmatch('c*', 'cats') #=> true # '*' match 0 or more characters File.fnmatch('c*t', 'c/a/b/t') #=> true # ditto File.fnmatch('ca[a-z]', 'cat') #=> true # inclusive bracket expression File.fnmatch('ca[^t]', 'cat') #=> false # exclusive bracket expression ('^' or '!') File.fnmatch('cat', 'CAT') #=> false # case sensitive File.fnmatch('cat', 'CAT', File::FNM_CASEFOLD) #=> true # case insensitive File.fnmatch('cat', 'CAT', File::FNM_SYSCASE) #=> true or false # depends on the system default File.fnmatch('?', '/', File::FNM_PATHNAME) #=> false # wildcard doesn't match '/' on FNM_PATHNAME File.fnmatch('*', '/', File::FNM_PATHNAME) #=> false # ditto File.fnmatch('[/]', '/', File::FNM_PATHNAME) #=> false # ditto File.fnmatch('\?', '?') #=> true # escaped wildcard becomes ordinary File.fnmatch('\a', 'a') #=> true # escaped ordinary remains ordinary File.fnmatch('\a', '\a', File::FNM_NOESCAPE) #=> true # FNM_NOESCAPE makes '\' ordinary File.fnmatch('[\?]', '?') #=> true # can escape inside bracket expression File.fnmatch('*', '.profile') #=> false # wildcard doesn't match leading File.fnmatch('*', '.profile', File::FNM_DOTMATCH) #=> true # period by default. File.fnmatch('.*', '.profile') #=> true File.fnmatch('**/*.rb', 'main.rb') #=> false File.fnmatch('**/*.rb', './main.rb') #=> false File.fnmatch('**/*.rb', 'lib/song.rb') #=> true File.fnmatch('**.rb', 'main.rb') #=> true File.fnmatch('**.rb', './main.rb') #=> false File.fnmatch('**.rb', 'lib/song.rb') #=> true File.fnmatch('*', 'dave/.profile') #=> true File.fnmatch('**/foo', 'a/b/c/foo', File::FNM_PATHNAME) #=> true File.fnmatch('**/foo', '/a/b/c/foo', File::FNM_PATHNAME) #=> true File.fnmatch('**/foo', 'c:/a/b/c/foo', File::FNM_PATHNAME) #=> true File.fnmatch('**/foo', 'a/.b/c/foo', File::FNM_PATHNAME) #=> false File.fnmatch('**/foo', 'a/.b/c/foo', File::FNM_PATHNAME | File::FNM_DOTMATCH) #=> true