Skip to main content
Added version using Enumerable.lazy
Source Link
KChaloux
  • 216
  • 1
  • 3

As Ruby, like Perl, emphasizes that there is more than one way to do something, let me suggest my own. This is close to what I'd do in a functional programming language:

def multiples(n, count)
    1.upto(count).map {|i| n * i}
end

multiples(23, 5)
=> [23, 46, 69, 92, 115]

It could also be added (via monkeypatching) to the Fixnum class, if you're willing to engage in that sort of thing. That would allow you to invoke it like this:

23.multiples(5)
=> [23, 46, 69, 92, 115]

This will do the same thing as the step methods provided in other answers. I find it a bit more readable (though I wasn't familiar with the step function until today, so take that as you will).

Edit

While we're at it, let's look at something neat you can do with Ruby 2.0. With the new Enumerable.lazy class, you can easily express this as an infinite sequence, from which you can take small subsets:

def multiples(n)
    (1..Float::INFINITY).lazy.map {|x| x * n}
end

# Now we can get an infinite list, and defer operating on it until later
multiples(23).take(5).to_a
=> [23, 46, 69, 92, 115]

As Ruby, like Perl, emphasizes that there is more than one way to do something, let me suggest my own. This is close to what I'd do in a functional programming language:

def multiples(n, count)
    1.upto(count).map {|i| n * i}
end

multiples(23, 5)
=> [23, 46, 69, 92, 115]

It could also be added (via monkeypatching) to the Fixnum class, if you're willing to engage in that sort of thing. That would allow you to invoke it like this:

23.multiples(5)
=> [23, 46, 69, 92, 115]

This will do the same thing as the step methods provided in other answers. I find it a bit more readable (though I wasn't familiar with the step function until today, so take that as you will).

As Ruby, like Perl, emphasizes that there is more than one way to do something, let me suggest my own. This is close to what I'd do in a functional programming language:

def multiples(n, count)
    1.upto(count).map {|i| n * i}
end

multiples(23, 5)
=> [23, 46, 69, 92, 115]

It could also be added (via monkeypatching) to the Fixnum class, if you're willing to engage in that sort of thing. That would allow you to invoke it like this:

23.multiples(5)
=> [23, 46, 69, 92, 115]

This will do the same thing as the step methods provided in other answers. I find it a bit more readable (though I wasn't familiar with the step function until today, so take that as you will).

Edit

While we're at it, let's look at something neat you can do with Ruby 2.0. With the new Enumerable.lazy class, you can easily express this as an infinite sequence, from which you can take small subsets:

def multiples(n)
    (1..Float::INFINITY).lazy.map {|x| x * n}
end

# Now we can get an infinite list, and defer operating on it until later
multiples(23).take(5).to_a
=> [23, 46, 69, 92, 115]
added 271 characters in body
Source Link
KChaloux
  • 216
  • 1
  • 3

As Ruby, like Perl, emphasizes that there is more than one way to do something, let me suggest my own. This is close to what I'd do in a functional programming language:

def multiples(n, count)
    1.upto(count).map {|i| n * i}
end 

multiples(23, 5)
=> [23, 46, 69, 92, 115]

It could also be added (via monkeypatching) to the Fixnum class, if you're willing to engage in that sort of thing. That would allow you to invoke it like this:

23.multiples(5)
=> [23, 46, 69, 92, 115]

This will do the same thing as the step methods provided in other answers. I find it a bit more readable (though I wasn't familiar with the step function until today, so take that as you will).

As Ruby, like Perl, emphasizes that there is more than one way to do something, let me suggest my own. This is close to what I'd do in a functional programming language:

def multiples(n, count)
    1.upto(count).map {|i| n * i}
end

This will do the same thing as the step methods provided in other answers. I find it a bit more readable (though I wasn't familiar with the step function until today, so take that as you will).

As Ruby, like Perl, emphasizes that there is more than one way to do something, let me suggest my own. This is close to what I'd do in a functional programming language:

def multiples(n, count)
    1.upto(count).map {|i| n * i}
end 

multiples(23, 5)
=> [23, 46, 69, 92, 115]

It could also be added (via monkeypatching) to the Fixnum class, if you're willing to engage in that sort of thing. That would allow you to invoke it like this:

23.multiples(5)
=> [23, 46, 69, 92, 115]

This will do the same thing as the step methods provided in other answers. I find it a bit more readable (though I wasn't familiar with the step function until today, so take that as you will).

Source Link
KChaloux
  • 216
  • 1
  • 3

As Ruby, like Perl, emphasizes that there is more than one way to do something, let me suggest my own. This is close to what I'd do in a functional programming language:

def multiples(n, count)
    1.upto(count).map {|i| n * i}
end

This will do the same thing as the step methods provided in other answers. I find it a bit more readable (though I wasn't familiar with the step function until today, so take that as you will).