you are in: codestackercodes [RSS] → tag: array [RSS]

array guide Delicious

source: http://blog.carlosgabaldon.com/calabro/blog/post/2008/04/08/Ruby_Arrays

show/hide lines
   1  # Creating a new array
   2  
   3  array = Array.new
   4  # or
   5  array = []
   6  
   7  # Adding array elements By position
   8  
   9  array = []
  10  array[0] = "first element"
  11  
  12  # To the end
  13  
  14  array << "last element"
  15  # This adds "last element" to the end of the array or
  16  
  17  array.push("last element")
  18  # This adds "last element" to the end of the array
  19  
  20  # To the beginning
  21  
  22  array = ["first element", "second element"]
  23  array.unshift("before first element")
  24  
  25  # This adds "before first element" to the beginning of the array
  26  # Retrieving array elements
  27  
  28  # By position
  29  
  30  array = ["first element", "second element", "third element"]
  31  third_element = array[2]
  32  # This returns the third element.
  33  # By positions
  34  
  35  # second_and_third_element = array[1, 2]
  36  # This returns a new array that contains the second & third elements
  37  # Removing array elements
  38  
  39  # From the beginning
  40  
  41  array = ["first element", "second element"]
  42  first_element = array.shift
  43  # This removes "first element" from the beginning of the array
  44  # From the end
  45  
  46  last_element = array.pop
  47  # This removes "second element" or the last element from the end of the array
  48  
  49  # Combining arrays
  50  
  51  # To a new array
  52  array = [1, 2, 3]
  53  array.concat([4, 5, 6, 7]) # [1, 2, 3, 4, 5, 6, 7]
  54  # or
  55  new_array = array + [4, 5, 6]
  56  # Using the "+" method returns a new array, but does not modify the original array
  57  
  58  # Modifying the array
  59  
  60  array.replace([4, 5, 6]) # [4, 5, 6]
  61  # Pairing items
  62  
  63  array = [1, 2, 3]
  64  new_paired_array = array.zip([4, 5, 6]) # [[1,4],[2, 5],[3, 6]]
  65  # This creates an array of arrays
  66  
  67  # Flattening array of arrays
  68  
  69  array = [1, 2, 3]
  70  new_paired_array_flattened = array.zip([4, 5, 6]).flatten # [1, 4, 2, 5, 3, 6]
  71  
  72  # This flattens the arrays of arrays into one Array.
  73  
  74  # Collecting array elements
  75  
  76  array = [1, 2, 3]
  77  array.collect {|x| x * 2 } # [2, 4, 6]
  78  
  79  # Invokes block once for each element of self. Creates a new array containing the values returned by the block.
  80  
  81  # Iterating over arrays
  82  
  83  [1, 2, 3, 4] .each {|x| puts x}
  84  # 1
  85  # 2
  86  # 3
  87  # 4
  88  # Filtering arrays
  89  
  90  [1, 2, 3, 4, 5, 6] .find {|x| puts x > 5}
  91  # This returns the first element that matches the block criteria.
  92  
  93  # Querying arrays
  94  
  95  array.find_all {|item| criteria }
  96  # This returns a new array containing all the elements of the array that match the block criteria.
  97  
  98  array.size
  99  # This returns the number of elements in the array.
 100  
 101  array.empty?
 102  # This returns true if the array is empty; false otherwise.
 103  
 104  array.include?(item)
 105  # This returns true if the array contains the item; false otherwise.
 106  
 107  array.any? {|item| criteria }
 108  # This returns true if any item in the array matches the block criteria; false otherwise.
 109  
 110  array.all? {|item| criteria }
 111  # This returns true if all items in the array match the block criteria; false otherwise
created by leozera — 22 October 2008 — get a short url — tags: array ruby embed

ruby array sum sample Delicious

simple sample

show/hide lines
   1  # an array with numbers
   2  orders = %w(10 11.5 1.5 24 100) 
   3  
   4  # convert array to float (originally in string format)
   5  orders.map!{ |order| order.to_f } 
   6  
   7  total = orders.inject() {|result, element| result + element}
created by leozera — 17 October 2008 — get a short url — tags: array ruby embed

Array como objeto Delicious

Como criar um array usando ArrayObject e podendo acessar os valores como propriedade de um objeto

show/hide lines
   1  // acessando somente como array
   2  $array = new ArrayObject(array('name' => 'Rafael Souza', 'username' => 'rafaelss'));
   3  echo 'Nome: ', $array['name'], "\n";
   4  echo 'Usuário: ', $array['username'], "\n";
   5  
   6  // acessando como objeto
   7  $array = new ArrayObject(array('name' => 'Rafael Souza', 'username' => 'rafaelss'), ArrayObject::ARRAY_AS_PROPS);
   8  echo 'Nome: ', $array->name, "\n";
   9  echo 'Usuário: ', $array->username, "\n";
  10  
  11  // acessando de qualquer jeito
  12  $array = new ArrayObject(array('name' => 'Rafael Souza', 'username' => 'rafaelss'), ArrayObject::ARRAY_AS_PROPS | ArrayObject::STD_PROP_LIST);
  13  echo 'Nome: ', $array['name'], "\n";
  14  echo 'Usuário: ', $array['username'], "\n";
  15  
  16  echo 'Nome: ', $array->name, "\n";
  17  echo 'Usuário: ', $array->username, "\n";
created by rafaess — 14 July 2008 — get a short url — tags: array objeto embed