Re:Build a minimal decorator with Ruby in 30 minutes
by ricardobeat on Thursday June 12, 2025 @05:50AM
(#44255761)
I don’t get how this works. Won’t the spaceship operator always return 1, 0, -1?
[ Parent
| on HN ]
Re:Build a minimal decorator with Ruby in 30 minutes
by onli on Thursday June 12, 2025 @05:56AM
(#44255788)
Yes. And then clamp(0..) removes the -1, maps it to a 0. Why you'd want that? No idea.
Re:Build a minimal decorator with Ruby in 30 minutes
by mdoliwa on Thursday June 12, 2025 @06:35AM
(#44256011)
so this just says if we have free places or not?
students.size < maximum_number_of_students
Re:Build a minimal decorator with Ruby in 30 minutes
by jackbracken on Thursday June 12, 2025 @06:45AM
(#44256084)
It gives the number of free places, hinted by the method name `available_places`
Needlessly ugly way to write it imo though
Re:Build a minimal decorator with Ruby in 30 minutes
by vidarh on Thursday June 12, 2025 @07:29AM
(#44256414)
I think it probably made sense to the author because they've used all three (-1,0,1) for other examples, and would've been fine until separated out to a method reused to show the actual number.
I think they tried to be a bit too clever, basically.