Kokizzu
Kokizzu

Reputation: 26838

How to quote this on Ruby?

i have as string, with these value:

'`~!@#:;|$%^>?,)_+-={][&*(<]./"'

how to declare it on .rb without heredoc?

with heredoc:

bla = <<_
'`~!@#:;|$%^>?,)_+-={][&*(<]./"'
_
bla.chop!

Upvotes: 1

Views: 97

Answers (4)

vgoff
vgoff

Reputation: 11313

You don't need to use a heredoc to do this, and you can do this simply without using any escapes in your preparation.

>> %q '`~!@#:;|$%^>?,)_+-={][&*(<]./"' 
=> "'`~!@#:;|$%^>?,)_+-={][&*(<]./\"'"

The key here is that you are not using a space character in this collection of characters, and so we can use a space to delimit it.

You can use %q or %Q to do this.

Don't generally use space for delimiter for this, for obvious reasons, but sometimes it very useful.

Upvotes: 1

sawa
sawa

Reputation: 168101

Using heredoc:

bla = <<_
'`~!@#:;|$%^>?,)_+-={][&*(<]./"'
_
bla.chop!

you can observe the inspection and copy it:

"'`~!@#:;|$%^>?,)_+-={][&*(<]./\"'"

Simple as that.

Upvotes: 2

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230346

In that string you have every character that can be used to escape a string. So even %q/%Q won't help you to use this string verbatim.

%q('`~!@#:;|$%^>?,\)_+-={][&*\(<]./"')
# quoted parentheses

So, your only option is heredoc. With every other method, you'll have to backslash-escape some characters.

Upvotes: 0

Linuxios
Linuxios

Reputation: 35803

You should be using HEREDOC for this, but here you go:

str = '\'`~!@#:;|$%^>?,)_+-={][&*(<]./"\''

Just use double quotes and escape the single quotes in the string. Simple.

Upvotes: 2

Related Questions