Skip to content

Instantly share code, notes, and snippets.

@ivorpad
Created July 8, 2015 17:29
Show Gist options
  • Select an option

  • Save ivorpad/258ca5576be132d50820 to your computer and use it in GitHub Desktop.

Select an option

Save ivorpad/258ca5576be132d50820 to your computer and use it in GitHub Desktop.
The Each Method - Bloc.io Full Stack
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