Ruby Language: Array, Hash and Iterators

2,683 views
2,337 views

Published on

Slides for Ruby Class: Collections & Iterators
Original slides by Liah Hansen (edits and additions by Sarah Allen and Wolfram Arnold)

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,683
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • 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
  • 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
  • IRBif you go off the array it will be nil
  • join is cool because it makes a string for youshovel operatormultidimensional array
  • Does anyone know what a hash is? associative array collection of key-value pairskeys can be numbers or strings Difference from an Array
  • merge takes the value from the second hashmerge! changes h1
  • you would think that delete should need a bang to change the hash, but delete doesn’t exist with a bangdelete returns the value
  • Ruby Language: Array, Hash and Iterators

    1. 1. Collections & Iterators<br />With Hashes & Arrays<br />
    2. 2. Iterators: Conditional Looping<br />“while” allows us to loop through code while a set condition is true<br />x = 1<br />while x < 10<br /> puts x.to_s + “ iteration”<br />x += 1<br />end<br />
    3. 3. Times<br />5.times { puts “hello” }<br />5.times { |num| puts “hi”+num.to_s}<br />99.times do |beer_num| <br /> puts "#{beer_num} bottles of beer”<br />end<br />99.times do <br />puts "some bottles of beer”<br />end<br />
    4. 4. Creating a new array<br />x = [1, 2, 3, 4]<br />=> [1, 2, 3, 4]<br />x = %w(1 2 3 4) <br />=> [“1”, “2”, “3”, “4”]<br />chef = Array.new(3, “bork”)<br />=> [“bork”, “bork”, bork”]<br />
    5. 5. Accessing Array Values<br />a = [ "a", "b", "c", "d", "e" ] <br />a[0] #=> "a”<br />a[2] #=> "c”<br />a[6] #=> nil<br />a[1, 2] #=> ["b", "c”]<br />a[1..3] #=> ["b", "c", "d”]<br />a[1…3] #=> ["b", "c"]<br />
    6. 6. Operations on Arrays<br />[ 1, 2, 3 ] * 3 <br />=> [1, 2, 3, 1, 2, 3, 1, 2, 3]<br />[ 1, 2, 3 ].join(“,”)<br /><ul><li>"1,2,3”</li></ul>[ 1, 2, 3 ] + [ 4, 5 ] <br />=> [1, 2, 3, 4, 5]<br />[ 1, 1, 2, 2, 3, 3, 4, 5 ] - [ 1, 2, 4 ]<br />=> [3, 3, 5]<br />[ 1, 2 ] << "c" << "d" << [ 3, 4 ] <br />=> [1, 2, "c", "d", [3, 4]]<br />
    7. 7. Creating a Hash<br />h = { "a" => 100, "b" => 200 }<br />h[“a”]<br />h = { 1 => “a”, “b” => “hello” }<br />h[1]<br />
    8. 8. Operations on Hashes: Merge<br />h1 = { "a" => 100, "b" => 200 } <br />=> {"a"=>100, "b"=>200}<br />h2 = { "b" => 254, "c" => 300 }<br />=>{"b"=>254, "c"=>300}<br />h3 = h1.merge(h2)<br />=> {"a"=>100, "b"=>254, "c"=>300}<br />h1<br />=> {"a"=>100, "b"=>200}<br />h1.merge!(h2)<br />=> {"a"=>100, "b"=>254, "c"=>300}<br />
    9. 9. Operations on Hashes<br />h = { "a" => 100, "b" => 200 } <br />h.delete("a”)<br />h = { "a" => 100, "b" => 200, "c" => 300, "d" => 400 }<br />letters = h.keys <br />h = { "a" => 100, "b" => 200, "c" => 300 } <br />numbers = h.values<br />
    10. 10. Each<br />>> superheroes = ["catwoman", "batman", "wonderwoman"]<br />>> superheroes.each { | s | puts "#{ s } save me!" }<br />catwoman save me!<br />batman save me!<br />wonderwomansave me!<br />>> dogs = ["fido", "fifi", "rex", "fluffy"]<br />>> dogs_i_want = []<br />>> dogs.each do |dog| <br />>? dogs_i_want.push(dog) if dog != "fluffy" <br />>? end<br />>> dogs<br />=> ["fido", "fifi", "rex", "fluffy"]<br />>> dogs_i_want<br />=> ["fido", "fifi", "rex"]<br />
    11. 11. Map<br />superheroes = ["catwoman", "batman", "wonderwoman"]<br />>> superheroes.map { |s| s.upcase } <br />=> ["CATWOMAN", "BATMAN", "WONDERWOMAN"]<br />Shorter (available in Rails or Ruby 1.8.7+): <br />>> superheroes.map(&:upcase) <br />=> ["CATWOMAN", "BATMAN", ”WONDERWOMAN”]<br />

    ×