Created
April 17, 2019 15:36
-
-
Save rikas/26fe0ce2ed789ee0f846a3c89ef1b1e5 to your computer and use it in GitHub Desktop.
OOP Basics — Batch 243
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# car.rb | |
class Car | |
# attr_reader :color, :brand | |
attr_reader :brand | |
# attr_writer :color | |
attr_accessor :color | |
# initializer or constructor | |
def initialize(color, brand) | |
# instance variable 'color' | |
@color = color | |
@brand = brand | |
@engine_started = false | |
end | |
# instance method | |
def engine_started? | |
return @engine_started | |
end | |
# public instance method | |
def start! | |
inject_fuel | |
spark_the_battery | |
take_oxygen | |
@engine_started = true | |
end | |
# explicit getters (or readers) | |
# | |
# def color | |
# return @color | |
# end | |
# def brand | |
# return @brand | |
# end | |
# explicit setters (or writers) | |
# | |
# def color=(color) | |
# @color = color | |
# end | |
private | |
# private instance methods | |
def inject_fuel | |
# 30 lines of code | |
puts "1. Injecting fuel" | |
end | |
def spark_the_battery | |
# 33 lines of code | |
puts "2. Sparking the battery" | |
end | |
def take_oxygen | |
# 78 lines of code | |
puts "3. Taking oxygen" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# car_test.rb | |
require_relative 'car' | |
# Instantiation | |
# car is an instance of the class Car | |
car = Car.new('red', 'Fiat') | |
puts "My car is a #{car.color} #{car.brand}" | |
# paint job | |
car.color = 'blue' | |
# puts "My car is a #{car.color} #{car.brand}" | |
# puts "Is car running?" | |
# puts car.engine_started? ? 'Yes!' : 'No' | |
car.take_oxygen | |
car.inject_fuel | |
# car.start! | |
# puts "What about now?" | |
puts car.engine_started? ? 'Yes!' : 'No' | |
# car2 is an instance of the class Car | |
car2 = Car.new('pink', 'Volvo') | |
# puts "Is car2 running?" | |
# puts car2.engine_started? ? 'Yes!' : 'No' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pry-byebug' | |
def full_name(first, last) | |
first_name = first.capitalize | |
last_name = last.capitalize | |
# if first | |
# another = 'hi' | |
# hi = 'jsd' | |
# else | |
# "lksldk" | |
# end | |
"#{first_name} #{last_name}" | |
end | |
puts full_name('billy', 'joel') | |
puts "The end!" | |
# 1. gem install pry-byebug | |
# 2. require 'pry-byebug' | |
# 3. biniding.pry where you want to stop | |
# 4. !!! | continue | next |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment