Skip to content

Instantly share code, notes, and snippets.

@keiththomps
Created January 10, 2020 15:19
Show Gist options
  • Save keiththomps/619320ba8a4a48462352531ef633e0f1 to your computer and use it in GitHub Desktop.
Save keiththomps/619320ba8a4a48462352531ef633e0f1 to your computer and use it in GitHub Desktop.
Code from Introduction to Python Development lectures translated to Ruby

To test this out, download all of these files into the same directory and run the following command from within that directory on the command line:

$ ruby using_classes.rb
4-cylinder
front-driver
front-passenger
rear-driver
rear-passenger
A care with an 4-cylinder engine, and front-driver, front-passenger, rear-driver, rear-passenger tires
This is the description when `tires` contains Tire objects
A care with an 4-cylinder engine, and P205/55R15, P205/55R15, P205/55R15, P205/55R15 tires
Wheel circumference 418.4
Wheel circumference with empty array for `tires` 0
class Car
# Ruby doesn't have the concept of a docstring
# The attr_reader class function allows us specify that we want Ruby to automatically
# create instance methods that will access the data from the `@engine` and `@tires`
# instance variables. This effectively creates this method:
#
# def engine
# @engine
# end
attr_reader :engine, :tires
# attr_writer allows us to have Ruby automatically create a setter method for the `@tires` instance
# variables so that we can do this:
#
# our_car.tires = []
#
# This effective creates the following method:
#
# def tires=(tires_value)
# @tires = tires_value
# end
attr_writer :tires
# Note: You could use `attr_accessor` as a replacement for when you use attr_reader and attr_writer for the
# same instance variable
# the `initialize` method is equivalent to __init__ in python
def initialize(engine, tires)
# The @ indicates that we're working with an instance variable (an attribute of the object)
@engine = engine
@tires = tires
end
def description
# We're able to access our instance variables directly or we can use the reader method for `tire` that we
# created with the `attr_reader` line
puts "A care with an #{@engine} engine, and #{tires.join(", ")} tires"
end
def wheel_circumference
tires.length > 0 ? tires.first.circumference : 0
end
end
class Tire
attr_reader :tire_type, :width, :ratio, :diameter, :brand, :construction
def initialize(tire_type, width, ratio, diameter, brand='', construction='R')
@tire_type = tire_type
@width = width
@ratio = ratio
@diameter = diameter
@brand = brand
@construction = construction
end
def circumference
# It is important that 100 is a float here.
side_wall_inches = (width * (ratio / 100.0)) / 25.4
total_diameter = side_wall_inches * 2 * diameter
(total_diameter * Math::PI).round(1)
end
# to_s is roughly equivalent (but not exactly the same) as __repr__ in Python
def to_s
"#{tire_type}#{width}/#{ratio}#{construction}#{diameter}"
end
end
require_relative './car.rb'
require_relative './tire.rb'
honda = Car.new('4-cylinder', ['front-driver', 'front-passenger', 'rear-driver', 'rear-passenger'])
# Print the engine value
puts honda.engine
# Print the value of `tires` for the `honda` variable
puts honda.tires
# Call the `description` method on our Car instance so that it will automatically use the values of engine and tires for the instance
honda.description
# A more complicated example where we compose a Car by passing in instances of Tire for the tires
tire = Tire.new('P', 205, 55, 15)
tires = Array.new(4, tire)
honda = Car.new('4-cylinder', tires)
puts "This is the description when `tires` contains Tire objects"
honda.description
puts "Wheel circumference #{honda.wheel_circumference}"
honda.tires = []
puts "Wheel circumference with empty array for `tires` #{honda.wheel_circumference}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment