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?
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.
···
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?
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.
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.
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
···
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.
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...
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
···
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-forum.com/.
No problem. Also, after re-reading my previous comment I noticed I
sounded a little bit rude, so I apologize for that. I didn't mean to be
rude. I often misuse CAPS for emphasis and it ends up looking like
yelling instead.
Thanks for everyone's help. I just updated my little script called "rr"
available here <a href="http://bogojoker.com">at my blog</a>. Its
rather amateur but I'll continue to improve it.