Skip to content

Instantly share code, notes, and snippets.

@harrisonmalone
Created September 23, 2018 12:25
Show Gist options
  • Save harrisonmalone/7a259bc3248865704fae475a0737da35 to your computer and use it in GitHub Desktop.
Save harrisonmalone/7a259bc3248865704fae475a0737da35 to your computer and use it in GitHub Desktop.
# this is maybe the most important day in ruby fundamentals
animals = %w(Calf Duck Elephant Goat Lamb Lion Mule Dog)
animals[8] = "Puma"
animals.insert(4, "Joey")
p animals
animals.reverse!
p animals
animals[2] = "Foal"
animals.push("Bear")
p animals
shopping = []
while shopping.length < 3
puts "what item would you like to add to your shopping list"
item = gets.chomp
if item == "Broccoli"
item = "Ice Cream"
end
shopping << item
end
shopping.sort!
p shopping
# different data types
# we touched on when to use case and when to use if/else
# now looking at arrays and index
# sets with discrete math
# went through indexes and assigning a new value to an array with index
# went into the challenges that involved inserting and deleting parts from an array
animals = ["Calf", "Duck", "Elephant", "Goat", "Lamb", "Lion", "Mule", "Dog"]
new_animals = []
index = 0
while index < animals.length
if animals[index] == "Mule"
new_animals[index] = "Cat"
else
new_animals[index] = animals[index]
# here we are reassigning the value of animals to new_animals at the same index position
end
index += 1
# we increment the index for each loop
end
p new_animals
# more explanation for using while loops with a counter incrementor
# people found this challenge a bit difficult
# the reassignment part is difficult, the syntax can take people a bit to get use to
fruits = ["apple", "banana", "orange", "banana"]
banana = []
index = 0
while index < fruits.length
if fruits[index] == "banana"
banana.push(fruits[index])
end
index += 1
end
# using push in the same example just to reexplain how loops work
# maybe some of the stuff was unnecessary in terms of the different ways you could do push syntax
# and also the array within an array variable
# just know reassigning and the bang !
# showing the different variations you can run with sort and chaining methods together
# tested out sort using multiple arrays
person = {name: "John", height: "2m", weight: "70kgs"}
person[:occupation] = "developer"
person.delete(:weight)
# p person[:height]
person.each do |key, value|
puts "#{key}: #{value}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment