Deletes every element of self
for which the given block evaluates to false
.
See also Array#select!
If no block is given, an Enumerator
is returned instead.
a = %w{ a b c d e f } a.keep_if { |v| v =~ /[aeiou]/ } #=> ["a", "e"]
Returns an array containing the elements in self
corresponding to the given selector
(s).
The selectors may be either integer indices or ranges.
See also Array#select
.
a = %w{ a b c d e f } a.values_at(1, 3, 5) # => ["b", "d", "f"] a.values_at(1, 3, 5, 7) # => ["b", "d", "f", nil] a.values_at(-1, -2, -2, -7) # => ["f", "e", "e", nil] a.values_at(4..6, 3...6) # => ["e", "f", nil, "d", "e", "f"]
Deletes the element at the specified index
, returning that element, or nil
if the index
is out of range.
See also Array#slice!
a = ["ant", "bat", "cat", "dog"] a.delete_at(2) #=> "cat" a #=> ["ant", "bat", "dog"] a.delete_at(99) #=> nil
Deletes every element of self
for which block evaluates to true
.
The array is changed instantly every time the block is called, not after the iteration is over.
See also Array#reject!
If no block is given, an Enumerator
is returned instead.
scores = [ 97, 42, 75 ] scores.delete_if {|score| score < 80 } #=> [97]
When invoked with a block, yield all repeated permutations of length n
of the elements of the array, then return the array itself.
The implementation makes no guarantees about the order in which the repeated permutations are yielded.
If no block is given, an Enumerator
is returned instead.
Examples:
a = [1, 2] a.repeated_permutation(1).to_a #=> [[1], [2]] a.repeated_permutation(2).to_a #=> [[1,1],[1,2],[2,1],[2,2]] a.repeated_permutation(3).to_a #=> [[1,1,1],[1,1,2],[1,2,1],[1,2,2], # [2,1,1],[2,1,2],[2,2,1],[2,2,2]] a.repeated_permutation(0).to_a #=> [[]] # one permutation of length 0
When invoked with a block, yields all repeated combinations of length n
of elements from the array and then returns the array itself.
The implementation makes no guarantees about the order in which the repeated combinations are yielded.
If no block is given, an Enumerator
is returned instead.
Examples:
a = [1, 2, 3] a.repeated_combination(1).to_a #=> [[1], [2], [3]] a.repeated_combination(2).to_a #=> [[1,1],[1,2],[1,3],[2,2],[2,3],[3,3]] a.repeated_combination(3).to_a #=> [[1,1,1],[1,1,2],[1,1,3],[1,2,2],[1,2,3], # [1,3,3],[2,2,2],[2,2,3],[2,3,3],[3,3,3]] a.repeated_combination(4).to_a #=> [[1,1,1,1],[1,1,1,2],[1,1,1,3],[1,1,2,2],[1,1,2,3], # [1,1,3,3],[1,2,2,2],[1,2,2,3],[1,2,3,3],[1,3,3,3], # [2,2,2,2],[2,2,2,3],[2,2,3,3],[2,3,3,3],[3,3,3,3]] a.repeated_combination(0).to_a #=> [[]] # one combination of length 0
Passes elements to the block until the block returns nil
or false
, then stops iterating and returns an array of all prior elements.
If no block is given, an Enumerator
is returned instead.
See also Array#drop_while
a = [1, 2, 3, 4, 5, 0] a.take_while { |i| i < 3 } #=> [1, 2]
Drops elements up to, but not including, the first element for which the block returns nil
or false
and returns an array containing the remaining elements.
If no block is given, an Enumerator
is returned instead.
See also Array#take_while
a = [1, 2, 3, 4, 5, 0] a.drop_while {|i| i < 3 } #=> [3, 4, 5, 0]
Creates a string representation of self
.
[ "a", "b", "c" ].to_s #=> "[\"a\", \"b\", \"c\"]"
Return the list of all array-oriented instance variables.
Returns a new string object containing a copy of str.
The optional encoding keyword argument specifies the encoding of the new string. If not specified, the encoding of str is used (or ASCII-8BIT, if str is not specified).
The optional capacity keyword argument specifies the size of the internal buffer. This may improve performance, when the string will be concatenated many times (causing many realloc calls).
Returns a new directory object for the named directory.
The optional encoding keyword argument specifies the encoding of the directory. If not specified, the filesystem encoding is used.
Opens the file named by filename
according to the given mode
and returns a new File
object.
See IO.new
for a description of mode
and opt
.
If a file is being created, permission bits may be given in perm
. These mode and permission bits are platform dependent; on Unix systems, see open(2) and chmod(2) man pages for details.
The new File
object is buffered mode (or non-sync mode), unless filename
is a tty. See IO#flush
, IO#fsync
, IO#fdatasync
, and IO#sync=
about sync mode.
f = File.new("testfile", "r") f = File.new("newfile", "w+") f = File.new("newfile", File::CREAT|File::TRUNC|File::RDWR, 0644)
Creates a new Enumerator
object, which can be used as an Enumerable
.
In the first form, iteration is defined by the given block, in which a “yielder” object, given as block parameter, can be used to yield a value by calling the yield
method (aliased as +<<+):
fib = Enumerator.new do |y| a = b = 1 loop do y << a a, b = b, a + b end end p fib.take(10) # => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
The optional parameter can be used to specify how to calculate the size in a lazy fashion (see Enumerator#size
). It can either be a value or a callable object.
In the second, deprecated, form, a generated Enumerator
iterates over the given object using the given method with the given arguments passed.
Use of this form is discouraged. Use Kernel#enum_for or Kernel#to_enum instead.
e = Enumerator.new(ObjectSpace, :each_object) #-> ObjectSpace.enum_for(:each_object) e.select { |obj| obj.is_a?(Class) } #=> array of all classes
Construct a new Exception
object, optionally passing in a message.
Create a new SystemExit
exception with the given status and message. Status is true, false, or an integer. If status is not given, true is used.
Construct a new SignalException
object. sig_name
should be a known signal name.
Construct a SyntaxError
exception.
Construct a new NameError
exception. If given the name parameter may subsequently be examined using the NameError.name
method.
Construct a NoMethodError
exception for a method of the given name called with the given arguments. The name may be accessed using the name
method on the resulting object, and the arguments using the args
method.
If errno corresponds to a known system error code, constructs the appropriate Errno
class for that error, otherwise constructs a generic SystemCallError
object. The error number is subsequently available via the errno
method.
Creates a new anonymous module. If a block is given, it is passed the module object, and the block is evaluated in the context of this module like module_eval
.
fred = Module.new do def meth1 "hello" end def meth2 "bye" end end a = "my string" a.extend(fred) #=> "my string" a.meth1 #=> "hello" a.meth2 #=> "bye"
Assign the module to a constant (name starting uppercase) if you want to treat it like a regular module.
Create a new BigDecimal
object.
The initial value, as an Integer
, a Float
, a Rational
, a BigDecimal
, or a String.
If it is a String, spaces are ignored and unrecognized characters terminate the value.
The number of significant digits, as an Integer
. If omitted or 0, the number of significant digits is determined from the initial value.
The actual number of significant digits used in computation is usually larger than the specified number.
TypeError
If the initial
type is neither Integer
, Float
, Rational
, nor BigDecimal
, this exception is raised.
TypeError
If the digits
is not an Integer
, this exception is raised.
ArgumentError
If initial
is a Float
, and the digits
is larger than Float::DIG + 1, this exception is raised.
ArgumentError
If the initial
is a Float
or Rational
, and the digits
value is omitted, this exception is raised.
Creates a date object denoting the given calendar date.
In this class, BCE years are counted astronomically. Thus, the year before the year 1 is the year zero, and the year preceding the year zero is the year -1. The month and the day of month should be a negative or a positive number (as a relative month/day from the end of year/month when negative). They should not be zero.
The last argument should be a Julian day number which denotes the day of calendar reform. Date::ITALY
(2299161=1582-10-15), Date::ENGLAND
(2361222=1752-09-14), Date::GREGORIAN
(the proleptic Gregorian calendar) and Date::JULIAN
(the proleptic Julian calendar) can be specified as a day of calendar reform.
Date.new(2001) #=> #<Date: 2001-01-01 ...> Date.new(2001,2,3) #=> #<Date: 2001-02-03 ...> Date.new(2001,2,-1) #=> #<Date: 2001-02-28 ...>
See also ::jd
.