Coffeescript backwards double quotes - ruby-on-rails

in this gist
https://gist.github.com/greedo/957ba26575b3f5e445dc
there is a comments.coffee file.
in that it says
#accessor 'quote', ->
"“#{#get('current_comment')?.body}”"
The alternate type of double quotes is used. Is that on purpose? What are those called, and what is it doing there? Or is this just some character set conversion error. Tried to search but i have no idea what backwards double quotes are called.

Check out #mudasobwa's answer on this question How do I declare a string with both single and double quotes in YAML?
The main purpose of those quotes is so that it doesn't collide with the standard double quotes if dealing with a string that needs to output one. The coder can get away with having to remember to add a \ if he makes sure that every string that needs a double quote uses “ instead of ".
The code may be changed to the following without any effect.
#accessor 'quote', -> "\"#{#get('current_comment')?.body}\""

Related

Jenkins - How to use credentials and parameters within same shell string?

I am currently facing an issue, where I have to use parameters and credentials in the same shell string, which is causing me a lot of trouble.
When handling passwords in Jenkins the documentation emphasizes the use of single quotes to avoid the interpolation of sensitive variables:
Double quote example:
Single quote example:
However, using single quotes will result in my parameters not being interpolated as showed in the below picture:
Thus, I can't find a solution which allows my to have both, and as I see it, this leaves me with one of the following options:
I can choose to use single quotes, which result in my credentials
being masked but my parameters are not interpolated.
I can choose to use double quotes however, my username and password
are no longer masked but my parameters are being interpolated.
Do someone know if it is possible to have both in the same string or know some sort of workaround?
You can use double quotes, but escape the $ for the secret variables like this:
sh("curl -u \$USERNAME:\$PASSWORD ${url}")
You can also use single quotes:
sh('curl -u $USERNAME:$PASSWORD ' + url)
You can use it with withCredentials().

Ruby on Rails: How may create a quoted string to a request header?

I am writing from scratch a Twitter client and one of the requisites is not using Twitter gems, so I must create my own requests.
Twitter API documentation says here that I must have a Authorization header like this:
Authorization:
OAuth oauth_consumer_key="xvz1evFS4wEEPTGEFPHBog",
oauth_nonce="kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg",
oauth_signature="tnnArxj06cWHq44gCs1OSKk%2FjLY%3D",
oauth_signature_method="HMAC-SHA1",
oauth_timestamp="1318622958",
oauth_token="370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb",
oauth_version="1.0"
As you may see I must have something like oauth_consumer_key="xvz1evFS4wEEPTGEFPHBog" with the second part in quotes. I tried using %Q like in
["oauth_consumer_key",%Q( Figaro.env.TWITTER_ACCESS_TOKEN )].join('=')
assuming %Q would return a quoted string. but when I inspect the value, all I get is
oauth_consumer_key=xvz1evFS4wEEPTGEFPHBog
which, obviously, is different from the required result.
What am I missing?
1. My solution:
'oauth_consumer_key="#{Figaro.env.TWITTER_ACCESS_TOKEN}"'
2. Why:
%Q() basically replaces the variable with double quotes "", it is literally the same as if you wrote "Figaro.env.TWITTER_ACCESS_TOKEN"
In fact, to display the content of a variable, you have to use interpolation
instead of the name itself, using "#{var}".
You can also use %Q directly with interpolation using %Q{var} (note {} instead of ()).
Your problem is elsewhere: with the join() method. It's getting rid of double quotes. In that case doing ["var", var].join('=') and ["var", %Q{var}].join('=') ends doing exactly the same thing but more complicated.
#Artem Ignatiev's solution should works. But if you need to be more readable, you don't even need to join, imho, it makes sense only when you are using more than two variables.
For instance, I will use something like 'oauth_consumer_key="#{var}"' mixing single and double quote to make sure it causes no confusions.
If you do need to use %Q() and join, you can still use ["oauth_consumer_key", %Q("#{Figaro.env.TWITTER_ACCESS_TOKEN})].join('=').
Note that because of the join you can use single quote interpolation %q or double quote interpolation %Q without affecting the ends results:
e.g
['oauth_consumer_key', %q("#{var}")].join('=') == ["oauth_consumer_key", %Q("#{var}")].join('=')
%Q(x) is basically the same as "x".
To achieve the desired result you have to manually introduce quotes into %Q expression, like this: %Q("#{Figaro.env.TWITTER_ACCESS_TOKEN}")

How to disable string conversion in Swift?

I have a encrypted string which would be passed from the server side, now I want to test to convert it into readable language by some conventional decoding method.
but I found I totally cannot use the string:
The error shows: invalid escape sequence in literal.
There exists some conversions in swift string like "\(variable)" or "\b".
Is there a way for me to use pure String?
For example. in python, I can declare a = """content""" to represent pure String
It's the backslash (\), just before the character the up-arrow is pointing to in the error message. In a literal, this needs to be represented by a double backslash (\\).
This issue won't arise once you're no longer testing and you're doing this all with actual values; it's a feature only of literal strings.
I recently fount a quick solution:
Use ' content ' instead of " content ", then Xcode would give you a warning.
Press Fix-it, Xcode would automatically add \ at right places to avoid literal convention.
I would suggest you save the string in a file in you app and read it from there, to avoid having to modify the string (it's a rather ugly string and you will have to escape a bunch of stuff).
You can use NSBundle.pathForResource and NSString.initWithContentsOfFile to get the string into memory from the file.

How to add prefix and remove double quotes from the value in ruby on rails?

I have a value called "FooBar". I want to replace this text with the quotes to Enr::Rds::FooBar without quotes.
Update:
For example, #question.answer_model gives the value "FooBar" (with the quotes)
I am a newbie and somebody please refer me how would i start to know about regex? What is the best way to practice in online?
Since you want to: a) drop the quotes and b) prepend FooBar with Enr::Rds::, I would suggest you preform exactly what is intended, literally:
'"FooBar"'.delete('"').gsub(/\A/, "Enr::Rds::")
# => "Enr::Rds::FooBar"
I think you are trying to convert a string to a constant. try the following
"Enr::Rds::#{#question.answer_model.gsub('"', '')}".constantize.find(...)

Ruby: Add a variable to translation key

I need to add a variable in a translation key in a view of a Ruby on Rails project (not in the value, in the key). Ex, this is my key:
= t 'services.categories.website_html'
What I need to do is that the word "website" from that key comes from a variable named "category.className"
I have tryed this, with no results:
= t 'services.categories.#{category.className}_html'
Thanks in advance.
Use double quotes instead of simple quotes ;)
= t "services.categories.#{category.className}_html"
Strings are not interpolated inside single quotes, but they are in double quotes.

Resources