Skip to content

Instantly share code, notes, and snippets.

@dukegreene
Created April 3, 2020 17:49
Show Gist options
  • Save dukegreene/5b391061c193aa8de2fdcf6fb234a689 to your computer and use it in GitHub Desktop.
Save dukegreene/5b391061c193aa8de2fdcf6fb234a689 to your computer and use it in GitHub Desktop.
Worked on review questions this morning:
## QUESTION 1
pokemon = [
{
"id": 1,
"name": "bulbasaur",
"base_experience": 64,
"height": 7,
"is_default": true,
"order": 1,
"weight": 69,
"abilities": [
{
"is_hidden": true,
"slot": 3,
"ability": {
"name": "chlorophyll",
"url": "http://pokeapi.co/api/v2/ability/34/"
}
}
]
},
{
"id": 3,
"name": "venesaur",
"base_experience": 50,
"height": 10,
"is_default": true,
"order": 1,
"weight": 90,
"abilities": [
{
"is_hidden": true,
"slot": 3,
"ability": {
"name": "fire",
"url": "http://pokeapi.co/api/v2/ability/38/"
}
}
]
},
{
"id": 2,
"name": "pikachu",
"base_experience": 60,
"height": 4,
"is_default": true,
"order": 1,
"weight": 37,
"abilities": [
{
"is_hidden": true,
"slot": 3,
"ability": {
"name": "lightning",
"url": "http://pokeapi.co/api/v2/ability/30/"
}
}
]
}
]
# How would you get the url for Bulbasaur's ability?
p pokemon[0][:abilities][0][:ability][:url]
# How would you return the first pokemon with base experience over 40?
found_pokemon = pokemon.find do |poke_hash|
poke_hash[:base_experience] > 40
end
p found_pokemon
# How would you return ALL OF THE pokemon with base experience over 40? (Gotta catch em all)
found_pokemons = pokemon.select do |poke_hash|
poke_hash[:base_experience] > 40
end
p found_pokemons
# How would you return an array of all of the pokemon's names?
names = pokemon.map { |poke| poke[:name] }
p names
# How would you determine whether or not the pokemon array contained any pokemon with a weight greater than 60?
# whatever method you use should return true if there are any such pokemon, false if not.
p pokemon.any? do |poke|
poke[:weight] > 60
end
# QUESTION 2
# Finish the implementation of the Car class so it has the functionality described below
class Car
attr_reader :make, :model
@@all = []
def self.all
@@all
end
# If no argument is give, an empty hash will be used instead
def initialize(car_info = {})
@make = car_info[:make]
@model = car_info[:model]
end
def self.drive
"VROOOOOOOOOOOOM!"
end
end
# Refactored all .new calls to take in a hash, to tackle the bonus
volvo_lightning = Car.new({ make: "Volvo", model: "Lightning" })
yugo = Car.new({ make: "Zastava", model: "Yugo" })
# Hash brackets are optional when passing in keys/value pairs as arguments
lada = Car.new(make: "AvtoVAZ", model: "Lada")
p volvo_lightning.make
#=> "Volvo"
p volvo_lightning.model
#=> "Lightning"
p Car.drive
# => "VROOOOOOOOOOOOM!"
p Car.all
#=> [#<Car:0x00007fae28930f20>, #<Car:0x00007fae28923370>, #<Car:0x00007fae2891ae78>]
# BONUS:
merkur_scorpio = Car.new(make: "Merkur", model: "Scorpio")
p merkur_scorpio.make
#=> "Merkur"
p merkur_scorpio.model
#=> "Scorpio"
# QUESTION 3
# begin to build a simple program that models Instagram
# you should have a User class, a Photo class and a comment class
class User
attr_reader :name
def initialize(name)
@name = name
end
def photos
Photo.all.select do |photo|
photo.user == self
end
end
end
class Photo
attr_accessor :user
@@all = []
def self.all
@@all
end
def initialize
self.class.all << self
end
def make_comment(message)
Comment.new(message, self)
end
def comments
Comment.all.select do |comment|
comment.photo == self
end
end
end
class Comment
attr_reader :message, :photo
@@all = []
def self.all
@@all
end
def initialize(message, photo)
@message = message
@photo = photo
@@all << self
end
end
sandwich_photo = Photo.new
sophie = User.new("Sophie")
sandwich_photo.user = sophie
p sandwich_photo.user.name
# => "Sophie"
p sophie.photos
# => [#<Photo:0x00007fae2880b370>]
p sandwich_photo.comments
# => []
p sandwich_photo.make_comment("this is such a beautiful photo of your lunch!! I love photos of other people's lunch")
p sandwich_photo.comments
# => [#<Comment:0x00007fae28043700>]
p Comment.all
#=> [#<Comment:0x00007fae28043700>]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment