Needs to implement a wrap_array_elements method that takes in an array and two args for the left and right side wrapping and returns a new array with a string containing the "wrapped" version of each element.
Example arguments: [5, 6, 7, 8, 9]
, "<<"
, ">>"
Should returns an array: ["<<5>>", "<<6>>", "<<7>>", "<<8>>", "<<9>>"]
class Formatter
def wrap_array_elements(elements, left, right)
@elements= elements
@left = left
@right = right
formatted_array = Array.new
formatted_array.push(elements.map {|element| @left item @right})
puts "#{formatted_array}"
end
end
CodePudding user response:
It is much simpler than that, you just need to produce a new array from the original one with map
.
class Formatter
def wrap_array_elements(elements, left, right)
elements.map {|element| "#{left}#{element}#{right}"}
end
end
CodePudding user response:
Or, instead of using string interpolation like in javiyu's solution, you can use Array.join
:
class Formatter
def wrap_array_elements(elements, left, right)
elements.map { |element| [left, element, right].join }
end
end