How do I convert an array of strings into a comma-separated string?
Ruby on-RailsRubyArraysRuby on-Rails Problem Overview
I have an array:
array = ["10", "20", "50", "99"]
And I want to convert it into a simple comma-separated string list like this:
"10", "20", "50", "99"
Ruby on-Rails Solutions
Solution 1 - Ruby on-Rails
array.join(',')
will almost do what you want; it will not retain the quotes around the values nor the spaces after.
For retaining quotes and spaces: array.map{|item| %Q{"#{item}"}}.join(', ')
This will print "\"10\", \"20\", \"50\", \"99\""
. The escaped quotes are necessary assuming the question does in fact call for a single string.
Documentation on the %Q
: string literals.
You could use inspect
as suggested in another answer, I'd say that's personal preference. I wouldn't, go look at the source code for that and choose for yourself.
Useful aside: array.to_sentence
will give you a "1, 2, 3 and 4" style output, which can be nice!
Solution 2 - Ruby on-Rails
["10", "20", "50","99"].map(&:inspect).join(', ') # => '"10", "20", "50", "99"'
Solution 3 - Ruby on-Rails
Here:
array.map {|str| "\"#{str}\""}.join(',')
Solution 4 - Ruby on-Rails
Several answers have offered solutions using #map
, #inspect
, #join
. All of them fail to get certain details of CSV encoding correct for edge cases involving embedded commas and/or string delimiters in the elements.
It's probably a better idea to use the stdlib class CSV then to roll your own.
irb> require 'csv'
=> true
irb> a = [10,'1,234','J.R. "Bob" Dobbs',3.14159]
=> [10, "1,234", "J.R. \"Bob\" Dobbs", 3.14159]
irb> puts a.to_csv
10,"1,234","J.R. ""Bob"" Dobbs",3.14159
The map.join solutions are sufficient if this encoding doesn't need to care about embedded delimiters, or is intended for some internal representation only, but they will fail if generating data for exchange with other programs that expect Comma Separated Values (CSV) as a generally understood representation.
Solution 5 - Ruby on-Rails
The simplest solution is to use the built in ".to_sentence" method.
So
["fred", "john", "amy"].to_sentence outputs "fred, john, and amy"
Solution 6 - Ruby on-Rails
This is a slightly alternative solution, particularly handy if you need to convert an array with double quoted strings to a single quoted list (for say SQL queries):
"'#{["John Oliver", "Sam Tom"].join("','")}'"
to
'John Oliver', 'Sam Tom'
Attribution: https://alok-anand-ror.blogspot.com/2014/04/ruby-join-array-elements-with-single.html
Solution 7 - Ruby on-Rails
This is how you can send push notifications using FCM for Android devices. Assuming you want notify followers when ever a user posts something on their status this is how you do it. This is done in Rails 5.2.6 for Rest Apis--- But still you can use the same for web push notifications. This is for sending to many devices with registration_ids to target followers with notifications. Gem : fcm in your controller:
require "fcm"
def create_vibe(user)
@vibe = user.vibes.new(content: @content, picture: @picture, video: @video, isvideofile: @isvideofile, video_thumbnail: @video_thumbnail, source: @source, background_color: @background_color)
@followed = user.followers
if @followed.present?
@registration = @followed.map { |s| s.registration_id }
end
if @vibe.save
fcm = FCM.new("") # set your FCM_SERVER_KEY
options = {
data: {
notification_type: 1,
title: "#{@vibe.user.username} " "has posted a new Vibe",
body: "#{@vibe.content}",
user_data: {
vibe_id: @vibe.id,
user_id: @vibe.user.id,
background_color: @background_color,
},
},
}
response = fcm.send(@registration, options)
puts response
render :status => 200,
:json => { :success => true,
:info => "Vibe posted successfully",
:vibe_info => {
:content => @content,
:picture => @picture,
:video => @video,
:video_thumbnail => @video_thumbnail,
:isvideofile => @isvideofile,
:source => @source,
:fcm => options,
} }
else
render :status => 200, :json => { :success => false, :result => "Vibe can't be blank" }
end
end