diff --git a/lib/1/1_2.rb b/lib/1/1_2.rb new file mode 100644 index 0000000..57beb84 --- /dev/null +++ b/lib/1/1_2.rb @@ -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 diff --git a/spec/1/1_2_spec.rb b/spec/1/1_2_spec.rb new file mode 100644 index 0000000..3870f52 --- /dev/null +++ b/spec/1/1_2_spec.rb @@ -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