Jamy
Jamy

Reputation: 115

What is Ruby's equivalent to pythons xrange?

Apparently xrange in python is faster than range. because xrange creates a sequence of objects lazily. However range creates objects in memory.

What I'd like to know is what is Ruby's equivalent of pythons xrange?

Upvotes: -3

Views: 664

Answers (1)

user2357112
user2357112

Reputation: 280251

Ruby ranges are already lazy, like Python 3 range. Just use a range:

1..10  # includes endpoint
1...10 # excludes endpoint

Idiomatic iteration in Ruby often doesn't involve ranges, though. For example, if you want to do a thing n times, like in your comment:

n.times { do_something }

Upvotes: 3

Related Questions