Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'd like to end up with the string "/a_setting/c\blah = something" where attribute gets evaluated to its value: blah. However, I'm seeing the following behavior where the preceding backslash seems to stop the evaluation of the variable:
attribute = "blah"
"/a_setting/c\#{attribute} = something"
=> "/a_setting/c\#{attribute} = something"
"/a_setting/c\ #{attribute} = something"
=> "/a_setting/c blah = something"
To get the string you want:
"/a_setting/c\\#{attribute} = something"
You need to escape the backslash by backslash.
When you do "\#", the "#" is escaped, and is interpreted not as an interpolation element, but as the verbatim "#", which in inspection, appears as "\#" in front of {...} to avoid ambiguity with interpolation.
When you do "\ ", the " " is (redundantly) escaped, and is interpreted as the verbatim " ".
I don't understand what you are pointing at.
But if you are trying to have your attributed evaluated in the string, probably this is what you want
"/a_setting/c\\#{attribute} = something"
coz by
"/a_setting/c\#{attribute} = something"
you are escaping the evaluation by #{} by adding the escape character \
So interpreter will evaluate #{} rather as an expression.
When you add another \ before the other \, the next \ is escaped and evaluated as a normal character.
"\#{attribute}" #:=> "\{attribute}
"\\#{attribute}" #;=> "\blah"
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I tried string interpolation with single quotes using #{} but it does not work. When I tried it with double quotes it works. Please explain to me why this is so, if it's possible to do string interpolation with single quotes and how to do so in ruby if it is possible.
Ruby doesn't interpret single-quoted strings.
This might seem like a limitation at first, but it's actually a nice feature. It allows you to enter many characters without having to escape them, which results in more legible code:
file = 'C:\foo\bar\baz.txt'
# as opposed to:
file = "C:\\foo\\bar\\baz.txt"
Or when having a string about string interpolation itself: (note that Stack Overflow's syntax highlighting is misleading – there's no interpolation)
string = 'In Ruby, you can write "1 + 2 = #{ 1 + 2 }" to get "1 + 2 = 3".'
# instead of:
string = "In Ruby, you can write \"1 + 2 = \#{ 1 + 2 }\" to get \"1 + 2 = 3\"."
Apart from '...' and "...", Ruby also has %q(...) and %Q(...) style string literals (the former without, the latter with interpolation). This is especially useful if your string contains both, single and double quotes:
string = %q(A string containing '...' and "...")
You can even pick your own delimiter: (again, the syntax highlighter can't keep up)
string = %q#A string containing '...', "..." and (...)"#
And finally, you can mix and match different string literal styles:
string = %q(foo) 'bar' "baz"
#=> "foobarbaz"
This is how ruby language is designed.
From language docs: Literals
Double-quote strings allow escaped characters such as \n for newline,
\t for tab, etc.
Double-quote strings allow interpolation of other
values using #{...}:
value = 10
puts "Test value is #{value}"
# => Test value is 10
Interpolation may be disabled by escaping the “#” character or using single-quote strings
puts 'Test value is #{value}'
# => Test value is #{value}
Single-Quoted Strings and Escapes/Expressions
That's just how the language is defined: you can't interpolate using the embedded expression operator (#{}) within a single-quoted string. Unlike double-quoted strings, single-quoted strings aren't scanned for embedded expressions or most escapes. This is important when you need to do things like printing escape characters or unevaluated expressions such as:
puts 'This is a newline character: \n'
puts 'This is how you embed an expression: #{foo}'
That doesn't mean you can't approximate interpolation with single-quoted strings in other ways. For example:
'foo: %s' % 'bar'
#=> "foo: bar"
sprintf '%d, %d, %d', 1, 2, 3
#=> "1, 2, 3"
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a password that contains "\y". I get:
"a\yb" # => "ayb"
'a\yb' # => 'a\\yb'
"a\\yb" # => "a\\yb"
'a\\b' # => "a\\yb"
And nothing (like concatenation or sub) works.
Is there a way to not change the password?
When you say:
'a\yb'
Then you get this back:
"a\\yb"
These are identical as far as Ruby is concerned. Inside of double quotes (") the backslash has special meaning. A single backslash is used to indicate either control codes like \n meaning newline, or literal versions of same, like \\ meaning literal backslash.
Try this:
puts "a\\yb"
Then you'll see exactly what you want. The \\ part is just escaping.
When you use p you're calling:
puts "a\\yb".inspect
This inspect part puts it back into a double-quoted and escaped string, which is where you're getting confused. Print the string, not the "inspected" version of it.
Works for me. Can you show us some context?
ruby -v
#=> ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-linux]
ruby -e 'puts "a\\yb"'
#=> a\yb
After your edits, the problem is using p instead of puts. For instance:
ruby -e 'puts "a\\yb"'
#=> a\yb
ruby -e 'p "a\\yb"'
#=> "a\\yb"
See this discussion.
In simple words p print obj.inspect to output
Simple way to see this:
irb(main):009:0> string = %q{a\yb}
=> "a\\yb"
irb(main):018:0> puts string.inspect
"a\\yb"
irb(main):019:0> p string
"a\\yb"
irb(main):010:0> puts string
a\yb
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Consider i have a strings
goo = "test check\ncode"
if goo =~ /#{Regexp.quote(foo)}/
puts "success!"
end
I need to compare with "foo" regex. How can i write this regex?
Kindly help me in to find this!
I assume that foo contains a string, which represents the regexp. If so, you can initialize Regexp object from that string and perform your matching as follows:
foo = 'test.*check.*code'
goo =~ Regexp.new(foo, Regexp::MULTILINE)
goo = "test check\ncode"
foo = "test.*check.*code"
goo =~ /#{foo}/m
#⇒ 0
The reason why your regexp did not do the trick, is that you have to explicitly set . to match new lines with m Regexp modifier.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to convert "her" to "\"her".
I've tried using insert method:
>> "her".insert(0,'\"')
=> "\\"her"
and
>> "her".insert(0,'"')
=> ""her"
None of them gives me what I want: "\"her"
"her".insert(0,'"')
actually returns "\"her", which is what you said you wanted in the first place.
If you want to obtain "\"her\"", you might want to use Object#inspect:
"her".inspect
=> "\"her\""
Or, you can simply concatenate quotes at the beginning and at the end:
'"' + "her" + '"'
=> "\"her\""
If you just want "\"her"
a = "her".insert(0,'\"')
#=> "\\\"her"
puts a
#=> \"her
If you want output "\"her\""
a = "her"
#=> "her"
b = '\"'+a+'\"'
#=> "\\\"her\\\""
puts b
#=> \"her\"
I think your code is fine, you just don't know the meaning of \\ in console.
The first \ is escape character and second \ is character itself.
You will see \"her in the text foo.txt as you expected by:
File.write("foo.txt", "her".insert(0,'\"'))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the code as below:
require 'colored'
require 'byebug'
str = '英 [faɪnd] 美 [faɪnd]'
regex = /\[([^\[\]]*)\]/
blk = Proc.new{|mat| mat.send(:yellow)}
to_search = str.dup
while regex =~ to_search do
byebug
str.sub! /#{$1}/, blk.call($1)
...
end
Before str.sub! /#{$1}/, blk.call($1),
$1 is "faɪnd"
$' is " 美 [faɪnd]"
After it,
$1 is nil
$' is "] 美 [faɪnd]"
Why does this happen?
It is because the regex /#{$1}/ becomes /faɪnd/. When this regex matches against '英 [faɪnd] 美 [faɪnd]',
The first capture $1 will be nil because /faɪnd/ has no capturing group.
The affix $' becomes "] 美 [faɪnd]", which is right after the match.
It's unclear how you think $1 is being set. It's not a positional parameter like in Bash; it's a special global variable described in the Regexp class. $1 is set to the first capture group of the last match.
Consider:
"foo".match /(foo)/; $1
#=> "foo"
"foo".match /foo/; $1
#=> nil