you are here: codestackercodes [RSS]Array method to return array elements as quoted strings

Array method to return array elements as quoted strings

class Array
  def to_quoted_s(q="'")
    "#{q}#{self.join("#{q}, #{q}")}#{q}"
  end
end

# Example:


a = ['a', 'b', 'c', 'd', 'e']
puts a.to_s # abcde

puts a.to_quoted_s # 'a', 'b', 'c', 'd', 'e'

puts a.to_quoted_s("\"") # "a", "b", "c", "d", "e"

Comments