Yaml - how to break very long string [duplicate] - yaml

This question already has answers here:
How do I break a string in YAML over multiple lines?
(9 answers)
Closed 4 years ago.
I have an entry in my yaml file that looks like this
my_key:['short string', 'thisisaverylongstringthatcontains.,specialcharacterssoI havetousequotes,andI wanttobreakintomultiplelines']
My very long string can't contain spaces, and I am worried if I simply use newline, it will get converted to space.
What is the cleanest, simplest way to break down that second string across multiple lines for easier readability and convenience?

key : ['short string', "this is a very long string
that I want to break into
multiple lines"]
Have you tried just inserting a newline? This is valid YAML.
If you don't want spaces, add \ to the end of each line.
key : ['short string', "this is a very long string\
that I want to break into\
multiple lines"]

Related

How to split a string by space and newline in Golang? [duplicate]

This question already has answers here:
Split a string on whitespace in Go?
(4 answers)
Closed 10 months ago.
I'm a beginner in Golang and I want to split a multiline text terminated by the EOF indicator, and I want to do it by the space and the presence of new lines ( since the user is gonna press "enter" a lot ).
Any idea of how?
Use strings.Fields
words := strings.Fields(someString)
See example in The Go Playground

How to add a line in code for ruby [duplicate]

This question already has answers here:
Breaking up long strings on multiple lines in Ruby without stripping newlines
(8 answers)
Closed 6 years ago.
I apologise I am sure the answer is out there but I simply cannot articulate it well enough for google search..
Given a long piece of code
puts 'This is a really long line of ruby code here'
How can you separate it over 2 lines, i.e.
puts 'This is a really long
line of ruby code here'
str = 'first line'\
' second line'\
' third line'
puts str

Surprising string concatenation [duplicate]

This question already has answers here:
Where is Ruby's string literal juxtaposition feature officially documented?
(4 answers)
Closed 7 years ago.
I am surprised by some string concatenation I've stumbled upon in a codebase I support. Why, or how really, does the following manage to concatenate two strings together?
queue_name = 'gen-request-' "#{ENV['USERNAME'].gsub('.','')}"
=> "gen-request-robertkuhar"
I had expected to see a '+' between the two strings, but its not there. Is it implied or something?
I know this just makes more sense with up-the-middle string interpolation. Thats not what I'm asking. I want to know what it is about the language syntax that allows this to work in the first place.
This only works for string literals, and a part of the literal syntax.
If you have 2 string literals with just whitespace between them, they get turned into a single string. It's a convention borrowed from later versions of C.

Not outputting #{variable} within a string [duplicate]

This question already has answers here:
Double vs single quotes
(7 answers)
Ruby - what's the difference between single and double quotes? [duplicate]
(1 answer)
Closed 8 years ago.
I cannot print an input variable as part of a string.
name = gets
>>> Lizzie
print #{name}
>>> Lizzie
That works. However, when I want to do:
name = gets
print 'Your name is: #{name}'
it outputs:
Your name is: #{name}
Any help would be appreciated.
Not sure why #peter_huene didn't post this as an answer, but just to complete this question the answer is:
name = gets
print "Your name is: #{name}"
You use double quotes to get variable injection and other features. Single quotes are literal quotes with no post processing.
#Lizzie - Ruby isn't "more sensitive" - it offers you two explicit ways of handling strings, which is more flexible. Unfortunately this means a bit of "magic" in that strings behave differently depending on how quoted, but it's a feature not a peculiarity.

How to split the string by certain amount of characters in Ruby [duplicate]

This question already has answers here:
Chop a string in Ruby into fixed length string ignoring (not considering/regardless) new line or space characters
(5 answers)
Closed 8 years ago.
How to split the string by certain amount of characters in Ruby?
For example, suppose that I have the following string:
some_string
and I want to split it by every 4th character, so the resulting strings will look like this:
som
e_s
tri
ng
How can I do it?
Thanks in advance.
Using Enumerable#each_slice
'some_string'.chars.each_slice(3).map(&:join)
# => ["som", "e_s", "tri", "ng"]
Using regular expression:
'some_string'.scan(/.{1,3}/)
# => ["som", "e_s", "tri", "ng"]

Resources