JavaScript
x
3
1
def enumerate(arr):
2
(0..arr.length - 1).to_a.zip(arr)
3
Is something built in for this? It doesn’t need to have it’s members immutable, it just needs to be in the standard library. I don’t want to be the guy who subclasses the Array class to add a Python feature to a project.
Does it have a different name in Ruby?
JavaScript
1
3
1
%w(a b c).enumerate
2
=> [[0, "a"], [1, "b"], [2, "c"], [3, "d"]]
3
Advertisement
Answer
Something like this in Python:
JavaScript
1
4
1
a = ['do', 're', 'mi', 'fa']
2
for i, s in enumerate(a):
3
print('%s at index %d' % (s, i))
4
becomes this in Ruby:
JavaScript
1
5
1
a = %w(do re mi fa)
2
a.each_with_index do |s,i|
3
puts "#{s} at index #{i}"
4
end
5