Data Structures

I More With Arrays

i. Creating Arrays 1/16

my_array = [1,4,9,76,7]

ii. Access by Index 2/16

demo_array = [100, 200, 300, 400, 500]

print demo_array[2]

iii. Arrays of Non-Numbers 3/16

string_array = ["I","miss","you"]

iv. Arrays of Arrays 4/16

Save and Submit

v. Create Your Own 5/16

my_2d_array = [["learn", 23],[100,"still learn"]]
my_2d_array.each { |x| puts "#{x}\n" }

II Hashes

i. Introduction to Hashes 6/16
Save and Submit

ii. Using Hash.new 7/16

pets = Hash.new

iii. Adding to a Hash 8/16

pets = Hash.new
pets["peppy"] = "rabbit"

iv. Accessing Hash Values 9/16

pets = Hash.new
pets["peppy"] = "rabbit"
puts pets["peppy"]

IIIIterating Over Arrays and Hashes

i. (Re)Introduction to Iteration 10/16

Save and Submit

ii. Iterating Over Arrays 11/16

languages = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
languages.each {|element| puts element}

iii. Iterating Over Multidimensional Arrays 12/16

s = [["ham", "swiss"], ["turkey", "cheddar"], ["roast beef", "gruyere"]]
s.each {|sub_array| puts sub_array}

iv. Iterating Over Hashes 13/16

secret_identities = {
 "The Batman" => "Bruce Wayne",
 "Superman" => "Clark Kent",
 "Wonder Woman" => "Diana Prince",
 "Freakazoid" => "Dexter Douglas"
}
secret_identities.each do |movie,actor|
 puts "#{movie}: #{actor}"
end

IV Review

i. Multidimensional Arrays 14/16

my_array = [["september",15,91],[14,"august",00]]

ii Hashes 15/16

my_hash = {
 "friends" => 5,
 "family" => 100
}

iv. Iterating Over a Hash 16/16

lunch_order = {
 "Ryan" => "wonton soup",
 "Eric" => "hamburger",
 "Jimmy" => "sandwich",
 "Sasha" => "salad",
 "Cole" => "taco"
}
lunch_order.each { |name,food| puts food }

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s