[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

String escape mystery.

Rick DeNatale

11/17/2007 8:09:00 PM

I can't figure out what's happening here:

shadowfax:~ rick$ ruby -ve'p "a\b\c"'
ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.10.2]
-e:1: unterminated string meets end of file

shadowfax:~ rick$ ruby -e'p "a\b\\c"'
"a\b\\c"

shadowfax:~ rick$ ruby -e'puts "a\b\\c"'
\c
shadowfax:~ rick$

What's up with \c in a string literal?

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

5 Answers

Wolfgang Nádasi-donner

11/17/2007 8:22:00 PM

0

Rick Denatale wrote:
> What's up with \c in a string literal?

"\cx" and "\C-c" mean both "Control-c".

Wolfgang Nádasi-Donner
--
Posted via http://www.ruby-....

Wolfgang Nádasi-donner

11/17/2007 8:23:00 PM

0

Wolfgang Nádasi-Donner wrote:
> Rick Denatale wrote:
>> What's up with \c in a string literal?
>
> "\cx" and "\C-c" mean both "Control-c".
>
> Wolfgang Nádasi-Donner

Sorry, typo -> "\cc" and "\C-c" mean both "Control-c".

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

Stefan Rusterholz

11/17/2007 10:52:00 PM

0

Rick Denatale wrote:
> I can't figure out what's happening here:
>
> shadowfax:~ rick$ ruby -ve'p "a\b\c"'
> ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.10.2]
> -e:1: unterminated string meets end of file
>
> shadowfax:~ rick$ ruby -e'p "a\b\\c"'
> "a\b\\c"
>
> shadowfax:~ rick$ ruby -e'puts "a\b\\c"'
> \c
> shadowfax:~ rick$
>
> What's up with \c in a string literal?
>
> --
> Rick DeNatale
>
> My blog on Ruby
> http://talklikeaduck.denh...

Hu?
I fail to see what exactly is your problem. \b is backspace, so the a
becomes deleted in the output. In the inspect of course it still shows
up. \\ is a literal \ and that is followed by a c, so "\\c" naturally
prints as \c
Did I miss your point or so?

Regards
Stefan

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

Rick DeNatale

11/17/2007 10:59:00 PM

0

On Nov 17, 2007 5:52 PM, Stefan Rusterholz <apeiros@gmx.net> wrote:
>
> Rick Denatale wrote:
> > I can't figure out what's happening here:
> >
> > shadowfax:~ rick$ ruby -ve'p "a\b\c"'
> > ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.10.2]
> > -e:1: unterminated string meets end of file
> >
> > shadowfax:~ rick$ ruby -e'p "a\b\\c"'
> > "a\b\\c"
> >
> > shadowfax:~ rick$ ruby -e'puts "a\b\\c"'
> > \c
> > shadowfax:~ rick$
> >
> > What's up with \c in a string literal?
> >
> > --
> > Rick DeNatale
> >
> > My blog on Ruby
> > http://talklikeaduck.denh...
>
> Hu?
> I fail to see what exactly is your problem. \b is backspace, so the a
> becomes deleted in the output. In the inspect of course it still shows
> up. \\ is a literal \ and that is followed by a c, so "\\c" naturally
> prints as \c
> Did I miss your point or so?


No it was just a brain fart on my part.


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Wolfgang Nádasi-donner

11/17/2007 11:14:00 PM

0

Rick Denatale wrote:
> On Nov 17, 2007 5:52 PM, Stefan Rusterholz <apeiros@gmx.net> wrote:
>> >
>> > http://talklikeaduck.denh...
>>
>> Hu?
>> I fail to see what exactly is your problem. \b is backspace, so the a
>> becomes deleted in the output. In the inspect of course it still shows
>> up. \\ is a literal \ and that is followed by a c, so "\\c" naturally
>> prints as \c
>> Did I miss your point or so?
>
>
> No it was just a brain fart on my part.
>
>
> --
> Rick DeNatale
>
> My blog on Ruby
> http://talklikeaduck.denh...

Please take a look at...

irb(main):001:0> "a\b\c""
=> "a\b\002"

...and the documentation in
http://www.ruby-doc.org/docs/ProgrammingRuby/html/lan... in
"Basic Types", subchapter "Strings" is a table...

Substitutions in double-quoted strings

\a Bell/alert (0x07) \nnn Octal nnn
\b Backspace (0x08) \xnn Hex nn
\e Escape (0x1b) \cx Control-x
\f Formfeed (0x0c) \C-x Control-x
\n Newline (0x0a) \M-x Meta-x
\r Return (0x0d) \M-\C-x Meta-control-x
\s Space (0x20) \x x
\t Tab (0x09) #{expr} Value of expr
\v Vertical tab (0x0b)

...which explains everything in detail.

Wolfgang Nádasi-Donner
--
Posted via http://www.ruby-....