Collections & IteratorsWith Hashes & Arrays
Iterators: Conditional Looping“while” allows us to loop through code while a set condition is truex = 1while x < 10	puts x.to_s + “ iteration”x += 1end
Times5.times { puts “hello” }5.times { |num| puts “hi”+num.to_s}99.times do |beer_num| 		puts "#{beer_num} bottles of beer”end99.times do puts "some bottles of beer”end
Creating a new arrayx = [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 Valuesa = [ "a", "b", "c", "d", "e" ] a[0] #=> "a”a[2] #=> "c”a[6] #=> nila[1, 2] #=> ["b", "c”]a[1..3] #=> ["b", "c", "d”]a[1…3] #=> ["b", "c"]
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 Hashh = { "a" => 100, "b" => 200 }h[“a”]h = { 1 => “a”, “b” => “hello” }h[1]
Operations on Hashes: Mergeh1 = { "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 Hashesh = { "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!wonderwomansave 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"]
Mapsuperheroes = ["catwoman", "batman", "wonderwoman"]>> superheroes.map { |s| s.upcase } => ["CATWOMAN", "BATMAN", "WONDERWOMAN"]Shorter (available in Rails or Ruby 1.8.7+): >> superheroes.map(&:upcase) => ["CATWOMAN", "BATMAN", ”WONDERWOMAN”]

Ruby Language: Array, Hash and Iterators

  • 1.
  • 2.
    Iterators: Conditional Looping“while”allows us to loop through code while a set condition is truex = 1while x < 10 puts x.to_s + “ iteration”x += 1end
  • 3.
    Times5.times { puts“hello” }5.times { |num| puts “hi”+num.to_s}99.times do |beer_num| puts "#{beer_num} bottles of beer”end99.times do puts "some bottles of beer”end
  • 4.
    Creating a newarrayx = [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 Valuesa= [ "a", "b", "c", "d", "e" ] a[0] #=> "a”a[2] #=> "c”a[6] #=> nila[1, 2] #=> ["b", "c”]a[1..3] #=> ["b", "c", "d”]a[1…3] #=> ["b", "c"]
  • 6.
    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]]
  • 7.
    Creating a Hashh= { "a" => 100, "b" => 200 }h[“a”]h = { 1 => “a”, “b” => “hello” }h[1]
  • 8.
    Operations on Hashes:Mergeh1 = { "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}
  • 9.
    Operations on Hashesh= { "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
  • 10.
    Each>> superheroes =["catwoman", "batman", "wonderwoman"]>> superheroes.each { | s | puts "#{ s } save me!" }catwoman save me!batman save me!wonderwomansave 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"]
  • 11.
    Mapsuperheroes = ["catwoman","batman", "wonderwoman"]>> superheroes.map { |s| s.upcase } => ["CATWOMAN", "BATMAN", "WONDERWOMAN"]Shorter (available in Rails or Ruby 1.8.7+): >> superheroes.map(&:upcase) => ["CATWOMAN", "BATMAN", ”WONDERWOMAN”]

Editor's Notes

  • #4 5 is an object that is an instance of the integer classtimes is a method of the 5 objecttimes is a method on an object that is an instance of integer
  • #5 Alot of the time you will be using an array when you iterate over somethingAn array is just a list of items.Every spot in the list acts like a variable and you can make each spot point to a different objectW means wordsArray is a class, needs to start with capital letter
  • #6 IRBif you go off the array it will be nil
  • #7 join is cool because it makes a string for youshovel operatormultidimensional array
  • #8 Does anyone know what a hash is? associative array collection of key-value pairskeys can be numbers or strings Difference from an Array
  • #9 merge takes the value from the second hashmerge! changes h1
  • #10 you would think that delete should need a bang to change the hash, but delete doesn’t exist with a bangdelete returns the value