简体   繁体   中英

Force Literal ASCII character into string

I'm generating the following array for use in a JS charting library:

['DE',   50], ['GB',   9], ['IE',   2]

However, I need to force in the literal apostrophe so that the JS library renders the chart correctly. ie ['DE', 50], ['GB', 9], ['IE', 2]

I'm sure this is simple, but I just keep ending up with the ASCII codes.

My Ruby code is

@product.sales_data.map { |territory,sales| "['" << territory << "',   " << sales.to_s << "]" }.join(", ")

This works fine in ruby 2.0.0 at least

sales_data = [['DE', 50], ['GB', 9], ['IE', 2]]
sales_data.map { |territory, sales| "['" << territory << "', " << sales.to_s << "]"}.join(", ")

or alternatively

sales_data.map { |territory, sales| "['#{territory}', #{sales}]" }.join(", ")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM