Skip to content

Instantly share code, notes, and snippets.

@sjgjohnston
Created September 27, 2013 22:23
Show Gist options
  • Save sjgjohnston/6736068 to your computer and use it in GitHub Desktop.
Save sjgjohnston/6736068 to your computer and use it in GitHub Desktop.
09/27 Quiz gistfile1.rb
# ,o888888o. 8 8888 88 8 8888 8888888888',8888'
# . 8888 `88. 8 8888 88 8 8888 ,8',8888'
# ,8 8888 `8b 8 8888 88 8 8888 ,8',8888'
# 88 8888 `8b 8 8888 88 8 8888 ,8',8888'
# 88 8888 88 8 8888 88 8 8888 ,8',8888'
# 88 8888 `8. 88 8 8888 88 8 8888 ,8',8888'
# 88 8888 `8,8P 8 8888 88 8 8888 ,8',8888'
# `8 8888 ;8P ` 8888 ,8P 8 8888 ,8',8888'
# ` 8888 ,88'8. 8888 ,d8P 8 8888 ,8',8888'
# `8888888P' `8. `Y88888P' 8 8888 ,8',8888888888888
#
# This is our first week's !quiz Let's find out what we know.
#
# The ideal range of your motor cycle speed 20 - 55. Over 55 is SCAREE!
# Check if your moto_speed is within that range using boolean (&&, ||)
# operators and comparison operators (== =< >= !=)
# if your moto_speed variable is in the right range print out a good
# message, aka "Wheee!" Otherwise print out an appropriate response.
# Your code goes below:
moto_speed = 35 # can test with <20, 20 <= moto_speed <= 55, >55 settings
if ( (moto_speed.to_f >= 20) && (moto_speed.to_f <= 55) )
puts "Wheee!"
elsif (moto_speed.to_f > 55)
puts "Your are way too fast!"
else
puts "You're too slow!"
end
# Make a method that checks your moto speed when called
def check_speed (mph)
case
when (mph.to_f < 20)
puts "You're too slow!"
when ( mph.to_f >= 20) && ( mph.to_f<= 55)
puts "Wheee!"
when (mph.to_f > 55)
puts "Your are way too fast!"
end
end
#Test method
check_speed(19)
check_speed(20)
check_speed(55)
check_speed(56)
# Make a method to start your bike! It should print out "vrooom!"
# when it's called
# your code below:
def start_bike()
puts "vrooom!"
end
#Test method
start_bike
# You're the leader of the pack.
# Create an Array of 3 motorcycle makes!
my_convoy = ["Yamaha", "Kawasaki", "BMW"]
# Loop through your convoy and print out each motorcycle's make
# Your code below:
my_convoy.each do |make|
puts "#{make}"
end
# You need to keep track of your gang.
# Create 3 separate Hashes containing riders' info. like so:
# fred = { name, helmet, height }
# Then a larger Hash containing all riders
# my_gang = {rider hashes}
tom = { "name" => "tom", "helmet" => "white", "height" =>"175cm" }
dick = { "name" => "dick", "helmet" => "green", "height" =>"185cm" }
harry = { "name" => "harry", "helmet" => "blue", "height" =>"195cm" }
# my_gang = {
# tom => { "name" => "tom", "helmet" => "white", "height" =>"175cm" },
# dick => { "name" => "dick", "helmet" => "green", "height" =>"185cm" },
# harry => { "name" => "harry", "helmet" => "blue", "height" =>"195cm" }
# }
my_gang = { :member1 => tom, :member2 => dick, :member3 => harry }
#Test hash contents..
puts my_gang
# Loop through your gang and print out each rider's
# name & helmet color using a block. Your code below:
my_gang.keys.each do |member| # :member1, :member2, :member3
member.keys.each do |person_data| # tom, dick, harry
person_data.keys.each do |data| #
puts data
end
end
end
# Now for each rider add their motorcycle to their Hash,
# assume they are in the same order as your Array
# use a loop. Your code below:
# Define an Class to represent each gang member
# It should include methods to set their name and motorcyle make
# When say_name(name) is called the rider's name is printed out
# Class Rider
# def initialize(name, moto_model)
# end
# def say_name(rider)
# end
# end
# # A fellow student is noticing that instances of his new Foo class are missing
# # their @bar instance variable
# class Foo
# attr_reader :bar
# def intialize(bar)
# @bar = bar
# end
# end
# foo = Foo.new('value of bar')
# foo.bar # TODO value is missing!
# # Fix this code so it prints “hello”
# class Bar
# def say_something
# puts 'hello'
# end
# end
# bar = Bar.new
# bar.hello
# # Final Challenge:
# # 1. initialize 3 new instances of class Rider
# # 2. add these to a new Hash
# # 3. loop through the riders Hash and call say_name for each rider.
# # Hint: you will need an attr_accessor in Rider to call it's method
# # Your code below:
@featherart
Copy link

Nice job on the !Quiz

For future reference you don't need a triple loop to go through your my_gang Hash.
Just use my_gang.each |key, value|

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment