In this HackerRank Ruby – Strings – Methods I problem solution In this challenge, your task is to code a process_text method, which takes an array of strings as input and returns a single joined string with all flanking whitespace and new lines removed. Each string has to be separated by a single space.
> process_text([“Hi, n”, ” Are you having fun? “])
“Hi, Are you having fun?”
Problem solution.
# Enter your code here. Read input from STDIN. Print output to STDOUT def process_text arr clean_arr = [] arr.each do | l | l = l.chomp l = l.strip clean_arr << l end clean_arr.join( " " ) end
Second solution.
# Enter your code here. Read input from STDIN. Print output to STDOUT def process_text(arr) new_arr = arr.map { |x| x.strip }.join(" ") end
Third solution.
# Enter your code here. Read input from STDIN. Print output to STDOUT def process_text(arr = []) arr.map(&:chomp).map(&:strip).join(' ') end