简体   繁体   中英

Remove newlines in Ruby

Is there a way to write a very long xml request in multiple lines instead of just one? When I write it in multiple lines, the string is not accepted because of the newlines inserted. How can I add newlines in my text editor with adding the newlines in the actual program? I am using sublime.

Ruby

str = "this is \n multi-line \n text \n\n"
puts str 
# this is
# multi-line
# text

Use gsub! to modify the string

str.gsub!("\n","") 
puts str # this is  multi-line  text

Rails

Use squish to remove all unnecessary white space

str = "this is \nmulti-line \n text \n\n"
puts str.squish # this is multi-line text

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