Hallo
How can I best (shortest) achieve
collect_with_index {…}
It would be worth to add that like
each_with_index
Thanks
Berg
Hallo
How can I best (shortest) achieve
collect_with_index {…}
It would be worth to add that like
each_with_index
Thanks
Berg
each_with_index returns an Enumerator object, upon which you can call collect. Example:
[1, 2, 3].each_with_index.collect { |n, i| "#{i}. #{n}" }
=> ["0. a", "1. b", "2. c"]
-------- Original Message --------
Subject: collect_with_index
Local Time: May 28, 2016 6:32 AM
UTC Time: May 28, 2016 11:32 AM
From: aberger7890@gmail.com
To: ruby-talk@ruby-lang.org
Hallo
How can I best (shortest) achieve
collect_with_index {…}
It would be worth to add that like
each_with_index
Thanks
Berg
Thanks!
Special magic for the letters!
Am 28.05.2016 23:18 schrieb "Julien Negrotto" <jtnegrotto@protonmail.com>:
[1, 2, 3].each_with_index.collect { |n, i| "#{i}. #{n}" }
=> ["0. a", "1. b", "2. c"]
There's also just the with_index Enumerator method that can be used on any
enumerator to add indexs.
Example: [1, 2, 3].collect.with_index { |n, i| "#{i}. #{n}" }
Docs: http://ruby-doc.org/core-2.0.0/Enumerator.html#method-i-with_index
Thanks, I didn't know about that method; this looks much cleaner.
-------- Original Message --------
Subject: Re: collect_with_index
Local Time: May 29, 2016 4:23 AM
UTC Time: May 29, 2016 9:23 AM
From: rhys.m.stansfield@gmail.com
To: ruby-talk@ruby-lang.org
There's also just the with_index Enumerator method that can be used on any enumerator to add indexs.
Example: [1, 2, 3].collect.with_index { |n, i| "#{i}. #{n}" }
Docs: http://ruby-doc.org/core-2.0.0/Enumerator.html#method-i-with_index