I am tryiing to port some python code to ruby, and I am doing pretty well, using equivelent ruby functions, even removing / altering some to use ruby features more.
However at a core point I need to get slices from an array
in python the following works fine:
output=["Apple","Orange","Pear"]
team_slices=[(0,1),(1,2),(2,3)]
for start,end in team_slices:
print output[start:end]
Will output as expected:
['Apple']
['Orange']
['Pear']
Whereas the ruby code:
output=["Apple","Orange","Pear"]
team_slices=[[0,1],[1,2],[2,3]]
team_slices.each do |start,ending|
print output[start..ending]
end
Will output:
["Apple","Orange"]
["Orange","Pear"]
["Pear"]
Is there any way to do the slicing more equivalent to python? I know I am likely missing somethign simple here
Seems like python’s ranges exclude the end value, so just use the
...variant in ruby:PS: In ruby you should use 2 spaces for indentation, if you want to stick to conventions 😉
EDIT| Had to rename
endtolastdue to ruby using it as a syntactical keyword.