Ruby strange quoting - ruby

sorry for bad English. Why Ruby quoting so strange? Or may be this is a bug?
irb(main):027:0> p eval "\" \+ \\+ \\\+ \\\\+ \\\\\+ \""
produces
=> " + + + \\+ \\+ "
or
irb(main):027:0> puts eval "\" \+ \\+ \\\+ \\\\+ \\\\\+ \""
produces
=> + + + \+ \+
or another example
irb(main):067:0> " \" " =~ Regexp.new(eval("\" \\\" \""))
=> 0
irb(main):068:0> " + " =~ Regexp.new(eval("\" \\\\+ \""))
=> 0

When you write \" \+ \\+ \\\+ \\\\+ \" you get " + \+ \+ \\+ ". After, you use eval to execute this string, that contains another double-quoted string. You get, then, + + + \+.
\\ => \
\x => x (se não for nenhum caso especial, como \n)

ruby escaping is perfectly good,
eval = evaluate/execute the string

Related

How to describe a quoted string in EBNF

How do I describe a quoted string (like in C, Java, etc) in EBNF notation?
I was thinking of this (see below), but the AnyCharacter part will also match the double quotes (").
QuotedString = '"' AnyCharacter* '"' ;
In other words, how do I match all characters except the double quote character ("), but still allow escapes (/")?
You could do something like
string = " printable-chars | nested-quotes "
where
printable chars = letter | digit | ~ # # % _ $ & ' - + /
where
letter = A..Z | a..z | extended ascii
and
digit = 0..9
I think you've got the general idea

Ruby - How to remove space after some characters?

I need to remove white spaces after some characters, not all of them. I want to remove whites spaces after these chars: I,R,P,O. How can I do it?
"I ".gsub(/(?<=[IRPO]) /, "") # => "I"
"A ".gsub(/(?<=[IRPO]) /, "") # => "A "
" P $ R 3I&".gsub(/([IRPO])\s+/,'\1')
#=> " P$ R3I&"

Ruby escaping a set of special characters in a string

I have a set of special characters for Elasticsearch that I need to escape with Ruby.
They are: + - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /
How can I get any string to escape any of these characters?
Thanks
The problem as it is stated has no solution, because “escaping two subsequent characters” makes no sense. What result do you expect to receive “escaping”, say, &&?
I believe, you want to escape all single characters, so that && becomes \&\& and || — \|\|. That is easy.
to_escape = %w_+ - = & | > < ! ( ) { } [ ] ^ " ~ * ? : \ /_ # C"mon, SO parser
re = Regexp.union(to_escape)
print 'str (f) | a || b'.gsub(re) { |m| "\\#{m}" }
#⇒ str \(f\) \| a \|\| b
Another possibility would be to use Regexp#escape, but it will escape more, than you probably need (e. g. spaces.)
This is a variation on #mudasobwa's answer, using the form of String#gsub that uses a hash for replacements:
escapees = %w$ + - = & | > < ! ( ) { } [ ] ^ " ~ * ? : \ / $
#=> ["+", "-", "=", "&", "|", ">", "<", "!", "(", ")", "{", "}",
# "[", "]", "^", "\"", "~", "*", "?", ":", " /"]
h = escapees.each_with_object({}) { |c,h| h[c] = "\\#{c}" }
#=> {"+"=>"\\+", "-"=>"\\-",..., " /"=>"\\ /"}
h.default_proc = ->(h,k) { k }
If the hash h does not have a key k, Hash#default_proc= causes the h[k] to to return k.
s = 'str (f) | a || b'
ss = s.gsub(/./,h)
#=> "str \\(f\\) \\| a \\|\\| b"
puts ss
#=> str \(f\) \| a \|\| b

Add multiple consecutive whitespaces in a string replace operation

g.e. I would like to prepend all occurrences of the string "foo" with three spaces:
value.replace(/(foo)/, " " + "$1")
value.replace(/(foo)/, " $1")
value.replace(/(foo)/, " " + " " + " $1")
all return
foo
instead of
foo
Did you try the follow GREL expression value.replace('foo', ' foo') ?

Shorter way to remove non-characters than gsub(/\d|\W/, "")

my_string = 'Here's the #: 49848! - but will dashes, commas & stars (*) show?'
puts src.gsub(/\d|\W/, "")
i.e. can I remove the or ("|").
Here's how I got here, can I get shorter?
src = "Here's the #: 49848! - but will dashes, commas & stars (*) show?"
puts "A) - " + src
puts "B) - " + src.gsub(/\d\s?/, "")
puts "C) - " + src.gsub(/\W\s?/, "")
puts "D) - " + src.gsub(/\d|\W\s?/, "")
puts "E) - " + src.gsub(/\d|\W/, "")
puts "F) - " + src
A) - Here's the #: 49848! - but will dashes, commas & stars (*) show?
B) - Here's the #: ! - but will dashes, commas & stars (*) show?
C) - Heresthe49848butwilldashescommasstarsshow
D) - Heresthebutwilldashescommasstarsshow
E) - Heresthebutwilldashescommasstarsshow
F) - Here's the #: 49848! - but will dashes, commas & stars (*) show?
n.d. D) and E) are what I want for output. Just characters.
my_string = "Here's the #: 49848! - but will dashes, commas & stars (*) show?"
p my_string.delete('^a-zA-Z')
#=>"Heresthebutwilldashescommasstarsshow"
I have this one
src.gsub(/[^a-z]/i, "")
also not shorter, but better to read in my opinion.
The i modifier makes the regex case independent, so that a-z matches also A-Z. A small difference is that this regex will also replace _ which is not replaced by yours.
If you want to keep also unicode letters, use this one:
/\PL/
This matches all non letter character.

Resources