Ruby Method of the Day - Array.slice!
Signature
array.slice!(position) #=> object or nil
array.slice!(position) removes the object at position from array and
returns the removed object. If position is outside of array then nil is
returned. If position is 0 or positive then start counting from the
beginning of the array. If position is negative then start counting from the
end of the array.
array.slice!(position, count) #=> new_array or nil
array.slice!(position, count) removes count number of
objects, starting at position, from array and returns new_array
containing the objects that were removed from the array. If position is
outside of array then nil is returned. count must be either 0 or a
positive integer or an IndexError is thrown. If position is 0 or positive
then start counting from the beginning of the array. If position is negative
then start counting from the end of the array.
array.slice!(range) #=> new_array or nil
array.slice!(range) removes all objects at the positions
specified by range from array and returns new_array containing the
objects that were removed from the array. nil is returned if the start of
the range is positive and outside of the array. RangeError is thrown if the
start of the range is negative and outside of the array. If the start of the
range is 0 or positive then start counting from the beginning of the array. If
the start of the range is negative then start counting from the end of the
array.
Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
a = [1, 2, "foo", 3, "bar", 4, -9] b = a.clone; b.slice!(0) #=> 1 b #=> [2, "foo", 3, "bar", 4, -9] b = a.clone; b.slice!(-1) #=> -9 b #=> [1, 2, "foo", 3, "bar", 4] b = a.clone; b.slice!(20) #=> nil b #=> [1, 2, "foo", 3, "bar", 4, -9] b = a.clone; b.slice!(-20) #=> nil b #=> [1, 2, "foo", 3, "bar", 4, -9] b = a.clone; b.slice!(1,3) #=> [2, "foo", 3] b #=> [1, "bar", 4, -9] begin b = a.clone; b.slice!(1,-3) rescue Exception => e e.inspect #=> "#<IndexError: negative length (-3)>" end b = a.clone; b.slice!(-2,3) #=> [4, -9] b #=> [1, 2, "foo", 3, "bar"] b = a.clone; b.slice!(-3,3) #=> ["bar", 4, -9] b #=> [1, 2, "foo", 3] b = a.clone; b.slice!(-8,2) #=> nil b #=> [1, 2, "foo", 3, "bar", 4] b = a.clone; b.slice!(3..5) #=> [3, "bar", 4] b #=> [1, 2, "foo", -9] b = a.clone; b.slice!(0..99) #=> [1, 2, "foo", 3, "bar", 4, -9] b #=> [] b = a.clone; b.slice!(2..5) #=> ["foo", 3, "bar", 4] b #=> [1, 2, -9] b = a.clone; b.slice!(2..-4) #=> ["foo", 3] b #=> [1, 2, "bar", 4, -9] b = a.clone; b.slice!(2..-99) #=> [] b #=> [1, 2, "foo", 3, "bar", 4, -9] b = a.clone; b.slice!(-5..-2) #=> ["foo", 3, "bar", 4] b #=> [1, 2, -9] begin b = a.clone; b.slice!(-99..-1) rescue Exception => e e.inspect #=> "#<RangeError: -99..-1 out of range>" end |
Documentation Reference
Ruby version 1.8.6
Ruby Method of the Day - Array.[]=
Signature
Array.[]= assigns/replaces elements in an array and is the counter method
to
Array.[].
array[position] = object #=> object
array[position] = object replaces the object at position with
object and returns object. If position is negative and outside of
array, then an IndexError exception is thrown. If position is positive
and outside of array, then array is padded with
(position - array.length) number of nil objects and object
is placed at position.
array[position, count] = object #=> object array[position, count] = other_array #=> other_array
array[position, count] = object replaces count number of
objects, starting at position, with object. Similarly,
array[position, count] = other_array replaces count number of
objects, starting at position, with all of the objects in other_array.
count must be either 0 or a positive integer or an IndexError is thrown.
If position is negative and outside of array, then an IndexError
exception is thrown. If position is positive and outside of array, then
array is padded with (position - array.length) number of
nil objects and object is placed at position.
array[range] = object #=> object array[range] = other_array #=> other_array
array[range] = object replaces all objects at the positions
specified by range with object. Similarly, array[range] =
other_array replaces all objects at the positions specified by range
with all of the objects in other_array. If the start of the range is 0 or
positive then start counting from the beginning of the array. If the start of
the range is negative then start counting from the end of the string.
RangeError is thrown if the start of the range is negative and is outside of
the array.
Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
a = [1, 2, "foo", 3, "bar", 4, -9] b = a.clone; b[0] = nil; b #=> [nil, 2, "foo", 3, "bar", 4, -9] b = a.clone; b[0] = [5,4,3,2]; b #=> [[5, 4, 3, 2], 2, "foo", 3, "bar", 4, -9] b = a.clone; b[1] = nil; b #=> [1, nil, "foo", 3, "bar", 4, -9] b = a.clone; b[-1] = nil; b #=> [1, 2, "foo", 3, "bar", 4, nil] b = a.clone b.length #=> 7 # Scroll to right! ===> b[20] = nil; b #=> [1, 2, "foo", 3, "bar", 4, -9, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil] b.length #=> 21 begin b = a.clone b[-20] = nil rescue Exception => e e.inspect #=> "#<IndexError: index -20 out of array>" end b = a.clone; b[2,3] = 'xxxx'; b #=> [1, 2, "xxxx", 4, -9] b = a.clone; b[2,3] = %w{x x x}; b #=> [1, 2, "x", "x", "x", 4, -9] b = a.clone; b[2,4] = [5,4,3,2]; b #=> [1, 2, 5, 4, 3, 2, -9] b = a.clone; b[2,4] = [5,4,3,2,1,0,-1,-2,-3]; b #=> [1, 2, 5, 4, 3, 2, 1, 0, -1, -2, -3, -9] begin b = a.clone b[2,-3] = 'xxxx' rescue Exception => e e.inspect #=> "#<IndexError: negative length (-3)>" end b = a.clone; b[7,2] = 'xxxx'; b #=> [1, 2, "foo", 3, "bar", 4, -9, "xxxx"] b = a.clone; b[1,0] = 'xxxx'; b #=> [1, "xxxx", 2, "foo", 3, "bar", 4, -9] b = a.clone; b[-2,3] = 'xxxx'; b #=> [1, 2, "foo", 3, "bar", "xxxx"] b = a.clone; b[-3,3] = 'xxxx'; b #=> [1, 2, "foo", 3, "xxxx"] begin b = a.clone b[-8,2] = 'xxxx' rescue Exception => e e.inspect #=> "#<IndexError: index -8 out of array>" end # Scroll to right! ===> b = a.clone; b[99,5] = 'xxxx'; b #=> [1, 2, "foo", 3, "bar", 4, -9, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, "xxxx"] b.length #=> 100 b = a.clone; b[0..99] = 'xxxx'; b #=> ["xxxx"] b = a.clone; b[2..5] = 'xxxx'; b #=> [1, 2, "xxxx", -9] b = a.clone; b[2..5] = Array.new(4); b #=> [1, 2, nil, nil, nil, nil, -9] b = a.clone; b[2...5] = 'xxxx'; b #=> [1, 2, "xxxx", 4, -9] b = a.clone; b[2..-4] = 'xxxx'; b #=> [1, 2, "xxxx", "bar", 4, -9] b = a.clone; b[2..-99] = 'xxxx'; b #=> [1, 2, "xxxx", "foo", 3, "bar", 4, -9] b = a.clone; b[7..10] = 'xxxx'; b #=> [1, 2, "foo", 3, "bar", 4, -9, "xxxx"] b = a.clone; b[0..0] = 'xxxx'; b #=> ["xxxx", 2, "foo", 3, "bar", 4, -9] b = a.clone; b[-5..-2] = 'xxxx'; b #=> [1, 2, "xxxx", -9] b = a.clone; b[-5..-1] = 'xxxx'; b #=> [1, 2, "xxxx"] b = a.clone; b[-5..-6] = 'xxxx'; b #=> [1, 2, "xxxx", "foo", 3, "bar", 4, -9] begin b = a.clone b[-99..-1] = 'xxxx' rescue Exception => e e.inspect #=> "#<RangeError: -99..-1 out of range>" end |
Documentation Reference
Ruby version 1.8.6
Ruby Method of the Day - Array.[] and Array.slice
Signature
array[position] #=> object or nil array.slice(position) #=> object or nil
array[position] and array.slice(position) returns the object at
position or returns nil if position is outside of array. If position
is 0 or positive, then start counting from the beginning of the array. If
position is negative, then start counting from the end of the array.
array[position, count] #=> new_array or nil array.slice(position, count) #=> new_array or nil
array[position,count] and array.slice(position,count) returns
new_array containing count number of objects starting from position.
count must be a positive integer or nil is returned. If count is 0 then
an empty array is returned (and not to mention executing useless method call).
If position is positive and outside of array then an empty array is
returned. If position is 0 or positive, then start counting from the
beginning of the array. If position is negative, then start counting from
the end of the array. If position is negative and outside of array then
nil is returned.
array[range] #=> new_array or nil array.slice(range) #=> new_array or nil
array[range] and array.slice(range) returns new_array containing each
object for all positions specified by range. If the start of the range is 0
or positive then start counting from the beginning of the array. If the start
of the range is negative then start counting from the end of the array. If the
start of the range is negative and outside of array then nil is
returned.
Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
a = [1, 2, "foo", 3, "bar", 4, -9] a[0] #=> 1 a[1] #=> 2 a[-1] #=> -9 a[20] #=> nil a[-20] #=> nil a[2,3] #=> ["foo", 3, "bar"] a[2,-3] #=> nil a[7,2] #=> [] a[1,0] #=> [] a[-2,3] #=> [4, -9] a[-3,3] #=> ["bar", 4, -9] a[-8,2] #=> nil a[99, 5] #=> nil a[-99, 5] #=> nil a[0..99] #=> [1, 2, "foo", 3, "bar", 4, -9] a[2..5] #=> ["foo", 3, "bar", 4] a[2..-4] #=> ["foo", 3] a[2..-99] #=> [] a[7..10] #=> [] a[0..0] #=> [1] a[-5..-2] #=> ["foo", 3, "bar", 4] a[-5..-1] #=> ["foo", 3, "bar", 4, -9] a[-5..-6] #=> [] a[-99..-1] #=> nil a[-99..0] #=> nil a[-99..1] #=> nil a.slice(0) #=> 1 a.slice(1) #=> 2 a.slice(-1) #=> -9 a.slice(20) #=> nil a.slice(-20) #=> nil a.slice(2,3) #=> ["foo", 3, "bar"] a.slice(2,-3) #=> nil a.slice(7,2) #=> [] a.slice(1,0) #=> [] a.slice(-2,3) #=> [4, -9] a.slice(-3,3) #=> ["bar", 4, -9] a.slice(-8,2) #=> nil a.slice(99, 5) #=> nil a.slice(-99, 5) #=> nil a.slice(0..99) #=> [1, 2, "foo", 3, "bar", 4, -9] a.slice(2..5) #=> ["foo", 3, "bar", 4] a.slice(2..-4) #=> ["foo", 3] a.slice(2..-99) #=> [] a.slice(7..10) #=> [] a.slice(0..0) #=> [1] a.slice(-5..-2) #=> ["foo", 3, "bar", 4] a.slice(-5..-1) #=> ["foo", 3, "bar", 4, -9] a.slice(-5..-6) #=> [] a.slice(-99..-1) #=> nil a.slice(-99..0) #=> nil a.slice(-99..1) #=> nil |
Documentation Reference
Ruby version 1.8.6
Ruby Method of the Day - Array.<< and Array.push
Signature
array << object #=> array array.push(object1, ...) #=> array
array << object appends object to the end of array and
returns array (thus allowing the chaining of multiple calls to <<).
array.push(object1, ...) appends all arguments to the end of
array and returns array.
Examples
1 2 3 4 5 6 7 8 9 10 11 12 |
a = [1] #=> [1] a << 2 #=> [1, 2] a << 3 << 4 #=> [1, 2, 3, 4] a << [5, 6] #=> [1, 2, 3, 4, [5, 6]] b = [] b << 1 << "foo" << 1.34534 << :bar #=> [1, "foo", 1.34534, :bar] a = [1] #=> [1] a.push(2) #=> [1, 2] a.push(3,4) #=> [1, 2, 3, 4] a.push([5,6]) #=> [1, 2, 3, 4, [5, 6]] |
Documentation Reference
Ruby version 1.8.6
Ruby Method of the Day - Array.-
Signature
array - other_array #=> new_array
array - other_array returns new_array that contains the
difference of the
two arrays. In other words, new_array contains the elements that are in
array but not in other_array
Examples
1 2 3 |
a = [1,2,3,4,5,6,7,8,8,9,10] #=> [1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10] a - [1, 8] #=> [2, 3, 4, 5, 6, 7, 9, 10] a - [-1, 22, 4, 9] #=> [1, 2, 3, 5, 6, 7, 8, 8, 10] |
Documentation Reference
Ruby version 1.8.6
Ruby Method of the Day - Array.+
Signature
array + other_array #=> new_array
array + other_array returns new_array that contains the
concatenation of array and other_array.
Examples
1 2 |
[1,2,3,4,5] + [5,6,7,8,9] #=> [1, 2, 3, 4, 5, 5, 6, 7, 8, 9] [5,6,7,8,9] + [1,2,3,4,5] #=> [5, 6, 7, 8, 9, 1, 2, 3, 4, 5] |
Documentation Reference
Ruby version 1.8.6
Ruby Method of the Day - Array.&
Signature
array & other_array #=> new_array
array & other_array returns new_array which contains the
intersections of the two arrays.
Only unique intersections will be returned. In other words, if array and/or
other_array contain duplicate elements, new_array will not contain
duplicate intersections.
Examples
1 2 3 4 5 |
a = [1,2,3,4,5,6,7,8,8,9,10] #=> [1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10] a & [0,2,4,9,11] #=> [2, 4, 9] a & [0,11,22] #=> [] a & [0,8,8,7,6,-3] #=> [6, 7, 8] a & [0,8,8,7,6,-3] & [1,2,3,4,5,6] #=> [6] |
Documentation Reference
Ruby version 1.8.6
Ruby Method of the Day - Float.truncate
Signature
float.truncate #=> integer float.to_i #=> integer
float.truncate and float.to_i returns the integer portion of float.
Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
(1.99).truncate #=> 1 (1.5).truncate #=> 1 (1.0).truncate #=> 1 (0.5).truncate #=> 0 (-0.5).truncate #=> 0 (-1.0).truncate #=> -1 (-1.5).truncate #=> -1 (-1.99).truncate #=> -1 (1.99).to_i #=> 1 (1.5).to_i #=> 1 (1.0).to_i #=> 1 (0.5).to_i #=> 0 (-0.5).to_i #=> 0 (-1.0).to_i #=> -1 (-1.5).to_i #=> -1 (-1.99).to_i #=> -1 |
Documentation Reference
Ruby version 1.8.6
Ruby Method of the Day - Float.ceil
Signature
float.ceil #=> integer
float.ceil returns that first integer that is greater than or equal to float.
Examples
1 2 3 4 5 6 7 8 9 10 11 |
(2.9).ceil #=> 3 (2.0).ceil #=> 2 (1.9).ceil #=> 2 (1.0).ceil #=> 1 (0.1).ceil #=> 1 (0.0).ceil #=> 0 (-0.1).ceil #=> 0 (-1.0).ceil #=> -1 (-1.9).ceil #=> -1 (-2.0).ceil #=> -2 (-2.9).ceil #=> -2 |
Documentation Reference
Ruby version 1.8.6
