Created
July 8, 2015 17:29
-
-
Save ivorpad/258ca5576be132d50820 to your computer and use it in GitHub Desktop.
The Each Method - Bloc.io Full Stack
This file contains hidden or 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
| class ArrayModifier | |
| attr_accessor :array | |
| def initialize(array) | |
| @array = array | |
| end | |
| def exclaim | |
| new_array = [] | |
| array.each do | s | | |
| new_array << "#{s}" + "!" | |
| end | |
| new_array | |
| end | |
| def capsify | |
| new_array = [] | |
| array.each do | s | | |
| new_array << "#{s}".upcase | |
| end | |
| new_array | |
| end | |
| end | |
| my_array = ["I would walk 500 miles", "And I would walk 500 more"] | |
| ArrayModifier.new(my_array).exclaim | |
| ArrayModifier.new(my_array).capsify | |
| #=> ["I would walk 500 miles!", "And I would walk 500 more!"] | |
| class StringModifier | |
| attr_accessor :str | |
| def initialize(str) | |
| @str = str | |
| end | |
| def proclaim | |
| "#{str.split.join('! ')}!" | |
| end | |
| end | |
| StringModifier.new("I would walk 500 miles").proclaim | |
| #=> "I! would! walk! 500! miles!" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment