I'm doing a ruby kata to return an array of perfect squares given an array. A perfect square such as 4 will yield 2.0. A number that isn't perfect will be a decimal like 2.343 etc. I want to make it work so that, if the tenths place is anything other than zero, it will drop the number. Any thoughts?
I would use modulo, but I don't think there's a use for what I'm trying to do. I was wondering if there was a method other than modulo that could grab the remainder of a number for comparison. I saw the ruby docs for zero?
, divmod
, and a few others, but they don't do exactly what I need.
Here is my code:
def get_squares(array)
array.to_a.drop_while do |number|
Math.sqrt(number) #== drop if remainder is something other than .0
end
end
get_squares((1..16))