naitoh / py2rb

A code translator using AST from Python to Ruby.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

range with step doesnt stop before stop

jayvdb opened this issue · comments

for i in range(0, 10, 2):
    print(i)

should emit 2->8, stepped by two

The transpiler emits

for i in 0.step(10, 2)
    print(i)
  end

which emits 2->10, stepped by two

@jayvdb
Existing testcases didn't catch this one..
Ruby uses a greater than, whereas python is a greater-than/equal-to for positive steps

But this seems a little tricky to do "in-place" as the range step may be ascending or descending.

I'm not sure how much this deviates from the original philosophy of trying to do as much as possible in-line, but I think an additional range-function: PyLib.range() on the ruby side that closely maps to python's behavior is the best way to proceed in terms of correctness - at the expense of having to include the module using either the -r/-b options for py2rb.

I am open to either approach. py2many transpilers usually have a supporting library to deal with Python, and I am already adding a -r .../py2rb/builtins/module.rb in my wrapper. However most also implement Python range by doing +1 or -1 on the args in-place. See https://github.com/adsharma/py2many/search?q=visit_range