method
with_index
![Moderate documentation Importance_2](https://d2vfyqvduarcvs.cloudfront.net/images/importance_2.png?1349367920)
v2_4_6 -
Show latest stable
-
2 notes -
Class: Enumerator
- 1_8_6_287
- 1_8_7_72
- 1_8_7_330
- 1_9_1_378 (0)
- 1_9_2_180 (3)
- 1_9_3_125 (38)
- 1_9_3_392 (0)
- 2_1_10 (0)
- 2_2_9 (0)
- 2_4_6 (0)
- 2_5_5 (0)
- 2_6_3 (0)
- What's this?
with_index(p1 = v1)
public
Iterates the given block for each element with an index, which starts from offset. If no block is given, returns a new Enumerator that includes the index, starting from offset
offset |
the starting index to use |
Register or
log in
to add new notes.
Rubybull -
February 2, 2013 - (v1_9_3_125)
KimSJ -
March 5, 2013
![Default_avatar_30](https://www.gravatar.com/avatar/249c7fd851c5c5ac5a1abdb756472ae1?size=30&default=http://apidock.com/images/default_avatar_30.png)
1 thank
Enumerator#with_index has confusing documentation
Enumerator#with_index has confusing documentation, but hopefully this will make it clearer.
Code example
a=[11,22,31,224,44].to_enum => [11, 22, 31, 224, 44] a.with_index { |val,index| puts "index: #{index} for #{val}" } index: 0 for 11 index: 1 for 22 index: 2 for 31 index: 3 for 224 index: 4 for 44 a=[11,22,31,224,44].to_enum => #<Enumerator: [11, 22, 31, 224, 44]:each> a.with_index(2){ |val,index| puts "index: #{index} for #{val}" if val > 30 } index: 4 for 31 index: 5 for 224 index: 6 for 44 => [11, 22, 31, 224, 44
![Default_avatar_30](https://www.gravatar.com/avatar/f8d23f3453c2cf24ba36c568d84ed6c6?size=30&default=http://apidock.com/images/default_avatar_30.png)
0 thanks
Minor correction to Rubybull's examples?
Was your first example intended to be:
a=[11,22,31,224,44] => [11, 22, 31, 224, 44] a.each.with_index { |val,index| puts "index: #{index} for #{val}" }