Ruby array methods

Card Set Information

Author:
payetti
ID:
254173
Filename:
Ruby array methods
Updated:
2013-12-23 18:03:28
Tags:
ruby methods
Folders:
Ruby
Description:
Descriptions of Ruby methods
Show Answers:

Home > Flashcards > Print Preview

The flashcards below were created by user payetti on FreezingBlue Flashcards. What would you like to do?


  1. array.assoc(object)
    Searches through an array whose elements are also arrays comparing obj with the first element of each contained array using obj.==. Returns the first contained array that matches (that is, the first associated array), or nil if no match is found.

    • s1 = [ "colors", "red", "blue", "green" ]
    • s2 = [ "letters", "a", "b", "c" ]
    • s3 = "foo"
    • a = [ s1, s2, s3 ]
    • a.assoc("letters") #=> [ "letters", "a", "b", "c" ]
    • a.assoc("foo") #=> nil
  2. array.clear
    Empties array
  3. array.collect
    Invokes block once for each element of self. Creates a new array containing the values returned by the block.

    • a = [ "a", "b", "c", "d" ]
    • a.collect {|x| x + "!" } #=> ["a!", "b!", "c!", "d!"]
    • a
  4. array.combination
    creates new array with combinations of length n from elements in array

    • 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
  5. array.compact
    • returns array with nil elements removed.
    • [ "a", nil, "b", nil, "c", nil ].compact
    • #=> [ "a", "b", "c" ]
  6. array.concat(array2)
    • appends elements of array2 to array
    • [ "a", "b" ].concat( ["c", "d"] ) #=> [ "a", "b", "c", "d" ]
  7. array.count
    array.count { |item| block } → int
    Returns the number of elements. If an argument is given, counts the number of elements which equals to obj. If a block is given, counts the number of elements yielding a true value.

    • ary = [1, 2, 4, 2]
    • ary.count #=> 4
    • ary.count(2) #=> 2
    • ary.count{|x|x%2==0} #=> 3
  8. array.cycle(n)
    prints elements or array in order n times

    • a = ["a", "b", "c"]
    • a.cycle {|x| puts x } # print, a, b, c, a, b, c,.. forever.
    • a.cycle(2) {|x| puts x } # print, a, b, c, a, b, c.
  9. array.delete(element)
    Deletes items from self that are equal to obj.

    • a = [ "a", "b", "b", "b", "c" ]
    • a.delete("b") #=> "b"
    • a #=> ["a", "c"]
    • a.delete("z") #=> nil
    • a.delete("z") { "not found" } #=> "not found"
  10. array.delete_at(index)
    Deletes the element at the specified index, returning that element, or nil if the index is out of range

    • a = %w( ant bat cat dog )
    • a.delete_at(2) #=> "cat"
    • a #=> ["ant", "bat", "dog"]
    • a.delete_at(99) #=> nil
  11. array.delete_if {|item| block } → ary
    Deletes every element of self for which block evaluates to true. The array is changed instantly every time the block is called and not after the iteration is over.

    • a = [ "a", "b", "c" ]
    • a.delete_if {|x| x >= "b" } #=> ["a"]
  12. array.drop(n)
    deletes first n elements from array.

    • a = [1, 2, 3, 4, 5, 0]
    • a.drop(3) #=> [4, 5, 0]
  13. array.drop_while {|i| block } → new_ary
    Drops elements up to, but not including, the first element for which the block returns nilor false and returns an array containing the remaining elements.

    • a = [1, 2, 3, 4, 5, 0]
    • a.drop_while {|i| i < 3 } #=> [3, 4, 5, 0]
  14. array.fill(object)
    Replaces the elements of array with the object.

    • a = [ "a", "b", "c", "d" ]
    • a.fill("x") #=> ["x", "x", "x", "x"]
    • a.fill("z", 2, 2) #=> ["x", "x", "z", "z"]
    • a.fill("y", 0..1) #=> ["y", "y", "z", "z"]
    • a.fill {|i| i*i} #=> [0, 1, 4, 9]
    • a.fill(-2) {|i| i*i*i} #=> [0, 1, 8, 27]
  15. array.index(object)
    Returns the index of the first object in self such that the object is == to obj.

    • a = [ "a", "b", "c" ]
    • a.index("b") #=> 1
    • a.index("z") #=> nil
  16. array.flatten
    array.flatten(level)
    Returns a new array that is a one-dimensional flattening of this array (recursively). That is, for every element that is an array, extract its elements into the new array. If 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]
  17. array.replace(other_array)
    Replaces the contents of array with the contents of other_array

    • a = [ "a", "b", "c", "d", "e" ]
    • a.replace([ "x", "y", "z" ]) #=> ["x", "y", "z"]
    • a #=> ["x", "y", "z"]
  18. array.insert(index, object)
    Inserts object after the given index

    • a = %w{ a b c d }
    • a.insert(2, 99) #=> ["a", "b", 99, "c", "d"]
    • a.insert(-2, 1, 2, 3) #=> ["a", "b", 99, "c", 1, 2, 3, "d"]
  19. array.join(sep)
    Returns a string by converting each element of the array into a string, separated by sep

    • [ "a", "b", "c" ].join #=> "abc"
    • [ "a", "b", "c" ].join("-") #=> "a-b-c"
  20. array.keep_if {|item| block }
    Deletes every element of array for which block is false.

    • a = %w{ a b c d e f }
    • a.keep_if {|v| v =~ /[aeiou]/} #=> ["a", "e"]
  21. array.map {|item| block }
    Either returns a new array or (with .map!) mutates array in place. (collect is a synonym).

    • a = [ "a", "b", "c", "d" ]
    • a.collect {|x| x + "!" } #=> ["a!", "b!", "c!", "d!"]
    • a #=> ["a", "b", "c", "d"]
  22. array.permutation(n)
    Returns all permutations of length n

    • 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
  23. array.pop
    Removes last element of array. With optional argument can remove last n elements of array

What would you like to do?

Home > Flashcards > Print Preview