I'm trying to minify some inline JSON as part of my HTML minifier. How do I make this:
> {"#context": "http://schema.org", "#type": "WebSite"} <
Into this:
>{ "#context": "http://schema.org", "#type": "WebSite" }<
I've tried gsub[/\", \s+\"/, ", "], gsub[/"\"}"/, "\" }"] and gsub[/"\"}"/, "\" }"] but that errors out.
syntax error, unexpected [, expecting ']' (SyntaxError)
[/"\"}"/, "\" }"]
^
syntax error, unexpected ',', expecting keyword_end
syntax error, unexpected ',', expecting keyword_end
syntax error, unexpected ']', expecting keyword_end
UPDATE
I've now also tried these but to no good:
[/>\s+{/, ">{ "] # > { => >{
[/}\s+>/, " }<"] # } < => }<
[%r/{"/, '{ %r/"'] # {" => { "
[%r/"}/, '%r/" }'] # "} => " }
[%r/",\s+/, ", "] # , " => , "
Resulting in:
syntax error, unexpected [, expecting ']' (SyntaxError)
[/}\s+>/, " }<"] # } < => }<
^
I would suggest a different approach:
require JSON
str = '{"#context": "http://schema.org", "#type": "WebSite"}'
new_str = JSON.parse(str).to_json
puts new_str
> {"#context":"http://schema.org","#type":"WebSite"}
Use a %r Regexp literal to escape all characters but one :
a = '{"#context": "http://schema.org", "#type": "WebSite"}'
a.gsub(%r/{"/, '{ "').gsub(%r/"}/, '" }').gsub(/\s+/, ' ')
#=> { "#context": "http://schema.org", "#type": "WebSite" }
using %r{ ... } escapes all characters but { and },same goes for /, (, etc ...
credit : Escaping '“' with regular double quotes using Ruby regex
It will escapes double quotes
gsub(/("|")/, 34.chr)
Related
I do the following to add spaces after proper end-of-sentence punctuation:
string = '"something is wrong." but then, "why?" '
new_string = string.squeeze(" ").gsub(/([.?!"]) */,'\1 ')
puts new_string
=> " something is wrong. " but then, " why? "
My desired result is this:
"something is wrong." but then, "why?"
If something is after " to not add spaces unless it's ." in which case, add the same two spaces I would after a regular .
On the example string you give, I can get the desired result with
2.2.0 (main):0 > puts string.squeeze.strip
"something is wrong." but then, "why?"
I would like to run 2 commands in ruby but only if the first one succeeds.
In bash I would use && operator. I have tried this one and and keyword but && has thrown an error and and operator didn't works as expected.
The example I want to use it for:
#!/usr/bin/ruby
#
puts "asd" and puts "xxx"
executed as:
$ ./asd.rb
asd
The keyword and has lower precedence than &&. Both use short-circuit evaluation.
First, note that puts always returns nil. In ruby, nil is falsey.
2.2.0 :002 > puts "asdf"
asdf
=> nil
Now we try your example:
2.2.0 :002 > puts "asd" and puts "xxx"
asd
=> nil
This is the same as:
puts("asd") && puts("xxx")
asd
=> nil
In both cases puts "asd" and puts("asd") return nil so puts "xxx" and puts("xxx") are never evaulated because nil is falsey and there is short-circuit evaulation being used.
You also tried puts "asd" && puts "xxx", but this is a syntax error because of the higher precendence of the && operator.
puts "asd" && puts "xxx"
SyntaxError: (irb):3: syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '('
puts "asd" && puts "xxx"
^
That's because puts "asd" && puts "xxx" is the same as puts("asd" && puts) "xxx".
2.2.0 :012 > puts("asd" && puts) "xxx"
SyntaxError: (irb):12: syntax error, unexpected tSTRING_BEG, expecting end-of-input
puts("asd" && puts) "xxx"
^
See also: this related post
I've been racking my brain on this one and can't seem to figure it out. I don't see any extra quotes anywhere. Am I running into an issue with parsing the quotes?
The string starting:
At C:\scripts\365-export.ps1:288 char
:51
+ $execute = read-host -Prompt "Are you Sure?: (y/n) <<<< "
is missing the terminator: ".
At C:\scripts\365-export.ps1:297 char
:9
+ MainMenu <<<<
+ CategoryInfo : ParserError: (
if ($execute ...u}
}
MainMenu:String) [], ParseException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
Here are lines 288 through 297
$execute = read-host -Prompt "Are you Sure?: (y/n)"
if ($execute -ieq y) {New-MailboxSearch $searchname $endD $estimate $excludedupes $force $iph $recipient $keyword $sender $sourcebox $startD $statusmail $targetbox
} else {SConfMenu}
#END OF COMMAND EXECUTION
} else {
MainMenu}
}
MainMenu
I'm not sure if this will help but I've placed the script in pastebin here:
http://pastebin.ca/2532441
Any help is appreciated.
After adding the recommended quotes at ($execute -ieq "y"), I'm receiving this error now...
The string starting:
At C:\scripts\365-export.ps1:289 char
:21
+ if ($execute -ieq "y <<<< ") {New-MailboxSearch $searchname $endD $estimate $
excludedupes $force $iph $recipient $keyword $sender $sourcebox $startD $statu
smail $targetbox
is missing the terminator: ".
missing " # ($execute -ieq y)
if ($execute -ieq "*y"*)
The issue was found earlier in the code.
There was a problem with a variable that needed to be quoted with single quotes instead of double quotes.
Why is this a syntax error in ruby?
#!/usr/bin/ruby
servers = [
"xyz1-3-l"
, "xyz1-2-l"
, "dws-zxy-l"
, "abcl"
]
hostname_input = ARGV[0]
hostname = hostname_input.gsub( /.example.com/, "" )
servers.each do |server|
if hostname == server then
puts "that's the one"
break
end
end
... when I execute this script I get this output ...
$ ./test.rb abc1
./test.rb:5: syntax error, unexpected ',', expecting ']'
, "xyz1-2-l"
^
./test.rb:6: syntax error, unexpected ',', expecting $end
, "dws-zxy-l"
^
... if I simply put everything on the same line its ok ...
$ cat test.rb
#!/usr/bin/ruby
servers = [ "xyz1-3-l" , "xyz1-2-l" , "dws-zxy-l" , "abcl" ]
hostname_input = ARGV[0]
hostname = hostname_input.gsub( /.example.com/, "" )
servers.each do |server|
if hostname == server then
puts "that's the one"
break
end
end
$ ./test.rb dws-zxy-l
that's the one
Look ma, no commas (or quotes):
servers = %W[
xyz1-3-l
xyz1-2-l
dws-zxy-l
abcl
]
# => ["xyz1-3-l", "xyz1-2-l", "dws-zxy-l", "abcl"]
Newlines are significant in Ruby. You need to put the comma at the end of the line or use a backslash before your newline to indicate that the line is continuing (of course, in that case, what's the point in moving the comma to the next line?).
It seems that if I put code in my ternary evaluation it fails, but placing true or false it works.
Here is my code:
>test = [nil]
=> [nil]
>test.any? ? puts "AAA" : puts "BBB"
SyntaxError: (irb):16: syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '('
test.any? ? puts "AAA" : puts "BBB"
^
(irb):16: syntax error, unexpected ':', expecting $end
test.any? ? puts "AAA" : puts "BBB"
>test.any? ? true : false
=> false
>test << 1
=> [nil, 1]
>test.any? ? true : false
=> true
>test.any? ? puts "AAA" : puts "BBB"
SyntaxError: (irb):14: syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '('
test.any? ? puts "AAA" : puts "BBB"
^
(irb):14: syntax error, unexpected ':', expecting $end
test.any? ? puts "AAA" : puts "BBB"
^
You need parentheses.
>> test.any? ? puts("AAA") : puts("BBB")
BBB
=> nil
You should avoid parentheseless call in inline functions.