I am trying to eval the expression:
eval "\"1 pages\".gsub(/\D/,'')"
It always retrieves '1 pages'
while I expect only the string '1'
.
Why does eval
ignore gsub
?
I am trying to eval the expression:
eval "\"1 pages\".gsub(/\D/,'')"
It always retrieves '1 pages'
while I expect only the string '1'
.
Why does eval
ignore gsub
?
You need to escape the backslash since you're in an interpolated string:
eval "\"1 pages\".gsub(/\\D/, '')"
Or just don't use double-quotes since you don't need interpolation:
eval '"1 pages".gsub(/\D/, "")'
Why does
eval
ignoregsub
?
It does not. It may look so to you because \D
has no special meaning within double quotes, and hence it is interpreted as plain "D"
. And there is no "D"
within "1 pages"
.
You want to escape the D
as well, because that's part of the regex and not the character "D" alone:
eval "\"1 pages\".gsub(/\\D/,'')"