RB 129 Interview Preparation

  1. Knowing the material does not mean you are ready to teach the material; and
  2. I really suck at making up code examples under pressure.
class Cat
attr_reader :name
def initialize(name)
@name = name
end
end
whiskers = Cat.new("Whiskers")boots = Cat.new("Boots")
class Vehicle
def accelerate
puts "#{self.class} is moving forward"
end
end
class Car < Vehicle
end
class Truck < Vehicle
end
vehicles = [Vehicle.new, Car.new, Truck.new]vehicles.each do |vehicle|
vehicle.accelerate
end
class Student  def initialize(name, age)
@name = name
@age = age
end
def get_age
puts @age
end
private
attr_reader :age
endmike = Student.new("Mike", 25)
mike.age
class Studentdef initialize(name, age)
@name = name
@age = age
end
def get_age
puts @age
end
private
attr_reader :age
endmike = Student.new("Mike", 25)
mike.get_age
class Studentdef initialize(name, age)
@name = name
@age = age
end
def ==(other)
age == other.age
end
private
attr_reader :age
endmike = Student.new("Mike", 25)
diane = Student.new("Diane", 25)
puts mike == diane
class Studentdef initialize(name, age)
@name = name
@age = age
end
def ==(other)
age == other.age
end
protected
attr_reader :age
endmike = Student.new("Mike", 25)
diane = Student.new("Diane", 25)
puts mike == diane
class Animal
attr_accessor :name, :weight
def initialize(name, weight)
@name = name
@weight = weight
end
endclass Dog < Animal def speaks
puts "Woof"
end
end
class Cat < Animal def speaks
puts "Meow"
end
end
class Animal  def initialize(name, weight)
@name = name
@weight = weight
end
def weight
@weight
end
def weight=(new_weight)
@weight = new_weight
end
end
class Animal
attr_accessor :weight
def initialize(name, weight)
@name = name
@weight = weight
end
end
class Dog
attr_accessor :name, :weight

def initialize(name, weight)
@name = name
@weight = weight
end

def gain_five
self.weight += 5
end

def lose_five
self.weight -= 5
end

end
fido = Dog.new("Fido", 75)
scooby = Dog.new("Scooby", 75)
fido.gain_weight
class Dog
attr_accessor :name, :weight
@@number_of_dogs = 0
def initialize(name, weight)
@name = name
@weight = weight
@@number_of_dogs += 1
end

def self.total_dogs
@@number_of_dogs
end


end
fido = Dog.new("Fido", 125)
p Dog.total_dogs
class Animal  def initialize(name, weight)
@name = name
@weight = weight
end
end

class Dog < Animal
endclass Cat < Animalendclass Platypus < Animalend
  1. We could create a swim method for each individual class or copy and paste the same method into multiple classes. This may work in this simple example, but what if we plan to implement dozens more animals and may want to add even more behaviors in the future?
  2. We could create a module to “mix in” to the classes we want to have access to the swim method. This is likely the best approach for scaling, and is implemented below.
module Swims  def swim
p "I can swim!"
end
endclass Animaldef initialize(name, weight)
@name = name
@weight = weight
end
end
class Dog < Animal
include Swims
endclass Cat < Animalendclass Platypus < Animal
include Swims
end
module LawFirmLAW_SECTIONS = {1.01 => Torts, 1.02 => Criminal, 1.03 => Probate}module TryCases
def litigate
end
end
class Legal def initialize(name, hourly_rate)
(implementation here)
end
def research
end
endclass Lawyer < Legal
include TryCases
endclass Paralegal < Legal
end
end
legal_hash = LawFirm::LAW_SECTIONS

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Palindrome Number — LeetCode

THEXII NFT ASIAN ZODIAC

VIRTUALIZATION AND HYPERVISOR

Selecting Digital Mother Tongue

Golang imports sorting/grouping hell and painless tool to solve it

Creating Your First Azure SQL Database: The Basics

Deep Dive into Open Source with GitHub

Essential Roles and Responsibilities in a Software Development Team

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Clark Reynolds

Clark Reynolds

More from Medium

ELI5: Linked Lists

Valid Mountain Array LeetCode:

An Introduction to Linked Lists

3. Longest Substring Without Repeating Characters