In Ruby, when we create a class, we can define attribute accessor and attribute readers.
class Car
attr_reader :model, :top_speed
attr_accessor :current_speed
require_relative('./vehicle') | |
class Car < Vehicle | |
attr_reader :wheels | |
def initialize(title, capacity, make_year, color, fuel_type, wheels) | |
# remember: calling `super` alone will use the same set of parameters | |
# being passed into the current initialize when calling the parent's initialize | |
super(title, capacity, make_year, color, fuel_type) | |
@wheels = wheels |
# NOTE: | |
# for basic data structures such as strings, boolean and integers, | |
# please have a look at week 1 day 2 lecture notes | |
#################################################################### | |
# ARRAYS: lists of things | |
# an array of names | |
names = ["Faisal", "Sara", "Jane"] |
A class in a formal classist language can be an object, but it’s a special kind of object with special properties and methods. It is responsible for creating new instances and for defining the behaviour of instances.
class SmartShoe
def mergesort(array) | |
if array.count <= 1 | |
# Array of length 1 or less is always sorted | |
return array | |
end | |
# Apply "Divide & Conquer" strategy | |
# 1. Divide | |
mid = array.count / 2 |
ARGV
is a convention in programming which refers to the “argument vector,” in basic terms a variable that contains the arguments / parameters passed to a program through the command line.
Typically an array with contains each argument in a certain position within the array. This may work differently in languages other than Ruby.
require_relative('./car_methods') | |
class Car | |
attr_accessor :current_speed | |
def initialize(model, color) | |
@model = model | |
@color = color | |
@current_speed = 0 |
require_relative('./car') | |
bmw = BMW.new("M4") | |
begin | |
bmw.accelerate | |
bmw.accelerate | |
rescue => e | |
puts e | |
end |