Ruby map to append dot at the end

Viewed 156

I have the below line

new_records = records.map{|record| record.name.to_s.}.sort

which outputs

# Current Output:

new_records = ["ab-12.cd.net", "de-34.fg.net"]

How can I make it to return with a . (dot) at the end?

# Desired Output:

new_records = ["ab-12.cd.net.", "de-34.fg.net."]

I can do it using another map, for example

new_records = records.map{|record| record.name.to_s.}.sort
new_records2 = new_records.map { |record| "#{record}." }

But looking for an efficient way of achieving it using the 1st map itself.

1 Answers

this would work:

new_records = records.map{|record| "#{record.name.to_s}." }.sort

you can also use + like this:

new_records = records.map{|record| record.name.to_s + "." }.sort
Related