Can splat work on parameter of numbers?


Can splat work on parameter of numbers? In my example of code it only works on strings. But there could be the situation whan we could have x,y,z and so on splat arguments? Is it not working because of booleans? But we can store very mixed arrays, isn't it? Thanks in advance

def greeter(*name)
    return name

def by_three?(number) # Why does splat not work here?
    return number % 3 == 0

new_var = [greeter("John", "Anna"), greeter("Nika"), by_three?(9), by_three?(1)] # => [["John", "Anna"], ["Nika"], true, false]


Splat can work with numbers, you just need to indicate that you want to take many parameters (*) and find a correct way to return desired value.

Here is example:

def by_three?(*numbers)
    numbers.collect { |x| x % 3 == 0 }

print by_three?(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
=> [false, false, true, false, false, true, false, false, true, false]

I have used collect method to create a new array with boolean values indicating if given element is divisible by three or no.


Thanks a lot @factoradic !


You're very welcome! :slight_smile:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.