How to build parameters for System.cmd? - imagemagick

I want to use System.cmd run "convert" from ImageMagick, but I am having difficulty
System.cmd("convert", ["origin.jpg", "-fill", "black", "-pointsize", "12", "-gravity", "SouthWest", "-draw", "\"text +4,+4 'Test hello world!'\"", "output.jpg"])
The args -draw 's value is \"text +4,+4 'Test hello world!'\", but ImageMagick requires "text +4,+4 'Test hello world!'" Do not need to escape double quotes.
How can I do it?

You don't need the outer double quotes here with System.cmd/3. You need them when running the command from the shell because the argument contains spaces and without the outer double quotes the shell will split the whole thing on every space and end up passing the equivalent of ["text", "+4,+4", "Test hello world!"]. The following should work:
System.cmd(..., [..., "text +4,+4 'Test hello world!'", ...])

Related

Why do you use %w[] in rails?

Why would you ever use %w[] considering arrays in Rails are type-agnostic?
This is the most efficient way to define array of strings, because you don't have to use quotes and commas.
%w(abc def xyz)
Instead of
['abc', 'def', 'xyz']
Duplicate question of
http://stackoverflow.com/questions/1274675/what-does-warray-mean
http://stackoverflow.com/questions/5475830/what-is-the-w-thing-in-ruby
For more details you can follow https://simpleror.wordpress.com/2009/03/15/q-q-w-w-x-r-s/
These are the types of percent strings in ruby:
%w : Array of Strings
%i : Array of Symbols
%q : String
%r : Regular Expression
%s : Symbol
%x : Backtick (capture subshell result)
Let take some example
you have some set of characters which perform a paragraph like
Thanks for contributing an answer to Stack Overflow!
so when you try with
%w(Thanks for contributing an answer to Stack Overflow!)
Then you will get the output like
=> ["Thanks", "for", "contributing", "an", "answer", "to", "Stack", "Overflow!"]
if you will use some sets or words as a separate element in array so you should use \
lets take an example
%w(Thanks for contributing an answer to Stack\ Overflow!)
output would be
=> ["Thanks", "for", "contributing", "an", "answer", "to", "Stack Overflow!"]
Here ruby interpreter split the paragraph from spaces within the input. If you give \ after end of word so it merge next word with the that word and push as an string type element in array.
If can use like below
%w[2 4 5 6]
if you will use
%w("abc" "def")
then output would be
=> ["\"abc\"", "\"def\""]
%w(abc def xyz) is a shortcut for ["abc", "def","xyz"]. Meaning it's a notation to write an array of strings separated by spaces instead of commas and without quotes around them.

ruby : regex replace string beetween two char [ ]

i want to remove text in string beetween [ ]:
ex:
Hello everyone [hi hi], hello world [ha ha]
result:
Hello everyone, hello world
i use
string.gsub!(regex-here , ' ')
help me define a regex-here.
As I understood, you do not want leading/trailing spaces to be left in the result:
▶ str = "[ho ho] Hello everyone [hi hi], hello world [ha ha]"
▶ str.gsub /\s*\[.*?\]\s*/, ''
#⇒ "Hello everyone, hello world"
Try this:
\s*\[.*?\]
Demo
Explanation:
\[: matches ]
.*?: matches any character as few times as possible, expanding as needed

concatenate backslash in swift [duplicate]

This question already has answers here:
(Swift) how to print "\" character in a string?
(4 answers)
Closed 7 years ago.
How do you concatenate a backslash in swift?
i.e. "some string" + "\"
escaping the backslash gives me "some string\\" but I want "some string\"
Any ideas on how to accomplish this?
EDIT: I don't want to print out the string, I just want to concatenate the backslash. Escaping the backslash will store the string with two backslashes but I only want one.
EDIT 2: I think I figured it out. I used "\"" and that seems to work for me.
The double backslash solution is correct (see console output in my small sample)
Duplicate
(Swift) how to print "\" character in a string?
But..
If you're in a playground and type:
print("\\Hello, World")
... then yes both slashes and even a newline character appear in the results to the right as:
\\Hello, World\n
but that's not the same as actual output.
If you open the debug console (Cmd-Shit-Y), or click the tiny eyeball icon that shows you the output, you'll see that there's only one \ and the escape works as expected.
For concantenation & string interpolation you could do...
var string = "some string"
print("\(string)\\") //prints "some string\"
Or...
string +="\\"
print(string) //also prints "some string\"
Again, the right-column preview output is slightly different, so you have to look at the console. This is a somewhat confusing & annoying feature of Playgrounds. Not sure why they don't ensure both preview & console output are the same, or show the debug console by default.

How to ignore all the quotes and double quotes in a String in Ruby?

i would like to mock a web service response. The response is a XML, and contains both simple quotes and double quotes.
The response is pretty big, so here are my solutions:
trim the response to make it smaller and backslash the simple quotes for example
backslash the simple quotes for example
add the response to a file and parse it
But the thing is, I'd like to test a large response, and not create a resource test folder with a file. And as you can imagine, backslashing everything is long and boring.
I also tried the triple double quote, not working of course.
How would you do it?
You could use Ruby here documents.
xml = <<DOC
<xml>
<food attribute="soup">'eel'</food>
</xml>
DOC
use the %Q operator
a = :jed
%Q| "these double quotes are ignored" for as
long as you can type says #{a}
|
any start end delimiters work so if you are using tables in cucumber for example you can use backticks instead of pipes
→ irb
ruby-1.9.2-p0 > str = <<-STR
ruby-1.9.2-p0"> ' single quote
ruby-1.9.2-p0"> " double quote
ruby-1.9.2-p0"> STR
=> "' single quote\n" double quote\n"
ruby-1.9.2-p0 >

How do I escape #{ from string interpolation

I have a heredoc where I am using #{} to interpolate some other strings, but there is an instance where I also want to write the actual text #{some_ruby_stuff} in my heredoc, WITHOUT it being interpolated. Is there a way to escape the #{.
I've tried "\", but no luck. Although it escapes the #{}, it also includes the "\":
>> <<-END
#{RAILS_ENV} \#{RAILS_ENV}
END
=> " development \#{RAILS_ENV}\n"
For heredoc without having to hand-escape all your potential interpolations, you can use single-quote-style-heredoc. It works like this:
item = <<-'END'
#{code} stuff
whatever i want to say #{here}
END
I think the backslash-hash is just Ruby being helpful in some irb-only way.
>> a,b = 1,2 #=> [1, 2]
>> s = "#{a} \#{b}" #=> "1 \#{b}"
>> puts s #=> 1 #{b}
>> s.size #=> 6
So I think you already have the correct answer.
You can use ' quotes instead. Anything enclosed in them is not being interpolated.
Your solution with escaping # also works for me. Indeed Ruby interpreter shows
=> "\#{anything}"
but
> puts "\#{anything}"
#{anything}
=> nil
Your string includes exactly what you wanted, only p method shows it with escape characters. Actually, p method shows you, how string should be written to get exactly object represented by its parameter.

Resources