Created
May 26, 2014 02:46
-
-
Save vpoola88/a9b5392e537a6eb49cb4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add a method named add_index to the Array class. The method should take the index value of an element's position and add it to the String in the same position. | |
Hint: the each_with_index method should help you solve this problem. Try to find the each_with_index method yourself in Ruby Docs, and learn how it's used. | |
2.0.0-p353 :001 > class Array | |
2.0.0-p353 :002?> def add_index | |
2.0.0-p353 :003?> self.each_with_index do |value, position| | |
2.0.0-p353 :004 > puts "#{position} is #{value}" | |
2.0.0-p353 :005?> end | |
2.0.0-p353 :006?> end | |
2.0.0-p353 :007?> end | |
=> nil | |
2.0.0-p353 :008 > [1,2,3].add_index | |
0 is 1 | |
1 is 2 | |
2 is 3 | |
=> [1, 2, 3] | |
2.0.0-p353 :009 > [1,2,3].add_index.join(" ") | |
0 is 1 | |
1 is 2 | |
2 is 3 | |
=> "1 2 3" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
which you'd actually write: