f



puts "\\".gsub("\\", "\\\\")

Hello, I have a mini-ruby quiz. Guess what this line of code writes to
the console, then try it for yourself:

puts "\\".gsub("\\", "\\\\")

Why is that so?

Martin
0
4/24/2008 9:53:37 AM
comp.lang.ruby 48886 articles. 0 followers. Post Follow

4 Replies
906 Views

Similar Articles

[PageSpeed] 12

From: martinus [mailto:martin.ankerl@gmail.com]=20
# Hello, I have a mini-ruby quiz. Guess what this line of code writes to
# the console, then try it for yourself:
# puts "\\".gsub("\\", "\\\\")

puts "\\".gsub("\\", "\\\\")
\
#=3D> nil

# Why is that so?

faq. escaping the escape in sub/gsub. search the archives.

maybe you want something like,=20

puts "\\".gsub("\\"){"\\\\"}
\\
#=3D> nil

ie, use block wc is a lot more handy.

kind regards -botp



0
botp (987)
4/24/2008 10:01:13 AM
* martinus <martin.ankerl@gmail.com> (11:53) schrieb:

> Hello, I have a mini-ruby quiz. Guess what this line of code writes to
> the console, then try it for yourself:
>
> puts "\\".gsub("\\", "\\\\")
>
> Why is that so?

Well, it's an faq. In short: The backslash is special in strings *and*
in the replacement text. So for every literal backslash you need four
backslashes, which is quite unreadable. If you don't need \1 and Co you
better use the block form: gsub("\\") { "\\\\" }.

mfg,                       simon .... hth
0
overlord (171)
4/24/2008 12:28:55 PM
On Thu, Apr 24, 2008 at 8:35 AM, Simon Krahnke <overlord@gmx.li> wrote:
> * martinus <martin.ankerl@gmail.com> (11:53) schrieb:
>
>
>
>  > Hello, I have a mini-ruby quiz. Guess what this line of code writes to
>  > the console, then try it for yourself:
>  >
>  > puts "\\".gsub("\\", "\\\\")
>  >
>  > Why is that so?
>
>  Well, it's an faq. In short: The backslash is special in strings *and*
>  in the replacement text. So for every literal backslash you need four
>  backslashes, which is quite unreadable. If you don't need \1 and Co you
>  better use the block form: gsub("\\") { "\\\\" }.

Another thing which trips up newbies, and sometimes not so newbies, is
the difference between the contents of a sring, and the 'inspect'
presentation of a string, particularly when the string contains
escapes:

irb(main):001:0> puts "\\"
\
=> nil
irb(main):002:0> p "\\"
"\\"
=> nil

The point is that the literal string "\\" only contains one character.
 The puts method shows you the contents of the string, while p (which
is practically equivalent to puts string.inspect produces a literal
representation.

-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

0
rick.denatale (1691)
4/24/2008 1:15:33 PM
martinus <martin.ankerl@gmail.com> wrote:

> Hello, I have a mini-ruby quiz. Guess what this line of code writes to
> the console, then try it for yourself:
> 
> puts "\\".gsub("\\", "\\\\")

We were just up and down this road (and I gave a workaround):

<http://groups.google.com/group/comp.lang.ruby/msg/d370b684485c8838>

m.


-- 
matt neuburg, phd = matt@tidbits.com, http://www.tidbits.com/matt/
Leopard - http://www.takecontrolbooks.com/leopard-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com
0
matt231 (2249)
4/25/2008 3:12:48 AM
Reply: