[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Backslashes in Command Line Arguments

Joseph Pecoraro

12/16/2007 5:27:00 AM

In writing a script that takes strings on the command line I have run
into a small problem which I don't see any way around. Here is a very
simplified program that just prints out the first command line argument,
and using p you can see exactly what is in the string.

[example.rb]
<code>p $*[0]</code>


Now when I send some different (but similar) strings I end up with the
same result in the ARGV array:

$ ruby example.rb "\a"
"\\a"

$ ruby example.rb "\\a"
"\\a"

$ ruby example.rb \\\a
"\\a"

1) Is this because I am running in bash and the shell maps the \\ into a
single backslash?
2) Is there any way around this in Ruby or from the command line (in
bash) so that I can differentiate between these?

Thanks,
Joseph Pecoraro
--
Posted via http://www.ruby-....

12 Answers

Sebastian Hungerecker

12/16/2007 9:33:00 AM

0

Joseph Pecoraro wrote:
> 1) Is this because I am running in bash and the shell maps the \\ into a
> single backslash?

Yes.

> 2) Is there any way around this in Ruby or from the command line (in
> bash) so that I can differentiate between these?

~> echo \~> echo "\\"
~> echo '\\'
\

HTH,
Sebastian
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Joseph Pecoraro

12/16/2007 3:09:00 PM

0

Sebastian Hungerecker wrote:
> Joseph Pecoraro wrote:
>> 2) Is there any way around this in Ruby or from the command line (in
>> bash) so that I can differentiate between these?
>
> ~> echo \> > ~> echo "\\"
> > ~> echo '\\'
> \>
>
> HTH,
> Sebastian

Sadly, passing those into example.rb to Ruby produces the same
undesirable results. Thanks for your help, I didn't know that feature
about single quoted strings in bash.

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

Sebastian Hungerecker

12/16/2007 3:23:00 PM

0

Joseph Pecoraro wrote:
> Sebastian Hungerecker wrote:
> > ~> echo '\\'
> > \>
> Sadly, passing those into example.rb to Ruby produces the same
> undesirable results.

It does?
~> echo "p ARGV[0]">t.rb

~> ruby t.rb '\'
"\\"

~> ruby t.rb '\\'
"\\\\"

~> ruby t.rb '\\\'
"\\\\\\"


--
NP: Graveworm - The Day I Die
Jabber: sepp2k@jabber.org
ICQ: 205544826

Joseph Pecoraro

12/16/2007 3:32:00 PM

0

You are absolutely right, I don't know what I was thinking.
Thanks!
--
Posted via http://www.ruby-....

Rick DeNatale

12/17/2007 4:06:00 PM

0

On 12/16/07, Joseph Pecoraro <joepeck02@gmail.com> wrote:
> In writing a script that takes strings on the command line I have run
> into a small problem which I don't see any way around. Here is a very
> simplified program that just prints out the first command line argument,
> and using p you can see exactly what is in the string.
>
> [example.rb]
> <code>p $*[0]</code>
>
>
> Now when I send some different (but similar) strings I end up with the
> same result in the ARGV array:
>
> $ ruby example.rb "\a"
> "\\a"
>
> $ ruby example.rb "\\a"
> "\\a"
>
> $ ruby example.rb \\\a
> "\\a"
>
> 1) Is this because I am running in bash and the shell maps the \\ into a
> single backslash?

No, it's because you are using p instead of puts.

The p method prints the result of sending the inspect message to its
argument, and String#inspect produces a what ruby string literal to
represent the string looks like, and since \ is the escape character
in ruby string literals it needs to be escaped itself in the output
of inspect.

--
Rick DeNatale

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

Joseph Pecoraro

12/17/2007 5:05:00 PM

0

> No, it's because you are using p instead of puts.
>
> The p method prints the result of sending the inspect message to its
> argument, and String#inspect produces a what ruby string literal to
> represent the string looks like, and since \ is the escape character
> in ruby string literals it needs to be escaped itself in the output
> of inspect.
>
> --
> Rick DeNatale

I purposely used p instead of puts for that exact reason. Which is how
I noticed that if you send "\n" Ruby does not get a newline character,
it gets a backslash n literal.

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

MonkeeSage

12/17/2007 5:37:00 PM

0

On Dec 17, 11:05 am, Joseph Pecoraro <joepec...@gmail.com> wrote:
> > No, it's because you are using p instead of puts.
>
> > The p method prints the result of sending the inspect message to its
> > argument, and String#inspect produces a what ruby string literal to
> > represent the string looks like, and since \ is the escape character
> > in ruby string literals it needs to be escaped itself in the output
> > of inspect.
>
> > --
> > Rick DeNatale
>
> I purposely used p instead of puts for that exact reason. Which is how
> I noticed that if you send "\n" Ruby does not get a newline character,
> it gets a backslash n literal.
>
> Joe
> --
> Posted viahttp://www.ruby-....

It does get a newline character...try looking at "\n"[0] (or if you're
using 1.9, "\n".ord)...you'll notice it's the ascii ordinal 10...aka
"newline". That was Robert's point. #inspect (and by proxy #p)
represent non-printing characters as their escaped symbols so that you
can actually see them. Try entering this in irb and notice the
difference betwen the input string and that printed by #p...

p "hello\x09there"

Regards,
Jordan

MonkeeSage

12/17/2007 5:39:00 PM

0

On Dec 17, 11:36 am, MonkeeSage <MonkeeS...@gmail.com> wrote:
> ...That was Robert's point.

Doh! Sorry Rick...brain said one thing, fingers rebelled.

Joseph Pecoraro

12/17/2007 5:52:00 PM

0

> It does get a newline character...try looking at "\n"[0] (or if you're
> using 1.9, "\n".ord)...you'll notice it's the ascii ordinal 10...aka
> "newline". That was Robert's point. #inspect (and by proxy #p)
> represent non-printing characters as their escaped symbols so that you
> can actually see them. Try entering this in irb and notice the
> difference betwen the input string and that printed by #p...
>

It seems both of you have missed the point of my question. The input is
coming into Ruby from the Command Line.

Of course "\n"[0] is going to be a newline... IT is a newline. But if
you read the above posts you will see what I was talking about:

$ ruby example.rb "\n"

Does not result in YOUR "\n", it results in "\\n" which was part of MY
problem, which Sebastian showed me how to solve.

I know I have the literal string referring to a Backslash N and not a
Newline because I used #p...

"\n" #=> newline
"\\n" #=> backslash n
--
Posted via http://www.ruby-....

MonkeeSage

12/17/2007 6:25:00 PM

0

On Dec 17, 11:51 am, Joseph Pecoraro <joepec...@gmail.com> wrote:
> > It does get a newline character...try looking at "\n"[0] (or if you're
> > using 1.9, "\n".ord)...you'll notice it's the ascii ordinal 10...aka
> > "newline". That was Robert's point. #inspect (and by proxy #p)
> > represent non-printing characters as their escaped symbols so that you
> > can actually see them. Try entering this in irb and notice the
> > difference betwen the input string and that printed by #p...
>
> It seems both of you have missed the point of my question. The input is
> coming into Ruby from the Command Line.
>
> Of course "\n"[0] is going to be a newline... IT is a newline. But if
> you read the above posts you will see what I was talking about:
>
> $ ruby example.rb "\n"
>
> Does not result in YOUR "\n", it results in "\\n" which was part of MY
> problem, which Sebastian showed me how to solve.
>
> I know I have the literal string referring to a Backslash N and not a
> Newline because I used #p...
>
> "\n" #=> newline
> "\\n" #=> backslash n
> --
> Posted viahttp://www.ruby-....

Ah-ha. I understood your original problem, but I was confused about
your reasoning for using #p...by 'send "\n"' in your previous post I
(mis-)understood 'type "\n" in a script', and thought you were saying
that you used #p because you wanted to see whether ruby would output a
literal newline or keep the two-byte escape sequence. Sorry about
that!

Regards,
Jordan