Printing printing variable in single quoted string Ruby - ruby

I am attempting to send a tweet to twitter using the twitter_oauth gem with the following code:
client.update('.# #{tweeter}, have a nice day!')
Because of the single quotes I cannot get the variable to display but the tweet will not send if single quote are not used. Does anyone have any suggestions as to how to get this to work? thanks

Just replace the ' with ", single quoted strings don't do variable substitution and the other neat things of double quoted strings. They exist because of those missing features they are faster to parse.
If the tweet doesn't work despite using " then the problem is likely that the variable tweeter contains characters that are not allowed or in some other way invalid (maybe requiring some sort of escaping, e.g. URL or XML escaping).

Have you tried the old, java-esque way:
client.update('.# ' + tweeter + ', have a nice day!')
Or using a temporary variable:
message = ".# #{tweeter}, have a nice day!"
client.update(message)

Related

How to delete quotation mark in text file printed

I'm honestly a novice on scilab.
I'm using print function to create .txt file with my character matrix in it.
But , when I open txt file, double quote appeared. I just want words without "".
This is how I'm using print
Compterendu(1,1)= "Medecin demandeur: "
fileresname= fullfile(RES_PATH, "compterendu.txt")
print(fileresname,Compterendu)
And, compterendu.txt was printed out like this.
Would be so grateful for any help!!
Thanks
Why do you use "print" ? After looking into the doc, yes, it is used to produce the same text as when you type the expression or the variable name on the command line. Hence it does print double quotes for strings. If you need something more basic use lower level i/o commands, like mputl.
S.

Changing escape character for quotes

I am trying to read a CSV file which contains escaped quote values such as:
"1","unquoted text","\"quoted text\""
It seems that SuperCSV wants quotes to be quoted as
"1","unquoted text","""quoted text"""
Is there a way to change the escape character to a backslash? I've looked at the docs and not seen anything.
Just found a link to an issue logged in github: https://github.com/super-csv/super-csv/issues/14
Seems like a different CSV handler is in order.

How can I write an array to a file with quotes around every value?

I'm trying to write an array to a CSV file. In the past, I've just used:
my_array.to_csv
and quotes be damned. But I have to conform on this particular file I'm writing to the CSV "standard". That means that, where this:
a,b,c,"d, with a comma",e
was satisfactory before, now I must output:
"a","b","d, with a comma","e"
There must be some easy way, but I can't find it.
I tried:
x.map{|v| '"' + v + '"'}.to_csv
but the file ended up with:
"""a""","""b""","""c"""
I've tried a bunch of variations on that. I ALWAYS end up with 3 quote characters.
The only thing that works is this:
fout.puts x.map{|v| "\"#{v}\"" }.to_csv.gsub('"""','"')
Which of course is hideous. Any help is appreciated!
Simply add an option:
my_array.to_csv(force_quotes:true)
You can check out more options here
better way follow
assuming a is array initially
a ="'" + a.join("','") + "'"

Escaping an apostrophe in golang

How can I escape an apostrophe in golang?
I have a string
s = "I've this book"
and I want to make it
s = "I\'ve this book"
How to achieve this?
Thanks in advance.
Escaping a character is only necessary if it can be interpreted in two or more ways. The apostrophe in your string can only be interpreted as an apostrophe, escaping is therefore not necessary as such. This is probably why you see the error message unknown escape sequence: '.
If you need to escape the apostrophe because it is inserted into a database, first consider using library functions for escaping or inserting data directly. Correct escaping has been the culprit of many security problems in the last decades. You will almost certainly do it wrong.
Having said that, you have to escape \ to do what you want (click to play):
fmt.Println("\\'") # outputs \'
As you're using cassandra, you can use packages like gocql which provide you with parametrized queries:
session.Query(`INSERT INTO sometable (text) VALUES (?)`, "'escaping'").Exec();

Problem With Regular Expression to Remove HTML Tags

In my Ruby app, I've used the following method and regular expression to remove all HTML tags from a string:
str.gsub(/<\/?[^>]*>/,"")
This regular expression did just about all I was expecting it to, except it caused all quotation marks to be transformed into “
and all single quotes to be changed to ”
.
What's the obvious thing I'm missing to convert the messy codes back into their proper characters?
Edit: The problem occurs with or without the Regular Expression, so it's clear my problem has nothing to do with it. My question now is how to deal with this formatting error and correct it. Thanks!
Use CGI::unescapeHTML after you perform your regular expression substitution:
CGI::unescapeHTML(str.gsub(/<\/?[^>]*>/,""))
See http://www.ruby-doc.org/core/classes/CGI.html#M000547
In the above code snippet, gsub removes all HTML tags. Then, unescapeHTML() reverts all HTML entities (such as <, &#8220) to their actual characters (<, quotes, etc.)
With respect to another post on this page, note that you will never ever be passed HTML such as
<tag attribute="<value>">2 + 3 < 6</tag>
(which is invalid HTML); what you may receive is, instead:
<tag attribute="<value>">2 + 3 < 6</tag>
The call to gsub will transform the above to:
2 + 3 < 6
And unescapeHTML will finish the job:
2 + 3 < 6
You're going to run into more trouble when you see something like:
<doohickey name="<foobar>">
You'll want to apply something like:
gsub(/<[^<>]*>/, "")
...for as long as the pattern matches.
This regular expression did just about
all I was expecting it to, except it
caused all quotation marks to be
transformed into “ and all
single quotes to be changed to ”
.
This doesn't sound as if the RegExp would be doing this. Are you sure it's different before?
See this question here for information about the problem, it has got an excellent answer:
Get non UTF-8 form fields as UTF-8 in php.
I've run into a similar problem with character changes, this happened when my code ran through another module that enforced UTF-8 encoding and then when it came back, I had a different file (slurped array of lines) on my hands.
You could use a multi-pass system to get the results you are looking for.
After running your regular expression, run an expression to convert &8220; to quotes and another to convert &8221; to single quotes.

Resources