Over a million developers have joined DZone.

Ruby Array Extension Method To Return Array Elements As Quoted Strings

·
// Ruby Array extension 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"

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}