Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lib/1/1_2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Урок 1
# Задание 2
# реализовать класс Point, аттрибуты x, y, color. Пример использования:
# point1 = Point.new(x: 12, y: 43, color: 'black')

# point2 = Point.new do |point|
# point.x = 12
# point.y = 43
# point.color = 'black'
# end

# point1 == point2 # true

class Point
attr_accessor :x, :y, :color

def initialize(x: nil, y: nil, color: nil)
@x = x
@y = y
@color = color
yield(self) if block_given?
end

def ==(point)
point.is_a?(Point) && x == point.x && y == point.y && color == point.color
end
end
16 changes: 16 additions & 0 deletions spec/1/1_2_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require './lib/1/1_2'

RSpec.describe '1_2, Point:' do
let(:point1) { Point.new(x: 12, y: 43, color: 'black') }
let(:point2) { Point.new do |point|
point.x = 12
point.y = 43
point.color = 'black'
end }

it 'creates the same point objects using block or hash given to initializer' do
expect(point1).to eq(point2)
end
end