From f9bee05b038cada664e6c55a489fd69c44cc6455 Mon Sep 17 00:00:00 2001 From: Kirill Shestakov Date: Sat, 8 Jun 2024 19:56:31 +0400 Subject: [PATCH 1/2] Add Point class, override == method --- lib/1/1_2.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 lib/1/1_2.rb 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 From 03c86a24923d3d9cdeab0d19b13d86a18a57420f Mon Sep 17 00:00:00 2001 From: Kirill Shestakov Date: Sat, 8 Jun 2024 19:56:52 +0400 Subject: [PATCH 2/2] Add tests for Point class, override == method --- spec/1/1_2_spec.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 spec/1/1_2_spec.rb 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