Ignore embedded quotes in ruby - ruby

I'm not exactly sure what is happening, but the xml file I am writing to has this:
This "is" now my String
when it should look like this:
This "is" now my String
Here is the code, except I don't have access to the actual string at compile time. Is there a way to tell the assetName variable to treat embedded quotes as quotes? Thanks.
assetName = 'This "is" now my String'
response.search("property[name=next]").first.andand["value"] = assetName

In XML, quotes may never appear in attributes, regardless of the style of the quotes used for the attributes. It is likely that your XML library is escaping these quotes for you.

Related

Escape double quotes in qsTrId source string

For a project I'm working on, I'm forced to use qsTrid() that need the following syntax to work:
//% "My string"
text: qsTrId('my_string')
Until you need to use double quotes there are no drawbacks in using this for simple strings, still you cannot use arguments normally like you would do for qsTr().
The problem arise when you need something like this:
//% "My "super" string"
text: qsTrId('my_super_string')
lupdate will only see "My " as source string, since the double quotes are used as delimiter.
Up to now the best I could achieve is this:
//% "My ""super"" string"
That will produce "My super string", the string is not broken but is missing the double quotes.
I've tried searching online and in the documentation if there are special rules in this case, but had no luck.
I tried using single quotes but lupdate does not see the string at all in this case.
I cannot use \" because the source string is used in the other language that will not be translated, but if it is the only solution I'll try to propose it.
Anyone know how to correctly escape the double quotes in this situation?

How exactly do quotes in Ruby work to form a string?

I don't quite understand how string quotes in Ruby actually work. How does wrapping something in a quote suddenly make it a string? What exactly are the quotes doing? I'm trying to understand the C or core language implementation of this.
What exactly are the quotes doing?
The quotes themselves do nothing. They're just markers. Here's where a string starts, here's where it ends. When your code is being parsed to be executed, the parser will take what's between the quotes and make a string from that content. Simple as that.
If you take a compilers course in the school, chances are that you'll have to implement your own parser and compiler/interpreter for some toy language. Likely, with strings too. It's a fun exercise! :)
BTW, in ruby you can write a string literal in many ways. Not only using quotes. This is a string too, for example
html = <<-HTML
<head><title>stack overflow</title></head>
HTML
html # => " <head><title>stack overflow</title></head>\n"
In ruby the most common syntax for creating a string is using quotes like below.
my_msg = "Hello"
This is same in most other languages as well (c, java etc). AFAIK the language's parser is responsible for detecting the above syntax and continue to store Hello as a string in my_msg variable.
Ruby also has many other syntax for creating strings.

Interpolation within single quotes

How can I perform interpolation within single quotes?
I tried something like this but there are two problems.
string = 'text contains "#{search.query}"'
It doesn't work
I need the final string to have the dynamic content wrapped in double quotes like so:
'text contains "candy"'
Probably seems strange but the gem that I'm working with requires this.
You can use %{text contains "#{search.query}"} if you don't want to escape the double quotes "text contains \"#{search.query}\"".
'Hi, %{professor}, You have been invited for %{booking_type}. You may accept, reject or keep discussing more about this offer' % {professor: 'Mr. Ashaan', booking_type: 'Dinner'}
Use
%q(text contains "#{search.query}")
which means start the string with single quote. Or if you want to start with double quote use:
%Q(text contains '#{text}')

How to replace single quotes with escaped single quotes in ruby

I'm trying to replace single quotes (') with escaped single quotes (\') in a string in ruby 1.9.3 and 1.8.7.
The exact problem string is "Are you sure you want to delete '%#'". This string should become "Are you sure you want to delete \'%#\'"
Using .gsub!(/\'/,"\'") leads to the following string "Are you sure you want to %#'%#".
Any ideas on what's going on?
String#gsub in the form gsub(exp,replacement) has odd quirks affecting the replacement string which sometimes require lots of escaping slashes. Ruby users are frequently directed to use the block form instead:
str.gsub(/'/){ "\\'" }
If you want to do away with escaping altogether, consider using an alternate string literal form:
str.gsub(/'/){ %q(\') }
Once you get used to seeing these types of literals, using them to avoid escape sequences can make your code much more readable.
\' in a substitution replacement string means "The portion of the original string after the match". So str.gsub!(/\'/,"\\'") replaces the ' character with everything after it - which is what you've noticed.
You need to further escape the backslash in the replacement. .gsub(/'/,"\\\\'") works in my irb console:
irb(main):059:0> puts a.gsub(/'/,"\\\\'")
Are you sure you want to delete \'%#\'
You need to escape the backslash. What about this?
"Are you sure you want to delete '%#'".gsub(/(?=')/, "\\")
# => "Are you sure you want to delete \\'%#\\'"
The above should be what you want. Your expected result is wrong. There is no way to literally see a single backslash when it means literally a backslash.

Another way instead of escaping regex patterns?

Usually when my regex patterns look like this:
http://www.microsoft.com/
Then i have to escape it like this:
string.match(/http:\/\/www\.microsoft\.com\//)
Is there another way instead of escaping it like that?
I want to be able to just use it like this http://www.microsoft.com, cause I don't want to escape all the special characters in all my patterns.
Regexp.new(Regexp.quote('http://www.microsoft.com/'))
Regexp.quote simply escapes any characters that have special regexp meaning; it takes and returns a string. Note that . is also special. After quoting, you can append to the regexp as needed before passing to the constructor. A simple example:
Regexp.new(Regexp.quote('http://www.microsoft.com/') + '(.*)')
This adds a capturing group for the rest of the path.
You can also use arbitrary delimiters in Ruby for regular expressions by using %r and defining a character before the regular expression, for example:
%r!http://www.microsoft.com/!
Regexp.quote or Regexp.escape can be used to automatically escape things for you:
https://ruby-doc.org/core/Regexp.html#method-c-escape
The result can be passed to Regexp.new to create a Regexp object, and then you can call the object's .match method and pass it the string to match against (the opposite order from string.match(/regex/)).
You can simply use single quotes for escaping.
string.match('http://www.microsoft.com/')
you can also use %q{} if you need single quotes in the text itself. If you need to have variables extrapolated inside the string, then use %Q{}. That's equivalent to double quotes ".
If the string contains regex expressions (eg: .*?()[]^$) that you want extrapolated, use // or %r{}
For convenience I just define
def regexcape(s)
Regexp.new(Regexp.escape(s))
end

Resources