Skip to content

Instantly share code, notes, and snippets.

@AlxGolubev
Created October 11, 2021 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlxGolubev/674c4cab5c23568137ef841b744ba12f to your computer and use it in GitHub Desktop.
Save AlxGolubev/674c4cab5c23568137ef841b744ba12f to your computer and use it in GitHub Desktop.
For students lecture
class Task
attr_accessor :title, :completted
def initialize(title)
@title = title
@completted = false
end
end
class TodoList
def initialize(tasks = [])
@tasks = tasks
end
def add_task(text)
@tasks << Task.new(text)
@tasks.length
end
def toggle_task(id)
@tasks[id - 1].completted = !@tasks[id - 1].completted
end
end
requre 'date'
class User
def born_on
Date.new(1989, 9, 10)
end
end
class UserDecorator < SimpleDelegator
def birth_year
born_on.year
endkkkkkkkkkkkkkkkkkkkkkkkkkk
end
user = User.new
decorated_user = UserDecorator.new(user)
decorated_user.birth_year #=> 1989
decorated_user.born_on #=> #<Date: 1989-09-10 ((2447780j,0s,0n),+0s,2299161j)>
class Vehicle
attr_reader :model
def initialize(model)
@model = model
end
def power_reserve
raise NotImplementedError
end
end
class Car < Vehicle
attr_reader :fuel, :consumption
def initialize(model, fuel, consumption)
super(model)
@fuel = fuel
@consumption = consumption
end
def power_reserve
@fuel / @consumption * 100
end
end
class ElectricCar < Vehicle
def initialize(model, amp_hour, volt, consumption)
super(model)
@capacity = amp_hour * volt
@consumption = consumption
end
def power_reserve
@capacity / @consumption * 100
end
end
class VehicleDecorator < SimpleDelegator
def kilometers_range
"#{self.power_reserve} km"
end
end
car = Car.new('Volvo', 50, 11)
decorated_car = VehicleDecorator.new(car)
decorated_car.kilometers_range
e_car = ElectricCar.new('Tesla', 237, 43, 1650)
decorated_e_car = VehicleDecorator.new(e_car)
decorated_e_car.kilometers_range
class Emoji
def draw
""
end
end
class Apple < Emoji
def draw
puts "🍏"
end
end
class Snake < Emoji
def draw
puts "🐍"
end
end
def print_collection(collection)
collection.each { |entry| print "#{entry}, "}
end
print_collection([1,2,3,4])
print_collection(1..4)
print_collection({ a: 1, b: 2, c: 3, d: 4 })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment