Ziu
Ziu

Reputation: 659

How to add a newline in a double quoted string

I have have some code like this:

system("notify-send -i 
  #{Dir.pwd}/#{file} 
  #{parsed_songlist["song"][0]['title']} 
  #{parsed_songlist["song"][0]['artist'].concat("#{parsed_songlist["song"][0]['albumtitle']}")} )

The albumtitle follows the artist name. How can I add a newline between them?

Upvotes: 0

Views: 131

Answers (4)

jon snow
jon snow

Reputation: 3072

I think \n is enough :

"#{parsed_songlist["song"][0]['artist']".concat("\n#{parsed_songlist["song"][0]['albumtitle']")

Upvotes: 0

Mark Thomas
Mark Thomas

Reputation: 37507

Use join.

system("notify-send -i 
  #{Dir.pwd}/#{file} 
  #{parsed_songlist["song"][0]['title']} 
  #{ [
       parsed_songlist["song"][0]['artist'],
       parsed_songlist["song"][0]['albumtitle']
     ].join("\n") }"
)

Upvotes: 0

Dhanu Gurung
Dhanu Gurung

Reputation: 8840

Try this :

"\'#{parsed_songlist['song'][0]['title']}\' 
\'#{parsed_songlist['song'][0]['artist']}\n#{parsed_songlist['song'][0]['albumtitle']}\'"

The problem in your command is if your title/artist/albumtile contain multi-words like hello hi then in command it will appear as notify-send -i /home/username/file hello hi ...

So you could see how that multi-words 'title' converted as two argumenst for notify-send. To tackle such problem use \' as I used above.

However, '\n' is enough for adding a newline in a double qouted string.

Here, is what I get when I used above

enter image description here

Upvotes: 1

sawa
sawa

Reputation: 168081

Put "\n" before the string to be added.

Upvotes: 1

Related Questions