Created
October 11, 2021 07:36
-
-
Save AlxGolubev/674c4cab5c23568137ef841b744ba12f to your computer and use it in GitHub Desktop.
For students lecture
This file contains hidden or 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
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