Your SlideShare is downloading. ×
0
Iterators, Hashes, and Arrays
Iterators, Hashes, and Arrays
Iterators, Hashes, and Arrays
Iterators, Hashes, and Arrays
Iterators, Hashes, and Arrays
Iterators, Hashes, and Arrays
Iterators, Hashes, and Arrays
Iterators, Hashes, and Arrays
Iterators, Hashes, and Arrays
Iterators, Hashes, and Arrays
Iterators, Hashes, and Arrays
Iterators, Hashes, and Arrays
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Iterators, Hashes, and Arrays

624

Published on

Iterators, Hashes, and Arrays

Iterators, Hashes, and Arrays

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
624
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
16
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • \n
  • \n
  • 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
  • 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
  • IRB\nif you go off the array it will be nil\n
  • \n
  • join is cool because it makes a string for you\n\nshovel operator\n\nmultidimensional array\n
  • 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
  • merge takes the value from the second hash\nmerge! changes h1\n
  • you would think that delete should need a bang to change the hash, but delete doesn’t exist with a bang\ndelete returns the value\n
  • \n
  • \n
  • Transcript

    • 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. 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 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”]
    • 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. RANGESInclusive1..4# => runs 1 thru 4(1..4).to_a# => [1,2,3,4]Exclusive1...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 HASHh = { "a" => 100, "b" => 200 }h["a"]h = { 1 => "a", "b" => “hello” }h[1]
    • 9. 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}
    • 10. OPERATIONS ON HASHESh = { "a" => 100, "b" => 200 }h.delete("a”)h = { "a" => 100, "b" => 200, "c" => 300, "d" => 400 }letters = h.keysh = { "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. MAPsuperheroes = ["catwoman", "batman", "wonderwoman"]>> superheroes.map { |s| s.upcase }=> ["CATWOMAN", "BATMAN", "WONDERWOMAN"]Shorter (available in Rails or Ruby 1.8.7+):>> superheroes.map(&:upcase)

    ×