-3

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?

Jamy
  • 115
  • 1
  • 9
  • 1
    I don't know Ruby, but I googled "lazy range in ruby" and the results seem to be pretty helpful. – timgeb Oct 21 '18 at 18:07
  • I've searched this myself, but I can't use it like in python. For exmaple in python: `for i in xrange(m): matrix.append(map(int, raw_input().split()))` @timgeb – Jamy Oct 21 '18 at 18:09

1 Answers1

3

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 }
anothermh
  • 9,815
  • 3
  • 33
  • 52
user2357112
  • 260,549
  • 28
  • 431
  • 505