Single backslash in a string. How?

Is it possible to have a string with a single backslash?

I want var to have the value “\foo”

And when I do something like:

var = “\foo”
I get
“foo”

But when I do:

var = “\foo”
I get
“\foo”

And when I do:

var = ‘\foo’
I get
“\foo”

I even tried to remove one of the backslashes from the “\foo” string,
but then I just get “foo” :frowning:

Thanks for reading, and I hope you can help me out

Rafael V. wrote:

Is it possible to have a string with a single backslash?

I want var to have the value “\foo”

And when I do something like:

var = “\foo”
I get
“foo”

But when I do:

var = “\foo”
I get
“\foo”

And when I do:

var = ‘\foo’
I get
“\foo”

I even tried to remove one of the backslashes from the “\foo” string,
but then I just get “foo” :frowning:

Thanks for reading, and I hope you can help me out

use double quotes don’t use single quotes man
like var="\foo"

var="\foo"
u will get
“\foo”

var = “\foo”
u will get
“\foo”

Rafael V. wrote:

Is it possible to have a string with a single backslash?

Indeed.

s = “\foo”
p s

=> “\foo”

Lloyd L. wrote:

Rafael V. wrote:

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.

On Mon, Sep 8, 2008 at 7:36 AM, Rafael V. [email protected]
wrote:

=> “\foo”
var = “\d”

var = ‘\d’
I get
“\d”

Again, sorry for my bad example. I hope there’s a solution for this.

Posted via http://www.ruby-forum.com/.

If you are searching for the literal \d string then “\d” is what you
want.
You have to escape that \ try puts “\d” and see what you get.


“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)

Rafael V. wrote:

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?

Hi Rafael,

Rafael V. wrote:

But when I do:

I even tried to remove one of the backslashes from the “\foo” string,
but then I just get “foo” :frowning:

I can’t reproduce your results exactly, but I think I can explain what
you’re seeing. It’s a combination of things.

  1. 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.

  1. 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

HTH,
Bill

Lloyd L. wrote:

How about posting the actual code you use to get the bad result and what
you want as a good result with it?

The problem is simple

I want to be able to tell if a variable contains the value “\d”, but I
can’t seem to be able to represent this value with ruby.

This problem only occurs with the letters “d” and “l”

try “\d” and “\d” in irb and you’ll understand what I mean

Rafael V. wrote:

I want to be able to tell if a variable contains the value “\d”

What is the value “\d”? If your answer is “a (literal) backslash,
followed by
the letter d”, then that’s “\d” (or ‘\d’ if you will).
Try this in irb:

“\d” == ‘\d’
=> true

puts “\d”
\d
=> nil

“\s” == ‘\s’
=> false

“\s” == ’ ’
=> true

puts “\s”

=> nil

puts ‘\s’
\s
=> nil

puts “\s”
\s
=> nil

“\s” == ‘\s’
=> true

HTH,
Sebastian

On Sep 8, 2008, at 9:15 AM, Rafael V. wrote:

But when I do:

I even tried to remove one of the backslashes from the “\foo” string,
but then I just get “foo” :frowning:

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

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

On Mon, Sep 8, 2008 at 9:18 AM, Rafael V. [email protected]
wrote:

try “\d” and “\d” in irb and you’ll understand what I mean

Posted via http://www.ruby-forum.com/.

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)