COLLECTIONS & ITERATORS
      WITH HASHES AND ARRAYS




            Sarah Allen
ITERATORS: CONDITIONAL
          LOOPING
• “while” allows us to loop through code while a set condition is
  true

              x = 1
              while x < 10
               puts x.to_s + “ iteration”
               x += 1
              end
TIMES
5.times { puts “hello” }
5.times { |num| puts “hi”+num.to_s }

99.times do |beer_num|
    puts "#{beer_num} bottles of beer”
end

99.times do
  puts "some bottles of beer”
end
CREATING A NEW ARRAY
    x = [1, 2, 3, 4]
    => [1, 2, 3, 4]

    x = %w(1 2 3 4)
    => [“1”, “2”, “3”, “4”]

    chef = Array.new(3,
      “bork”)
    => [“bork”, “bork”, bork”]
ACCESSING ARRAY VALUES
a = [ "a", "b", "c", "d", "e" ]
a[0] #=> "a”

a[2] #=> "c”

a[6] #=> nil

a[1, 2] #=> ["b", "c”]

a[1..3] #=> ["b", "c", "d”]

a[1…3] #=> ["b", "c"]
RANGES
Inclusive
1..4# => runs 1 thru 4
(1..4).to_a# => [1,2,3,4]


Exclusive
1...4# => runs 1 thru 3
(1...4).to_a # => [1,2,3]
OPERATIONS ON ARRAYS
[ 1, 2, 3 ] * 3
=> [1, 2, 3, 1, 2, 3, 1, 2, 3]


[ 1, 2, 3 ].join(“,”)
⇒   "1,2,3”


[ 1, 2, 3 ] + [ 4, 5 ]
=> [1, 2, 3, 4, 5]


[ 1, 1, 2, 2, 3, 3, 4, 5 ] - [ 1, 2, 4 ]
=> [3, 3, 5]


[ 1, 2 ] << "c" << "d" << [ 3, 4 ]
=> [1, 2, "c", "d", [3, 4]]
CREATING A HASH
h = { "a" => 100, "b" => 200 }

h["a"]




h = { 1 => "a", "b" => “hello” }

h[1]
OPERATIONS ON HASHES:
           MERGE
h1 = { "a" => 100, "b" => 200 }
=> {"a"=>100, "b"=>200}
h2 = { "b" => 254, "c" => 300 }
=>{"b"=>254, "c"=>300}

h3 = h1.merge(h2)
=> {"a"=>100, "b"=>254, "c"=>300}
h1
=> {"a"=>100, "b"=>200}

h1.merge!(h2)
=> {"a"=>100, "b"=>254, "c"=>300}
OPERATIONS ON HASHES
h = { "a" => 100, "b" => 200 }
h.delete("a”)


h = { "a" => 100, "b" => 200, "c" => 300, "d"
  => 400 }
letters = h.keys


h = { "a" => 100, "b" => 200, "c" => 300 }
numbers = h.values
EACH
>> superheroes = ["catwoman", "batman", "wonderwoman"]
>> superheroes.each { | s | puts "#{ s } save me!" }
catwoman save me!
batman save me!
wonderwoman save me!

>>   dogs = ["fido", "fifi", "rex", "fluffy"]
>>   dogs_i_want = []
>>   dogs.each do |dog|
>?      dogs_i_want.push(dog) if dog != "fluffy"
>?   end

>>   dogs
=>   ["fido", "fifi", "rex", "fluffy"]
>>   dogs_i_want
=>   ["fido", "fifi", "rex"]
MAP
superheroes = ["catwoman", "batman", "wonderwoman"]



>> superheroes.map { |s| s.upcase }

=> ["CATWOMAN", "BATMAN", "WONDERWOMAN"]



Shorter (available in Rails or Ruby 1.8.7+):

>> superheroes.map(&:upcase)

Iterators, Hashes, and Arrays

  • 1.
    COLLECTIONS & ITERATORS WITH HASHES AND ARRAYS Sarah Allen
  • 2.
    ITERATORS: CONDITIONAL LOOPING • “while” allows us to loop through code while a set condition is true x = 1 while x < 10 puts x.to_s + “ iteration” x += 1 end
  • 3.
    TIMES 5.times { puts“hello” } 5.times { |num| puts “hi”+num.to_s } 99.times do |beer_num| puts "#{beer_num} bottles of beer” end 99.times do puts "some bottles of beer” end
  • 4.
    CREATING A NEWARRAY x = [1, 2, 3, 4] => [1, 2, 3, 4] x = %w(1 2 3 4) => [“1”, “2”, “3”, “4”] chef = Array.new(3, “bork”) => [“bork”, “bork”, bork”]
  • 5.
    ACCESSING ARRAY VALUES a= [ "a", "b", "c", "d", "e" ] a[0] #=> "a” a[2] #=> "c” a[6] #=> nil a[1, 2] #=> ["b", "c”] a[1..3] #=> ["b", "c", "d”] a[1…3] #=> ["b", "c"]
  • 6.
    RANGES Inclusive 1..4# => runs1 thru 4 (1..4).to_a# => [1,2,3,4] Exclusive 1...4# => runs 1 thru 3 (1...4).to_a # => [1,2,3]
  • 7.
    OPERATIONS ON ARRAYS [1, 2, 3 ] * 3 => [1, 2, 3, 1, 2, 3, 1, 2, 3] [ 1, 2, 3 ].join(“,”) ⇒ "1,2,3” [ 1, 2, 3 ] + [ 4, 5 ] => [1, 2, 3, 4, 5] [ 1, 1, 2, 2, 3, 3, 4, 5 ] - [ 1, 2, 4 ] => [3, 3, 5] [ 1, 2 ] << "c" << "d" << [ 3, 4 ] => [1, 2, "c", "d", [3, 4]]
  • 8.
    CREATING A HASH h= { "a" => 100, "b" => 200 } h["a"] h = { 1 => "a", "b" => “hello” } h[1]
  • 9.
    OPERATIONS ON HASHES: MERGE h1 = { "a" => 100, "b" => 200 } => {"a"=>100, "b"=>200} h2 = { "b" => 254, "c" => 300 } =>{"b"=>254, "c"=>300} h3 = h1.merge(h2) => {"a"=>100, "b"=>254, "c"=>300} h1 => {"a"=>100, "b"=>200} h1.merge!(h2) => {"a"=>100, "b"=>254, "c"=>300}
  • 10.
    OPERATIONS ON HASHES h= { "a" => 100, "b" => 200 } h.delete("a”) h = { "a" => 100, "b" => 200, "c" => 300, "d" => 400 } letters = h.keys h = { "a" => 100, "b" => 200, "c" => 300 } numbers = h.values
  • 11.
    EACH >> superheroes =["catwoman", "batman", "wonderwoman"] >> superheroes.each { | s | puts "#{ s } save me!" } catwoman save me! batman save me! wonderwoman save me! >> dogs = ["fido", "fifi", "rex", "fluffy"] >> dogs_i_want = [] >> dogs.each do |dog| >? dogs_i_want.push(dog) if dog != "fluffy" >? end >> dogs => ["fido", "fifi", "rex", "fluffy"] >> dogs_i_want => ["fido", "fifi", "rex"]
  • 12.
    MAP superheroes = ["catwoman","batman", "wonderwoman"] >> superheroes.map { |s| s.upcase } => ["CATWOMAN", "BATMAN", "WONDERWOMAN"] Shorter (available in Rails or Ruby 1.8.7+): >> superheroes.map(&:upcase)

Editor's Notes

  • #2 \n
  • #3 \n
  • #4 5 is an object that is an instance of the integer class\ntimes is a method of the 5 object\ntimes is a method on an object that is an instance of integer\n
  • #5 A lot of the time you will be using an array when you iterate over something\nAn array is just a list of items. \nEvery spot in the list acts like a variable and you can make each spot point to a different object\n\nW means words\n\nArray is a class, needs to start with capital letter\n
  • #6 IRB\nif you go off the array it will be nil\n
  • #7 \n
  • #8 join is cool because it makes a string for you\n\nshovel operator\n\nmultidimensional array\n
  • #9 Does anyone know what a hash is? \nassociative array \ncollection of key-value pairs\nkeys can be numbers or strings \n\nDifference from an Array\n
  • #10 merge takes the value from the second hash\nmerge! changes h1\n
  • #11 you would think that delete should need a bang to change the hash, but delete doesn&amp;#x2019;t exist with a bang\ndelete returns the value\n
  • #12 \n
  • #13 \n