Instructions
Create a Point data class with x and y attributes.
Data classes are created using:
ruby
Point = Data.define(:x, :y)
Data classes are:
- Immutable (can't change values after creation)
- Have automatic equality based on values
- Have a nice inspect/to_s output
Hints:
- Use Point = Data.define(:x, :y)
- Create with Point.new(x: 1, y: 2) or Point[1, 2]
- Access with point.x and point.y
Your Code
Point = Data.define(:x, :y)
RSpec.describe "Point Data class" do
it "creates a point with x and y" do
point = Point.new(x: 3, y: 4)
expect(point.x).to eq(3)
expect(point.y).to eq(4)
end
it "can be created with positional args" do
point = Point[1, 2]
expect(point.x).to eq(1)
expect(point.y).to eq(2)
end
it "has value equality" do
expect(Point[1, 2]).to eq(Point[1, 2])
end
it "is frozen (immutable)" do
point = Point[0, 0]
expect(point.frozen?).to be true
end
end
Results
Click "Run Tests" to see results