Reputation: 137
The no_repeats
method is to check if any of the years in the range have repeating number. I paired it with a no_repeat?
method that checks individual years.
def no_repeats(year_start, year_end)
no_repeat_years = [year_start..year_end].select { |year| no_repeat?(year) }
end
def no_repeat?(year)
numbers = year.split("")
numbers.each do |n|
if numbers.count(n) > 1
return false
end
end
return true
end
When I try to run the no_repeats
, I'm getting the following error:
undefined method `split' for 1980..2000:Range (NoMethodError)
Why is the entire range getting plugged into the helper function?
Upvotes: 0
Views: 61
Reputation: 2505
On the third line, you have [year_start..year_end]
. This is not what you want. What this does is it creates an array containing 1 element: your range. So this becomes equivalent to this:
years = year_start..year_end
value = [years]
value.select do |year|
year.class # => Range
end
So, instead of [year_start..year_end]
, you should do (year_start..year_end)
. Instead of creating an array, this puts precedence on the range, so it will still work as expected (i.e. the #select
method will be called on the range instead of year_end
).
Upvotes: 2