Yields each entry in this FormData
Raises a MissingTagError
or NotExpectedTagError
if the element is not properly formatted.
Raises NotAvailableValueError
if element content is nil
Returns the element at index
. A negative index counts from the end of self
. Returns nil
if the index is out of range. See also Array#[]
.
a = [ "a", "b", "c", "d", "e" ] a.at(0) #=> "a" a.at(-1) #=> "e"
Appends the elements of +other_ary+s to self
.
[ "a", "b" ].concat( ["c", "d"] ) #=> [ "a", "b", "c", "d" ] [ "a" ].concat( ["b"], ["c", "d"] ) #=> [ "a", "b", "c", "d" ] [ "a" ].concat #=> [ "a" ] a = [ 1, 2, 3 ] a.concat( [ 4, 5 ] ) a #=> [ 1, 2, 3, 4, 5 ] a = [ 1, 2 ] a.concat(a, a) #=> [1, 2, 1, 2, 1, 2]
See also Array#+
.
Calls the given block once for each element in self
, passing that element as a parameter. Returns the array itself.
If no block is given, an Enumerator
is returned.
a = [ "a", "b", "c" ] a.each {|x| print x, " -- " }
produces:
a -- b -- c --
Returns a new array by rotating self
so that the element at count
is the first element of the new array.
If count
is negative then it rotates in the opposite direction, starting from the end of self
where -1
is the last element.
a = [ "a", "b", "c", "d" ] a.rotate #=> ["b", "c", "d", "a"] a #=> ["a", "b", "c", "d"] a.rotate(2) #=> ["c", "d", "a", "b"] a.rotate(-3) #=> ["b", "c", "d", "a"]
Rotates self
in place so that the element at count
comes first, and returns self
.
If count
is negative then it rotates in the opposite direction, starting from the end of the array where -1
is the last element.
a = [ "a", "b", "c", "d" ] a.rotate! #=> ["b", "c", "d", "a"] a #=> ["b", "c", "d", "a"] a.rotate!(2) #=> ["d", "a", "b", "c"] a.rotate!(-3) #=> ["a", "b", "c", "d"]
Invokes the given block once for each element of self
.
Creates a new array containing the values returned by the block.
See also Enumerable#collect
.
If no block is given, an Enumerator
is returned instead.
a = [ "a", "b", "c", "d" ] a.collect { |x| x + "!" } #=> ["a!", "b!", "c!", "d!"] a.map.with_index { |x, i| x * i } #=> ["", "b", "cc", "ddd"] a #=> ["a", "b", "c", "d"]
Invokes the given block once for each element of self
, replacing the element with the value returned by the block.
See also Enumerable#collect
.
If no block is given, an Enumerator
is returned instead.
a = [ "a", "b", "c", "d" ] a.map! {|x| x + "!" } a #=> [ "a!", "b!", "c!", "d!" ] a.collect!.with_index {|x, i| x[0...i] } a #=> ["", "b", "c!", "d!"]
Returns the object in ary with the maximum value. The first form assumes all objects implement Comparable
; the second uses the block to return a <=> b.
a = %w(albatross dog horse) a.max #=> "horse" a.max { |a, b| a.length <=> b.length } #=> "albatross"
If the n
argument is given, maximum n
elements are returned as an array.
a = %w[albatross dog horse] a.max(2) #=> ["horse", "dog"] a.max(2) {|a, b| a.length <=> b.length } #=> ["albatross", "horse"]
Returns a new array that is a one-dimensional flattening of self
(recursively).
That is, for every element that is an array, extract its elements into the new array.
The optional level
argument determines the level of recursion to flatten.
s = [ 1, 2, 3 ] #=> [1, 2, 3] t = [ 4, 5, 6, [7, 8] ] #=> [4, 5, 6, [7, 8]] a = [ s, t, 9, 10 ] #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10] a.flatten #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a = [ 1, 2, [3, [4, 5] ] ] a.flatten(1) #=> [1, 2, 3, [4, 5]]
Flattens self
in place.
Returns nil
if no modifications were made (i.e., the array contains no subarrays.)
The optional level
argument determines the level of recursion to flatten.
a = [ 1, 2, [3, [4, 5] ] ] a.flatten! #=> [1, 2, 3, 4, 5] a.flatten! #=> nil a #=> [1, 2, 3, 4, 5] a = [ 1, 2, [3, [4, 5] ] ] a.flatten!(1) #=> [1, 2, 3, [4, 5]]
When invoked with a block, yield all permutations of length n
of the elements of the array, then return the array itself.
If n
is not specified, yield all permutations of all elements.
The implementation makes no guarantees about the order in which the permutations are yielded.
If no block is given, an Enumerator
is returned instead.
Examples:
a = [1, 2, 3] a.permutation.to_a #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] a.permutation(1).to_a #=> [[1],[2],[3]] a.permutation(2).to_a #=> [[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]] a.permutation(3).to_a #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] a.permutation(0).to_a #=> [[]] # one permutation of length 0 a.permutation(4).to_a #=> [] # no permutations of length 4
When invoked with a block, yields all 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 combinations are yielded.
If no block is given, an Enumerator
is returned instead.
Examples:
a = [1, 2, 3, 4] a.combination(1).to_a #=> [[1],[2],[3],[4]] a.combination(2).to_a #=> [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]] a.combination(3).to_a #=> [[1,2,3],[1,2,4],[1,3,4],[2,3,4]] a.combination(4).to_a #=> [[1,2,3,4]] a.combination(0).to_a #=> [[]] # one combination of length 0 a.combination(5).to_a #=> [] # no combinations of length 5
By using binary search, finds a value from this array which meets the given condition in O(log n) where n is the size of the array.
You can use this method in two use cases: a find-minimum mode and a find-any mode. In either case, the elements of the array must be monotone (or sorted) with respect to the block.
In find-minimum mode (this is a good choice for typical use case), the block must return true or false, and there must be an index i (0 <= i <= ary.size) so that:
the block returns false for any element whose index is less than i, and
the block returns true for any element whose index is greater than or equal to i.
This method returns the i-th element. If i is equal to ary.size, it returns nil.
ary = [0, 4, 7, 10, 12] ary.bsearch {|x| x >= 4 } #=> 4 ary.bsearch {|x| x >= 6 } #=> 7 ary.bsearch {|x| x >= -1 } #=> 0 ary.bsearch {|x| x >= 100 } #=> nil
In find-any mode (this behaves like libc’s bsearch(3)), the block must return a number, and there must be two indices i and j (0 <= i <= j <= ary.size) so that:
the block returns a positive number for ary if 0 <= k < i,
the block returns zero for ary if i <= k < j, and
the block returns a negative number for ary if j <= k < ary.size.
Under this condition, this method returns any element whose index is within i…j. If i is equal to j (i.e., there is no element that satisfies the block), this method returns nil.
ary = [0, 4, 7, 10, 12] # try to find v such that 4 <= v < 8 ary.bsearch {|x| 1 - x / 4 } #=> 4 or 7 # try to find v such that 8 <= v < 10 ary.bsearch {|x| 4 - x / 2 } #=> nil
You must not mix the two modes at a time; the block must always return either true/false, or always return a number. It is undefined which value is actually picked up at each iteration.
The primary interface to this library. Use to setup delegation when defining your class.
class MyClass < DelegateClass(ClassToDelegateTo) # Step 1 def initialize super(obj_of_ClassToDelegateTo) # Step 2 end end
Here’s a sample of use from Tempfile
which is really a File
object with a few special rules about storage location and when the File
should be deleted. That makes for an almost textbook perfect example of how to use delegation.
class Tempfile < DelegateClass(File) # constant and class member data initialization... def initialize(basename, tmpdir=Dir::tmpdir) # build up file path/name in var tmpname... @tmpfile = File.open(tmpname, File::RDWR|File::CREAT|File::EXCL, 0600) # ... super(@tmpfile) # below this point, all methods of File are supported... end # ... end
Returns a string containing the character represented by the int
‘s value according to encoding
.
65.chr #=> "A" 230.chr #=> "\346" 255.chr(Encoding::UTF_8) #=> "\303\277"
Returns the smallest number than or equal to int
in decimal digits (default 0 digits).
Precision may be negative. Returns a floating point number when ndigits
is positive, self
for zero, and truncate up for negative.
1.truncate #=> 1 1.truncate(2) #=> 1.0 15.truncate(-1) #=> 10
Returns the remainder after dividing big by numeric as:
x.remainder(y) means x-y*(x/y).truncate
Examples
5.remainder(3) #=> 2 -5.remainder(3) #=> -2 5.remainder(-3) #=> 2 -5.remainder(-3) #=> -2 -1234567890987654321.remainder(13731) #=> -6966 -1234567890987654321.remainder(13731.24) #=> -9906.22531493148
See Numeric#divmod
.
Returns the absolute value of int
.
-12345.abs #=> 12345 12345.abs #=> 12345 -1234567890987654321.abs #=> 1234567890987654321
Returns self.
Returns 1.
Returns the value as a rational. The optional argument eps is always ignored.
Returns the imaginary part.
Complex(7).imaginary #=> 0 Complex(9, -4).imaginary #=> -4