Let’s say I have a triangle with three sides and three angles. s1, a1, s2, a2, s3, a3 and I want to initialize this via triangle.new
Here is the code that I have:
module Triangle
class Triangle
attr_accessor :s1, :a1, :s2, :a2, :s3, :a3
def initialize(s1, a1, s2, a2, s3, a3)
@s1 = s1
@a1 = a1
.
.
.
end
end
end
Now here is my problem. I want to be able to handle triangles with missing angles or sides. I’ve played around with optional arguments, but if you give me four numbers, I don’t know which ones are sides and which ones are angles. Is the best thing to do is require six numbers and just call them using something like triangle.new(5,nil,6,nil,9,nil)? Or is there a better way?
Why not just pass a hash of options to the initialize method, like so:
With this you can do: