Ruby Struct
A Struct
is like a mini-class.
Circle = Struct.new(:radius) do
PI = 3.14
def area
radius * radius * PI
end
def circumference
2 * PI * radius
end
end
circle = Circle.new(3)
circle.area #=> 28.26
circle.circumference #=> 18.84
circle.radius #=> 3
Another example of a Struct:
ApiResponse = Struct.new(:status_code) do
def success?
status_code == 200 ? true : false
end
end
response = ApiResponse.new(200)
response.status_code # => 200
response.success? # => true