Skip to content

Instantly share code, notes, and snippets.

@M0119
Last active March 19, 2019 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save M0119/e33152016847f514104e4c86f128698f to your computer and use it in GitHub Desktop.
Save M0119/e33152016847f514104e4c86f128698f to your computer and use it in GitHub Desktop.

Lecture ✍️

Debugging with pry

Here is an example of binding.pry in action.

require 'pry'
def calculate_area(width, height)
    result = 0
    binding.pry
    if (height < 0)
        puts ("ERROR in input")
    else
        result = height * width
        puts ("Calculation check: #{result}")
    end
    return result
end
height = -10
width = 12
calculate_area(height, width)

Introduction to objects

As programs get larger we look for ways to better organise our code. Objects help us manage complexity. Object Oriented programming provides a means to accurately represent "real world concepts" and "business logic".

A class is a type of object. It's like a bluebrint or a template. For example, a person is a class, but you and the person next to you are objects. You both have an eye colour, height and favourite food. You both also perform actions like cook, eat and breathe. When we're writing code we refer to the things an object has as attributes (name, location) and the things an object does (drink, greet) as methods.

Often we want to represent objects in our code like 'students'. What attributes would a student have? What methods would a student have?

Defining a Car class

Here is an example of a Car class.

class Car
    def initialize(make, year, mileage)
        @make = make 
        @year = year 
        @mileage = mileage    
    end
    def drive(distance)
        @mileage += distance
    end
    def description()
        return "Make: #{@make}, Year: #{@year}, Mileage: #{@mileage}"
    end
end

To instantiate a an object we use .new

work_car = Car.new('Toyota', 2018, 5000)

We can then access methods on the car object.

work_car = Car.new('Toyota', 2018, 5000)
puts ("The work car is #{work_car.description()}")
work_car.drive(100)
puts ("The work car is #{work_car.description()}")

References

# 1. Add a data attribute for color
# - change the initialize method
# - change the description
# 2. Create a loop to simulate driving to work for a week
# The loop should display the drive distance and the current car description
# 3. Add a weekend car with the following values
# Make: Ford, Year: 1965, Mileage: 500000
# 4. Vintage cars are over 30 years old
# Add a method that will check if the car is vintage
# call it is_vintage?
# 5. Update description to display vintage status
# 6. Refactor the code to moving the loop to simulate driving to work to a method called drive_to_work
# 7. Add array of cars called fleet_cars
# 8. Add 10 Cars into the fleet_cars array
# 9. Call the drive_to_work method for each car
# 10. Display the description for each of the cars in the fleet_cars array
# ## Beast Mode
# 11. Add to the Car class a getter method called get_mileage that will return the
# value of @mileage
# 12. Update the drive_to_work method to use the get_mileage method instead of
# description
# 13. Add a data attribute for the number passengers to the Car class. Call it passengers.
# - change the initialize method
# - change the description
# 14. Add to the Car class a getter method called get_passengers that will return the
# value of @passengers
# 15. Calculate and display the total passenger capacity of the cars in the fleet_cars array
# Will your fleet be able to take our cohort
# Optional
# Build a Cat class.
# 1. Each cat should have a name
# 2. Implement a speak method to say meow.
# 3. Add each cat's name to the speak method (Pixie says meow).
# example burger
# hamburger1 = Hamburger.new('beef', 'brioche', true, ['tomato sauce', 'onion relish', 'mayo'])
# 1. Display hamburger2 and hamburger3 using puts like was done for hamburger1
# - add an attribute for cheese
# - add cheese to initialize and to_string
# - add a getter for cheese
# - use a puts to test the getter
# 2. Add an attribute for an array of condiments
# - add condiments to initialize and to_string
# - add a getter for condiments
# - use a puts to test the getter
# 3. Create three more burgers and display each of them using puts like was done for hamburger1
# 4. Add a method to toggle_cheese to change the value of cheese from true to false and vise versa
# 5. Call the toggle toggle_cheese for each of the hamburgers you have created and display them again
# 6. Add an attribute accessor to the Hamburger class. It will added directly below the first line of the class declaration
# class Hamburger
# attr_reader(:patty, :bun)
# 7. Test the accessor for hamburger1 by adding the following code
# puts "Using the accessor for bun #{hamburger1.bun}"
# puts "Using the accessor for patty #{hamburger1.patty}"
# 8. Add an accessor for condiments and cheese
# 9. Test the accessors for each of the hamburgers
# 10. Create an array of ten unique hamburgers and use new to initialize them
# 11. Use a loop to test the accessors for each of the hamburgers in the array
# 12. Use pry to explore the hamburger array you created, accessing hamburgers and their attributes
# Optional
# 13. Add an attribute for name
# - add name as the first parameter to initialize and update all references
# - add name to the start of the string in to_string
# - add a attribute accessor for name
# - use a puts to test the accessor
# 14. Add a method called menu_formmated_string.
# - It will display the hamburger details over two lines.
# - The hamburger name will be on the first line and be ALL CAPS.
# - Use the remaining attributes to describe the hamburger.
# 15. Add a method to check if the hamburger is vegetarian and return true or false
# call it is_vegetarian?
# 16. Add a method to check if the hamburger has a condiment ketchup and return true or false
# call it is_american?
# 18. Add a method to check if the hamburger has a condiment and return true or false
# call it is_spicy?
# 19. Add more unique hamburgers to your hamburger array so that there will be at least
# three that will be true in each of the is_american? is_american? is_spicy?
# 20. Use the menu_formmated_string method to display the hamburgers on a menu that has
# four headings; 'All burgers', 'Vegetarian', 'American', and 'Spicy'. Use is_american?
# is_american? and is_spicy? to help select what to display under each heading.
# Hamburgers may appear under more then one heading.
# 21. Refactor the code to create a class for menu
# - menu will have an array of hamburgers
# - menu will have a to_string method that produces a menu as described in 20. above
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment