Arc Forumnew | comments | leaders | submitlogin
4 points by scn 5937 days ago | link | parent

That gives

  (subseq "hello" 0 -1)
  "hello"
I was going for

  (subseq "hello" 0 -1)
  "hell"
to match python's

  "hello"[:-1]
  "hell"

Are reverse strings 0 or -1 indexed? :)


3 points by mdemare 5937 days ago | link

Both. They use ranges, with have the inclusive (..) and the exclusive notation:

    "ruby"[0 .. -1] #=> "ruby"
    "ruby"[0 ... -1] #=> "rub"

-----

2 points by nex3 5937 days ago | link

Ruby does it the other way:

  > "hello"[-3..-1]
  "llo"
However, given that we can do something like

  > (subseq "hello" -4)
  "ello"
if we want to take the last n characters, doing zero-based reverse indexing might make more sense.

-----

2 points by oddbod 5936 days ago | link

Tcl lrange: http://tcl.tk/man/tcl8.5/TclCmd/lrange.htm

    (subseq "hello" 0 'end)
    "hello"
    (subseq "hello" 0 -1)
    "hell"

-----

2 points by rapp 5937 days ago | link

Your 100% right. I was blindly matching the test case:

    > (subseq "foobar" 2 -2)
    "oba"

-----