Created
February 7, 2017 19:54
-
-
Save begin29/28e908b5d8c8e23003ea15d11406fed8 to your computer and use it in GitHub Desktop.
figure task from Denys
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 Figure | |
attr_reader :radius | |
def initialize(color,radius) | |
@color = color | |
@radius = radius | |
raise "first argument needs to be a string" unless color.class == String | |
raise "second argument needs to be a fixnum" unless radius.class == Fixnum | |
end | |
def calculate_area | |
puts "#{@color.to_s} #{self.class.to_s.downcase} has #{self.prepared_area.round(2).to_s} area}" | |
end | |
end | |
class Circle < Figure | |
def prepared_area | |
p radius**2 * Math::PI | |
end | |
end | |
class Square < Figure | |
def prepared_area | |
p radius**2 | |
end | |
end | |
c = Circle.new('blue', 10) | |
c.calculate_area | |
sq = Square.new('yellow', 12) | |
sq.calculate_area |
def calculate_area
puts "#{@color.to_s} ...
end
тут @color
- виклик метода на пряму. Зроби, будь-ласка, через геттер. Тобто attr_reader
та виклик як color
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
у випадку з raise потрібно в місці виклику тоді
виловлювати
цю помилку. і показувати користувачу. Тому що далі після виклику raise код не виконується.місце виклику:
Або другий варіант - це додати ще один атрибут
errors
і перевіряти чи є помилки перед виконуванням тіла методуcalculate_area
.Зроби, будь-ласка варіант 2). З варіантом один попрацюємо згодом, коли писатимемо власні класи помилок.