Is it possible to have a string with a single backslash?
Indeed.
s = “\foo”
p s
=> “\foo”
Hi, I’m very sorry for my bad example. This problem only occurs when I
need the values “\d” and “\l” on the strings. I need to be able to
compare this value to see if a string has it. (e.g.: if var = “\d” do
this…)
When I do something like:
var = “\d”
I get
“d”
But when I do:
var = “\d”
I get
“\d”
And when I do:
var = ‘\d’
I get
“\d”
Again, sorry for my bad example. I hope there’s a solution for this.
Hi, I’m very sorry for my bad example. This problem only occurs when I
need the values “\d” and “\l” on the strings. I need to be able to
compare this value to see if a string has it. (e.g.: if var = “\d” do
this…)
When I do something like:
var = “\d”
I get
“d”
But when I do:
var = “\d”
I get
“\d”
And when I do:
var = ‘\d’
I get
“\d”
Again, sorry for my bad example. I hope there’s a solution for this.
How about posting the actual code you use to get the bad result and what
you want as a good result with it?
I even tried to remove one of the backslashes from the “\foo” string,
but then I just get “foo”
I can’t reproduce your results exactly, but I think I can explain what
you’re seeing. It’s a combination of things.
First, there’s what IRB is showing you when you make an entry vs.
what
it’s actually going to provide when queried.
irb(main):001:0> var = ‘\foo’
=> “\foo”
irb(main):002:0> puts var
\foo
=> nil
irb(main):003:0> puts var.length
4
=> nil
The feedback at 0001:0 above is telling you that Ruby is storing the
backslash character you entered as an actual character vs. treating it
as an
escape character. You can see that when you do the puts. You can also
see
it in the length of the string.
Second, there’s the difference in how Ruby treats single vs double
quotes.
irb(main):004:0> var = “\foo”
=> “\foo”
irb(main):005:0> puts var
♀oo
=> nil
irb(main):006:0> puts var.length
3
=> nil
Double quotes tell Ruby to try to interpret what’s inside. In this case
it’s interpreting the first two characters as an escape sequence. This
is
the part of what you say you’re seeing that I can’t reproduce. On
Windows,
at 005:0 I get the above. On Linux I get a non-displayable character
rendered. In both cases the var.length is 3.
You can use double quotes, but you have to explicitly tell Ruby that the
backslash doesn’t signal an escape sequence by escaping it.
irb(main):007:0> var = “\foo”
=> “\foo”
irb(main):008:0> puts var
\foo
=> nil
irb(main):009:0> puts var.length
4
I even tried to remove one of the backslashes from the “\foo” string,
but then I just get “foo”
Thanks for reading, and I hope you can help me out
I just have to point out (since I don’t think anyone else did) that
“\f” is a single character string: \f is an escape code for a form-
feed (ASCII 0x0C)
You’re confusing the literal syntax for a backslash followed by a
letter with the actual contents of the string: “\f”.size == 2
Again we need clarification. Are you trying to match a decimal value
i.e.
[0…9] or are you trying to match the literal string “\d”?
If you are tyring to match the literal string then you want to look for
“\d” (you MUST escape the backslash hence the double backslash). If
you
are trying to find a decimal character they try =~ /\d/
–
“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”
-Greg Graffin (Bad Religion)
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.