Created
October 24, 2018 09:47
-
-
Save rikas/a2a01a30319a94107c3d969333108efa to your computer and use it in GitHub Desktop.
OOP Basics Lecture
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 | |
# filename - lower_snake_case | |
# class name - UpperCamelCase | |
# sports_car.rb => SportsCar | |
class Car | |
attr_reader :brand | |
# attr_writer :color | |
attr_accessor :color # attr_reader + attr_writer | |
# contructor — method that will be called on Car.new | |
def initialize(color, brand) | |
# instance variables - DATA | |
@engine_started = false | |
@color = color | |
@brand = brand | |
end | |
# Instance methods - BEHAVIOUR | |
def engine_started? | |
@engine_started | |
end | |
def start_engine | |
start_spark_plugs | |
turn_on_battery | |
check_fuel | |
@engine_started = true | |
end | |
# attr_reader :brand | |
# explicit getters / readers | |
# def color | |
# @color | |
# end | |
# def brand | |
# @brand | |
# end | |
# def paint_car(new_color) | |
# @color = new_color | |
# end | |
# explicit setters / writers | |
# def color=(new_color) | |
# @color = new_color | |
# end | |
def car_info | |
"This car is a #{@color} #{@brand}" | |
end | |
private | |
def start_spark_plugs | |
puts "1. STARTING SPARK PLUGS" | |
end | |
def turn_on_battery | |
puts "2. TURNING ON BATTERY" | |
end | |
def check_fuel | |
puts "3. CHECKING FUEL" | |
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' | |
# creating a new Car instance | |
car = Car.new('red', 'Ferrari') | |
puts "Engine started: #{car.engine_started?}" | |
car.start_engine | |
# car.start_spark_plugs | |
# car.check_fuel | |
puts "Engine started: #{car.engine_started?}" | |
puts "My car is a #{car.color} #{car.brand}" | |
# paint my car green | |
# car.paint_car('green') | |
car.color = 'green' # is just a car.color=('green') call | |
puts "My car is a #{car.color} #{car.brand}" |
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
# debugging.rb | |
require 'pry-byebug' | |
# gem install pry-byebug | |
# require 'pry-byebug' | |
# insert binding.pry calls | |
# commands: | |
# * next - execute the next line | |
# * continue - continue till the end or to the next | |
# binding.pry call | |
# * !!! - exit | |
def full_name(first, last) | |
# binding.pry | |
first_name = first.capitalize | |
last_name = last.capitalize | |
[first_name, last_name].join(' ') | |
end | |
puts full_name('paul', 'buzzfeed') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment