Federico Zagarzazú
6/6/2007 11:28:00 PM
Thanks for your answers, silly me, got confused :),
But, how does it work?, suppouse you want to convert "fede" to "f\4de"
1) puts 'fede'.sub('e', '\4') # => fde
2) puts 'fede'.sub('e', '\\4') # => fde
3) puts 'fede'.sub('e', '\\\4') # => f\4de
4) puts 'fede'.sub('e', '\\\\4') # => f\4de
5) puts 'fede'.sub('e', '\\\\\4') # => f\de
6) puts 'fede'.sub('e', '\\\\\\4') # => f\de
7) puts 'fede'.sub('e', '\\\\\\\4') # => f\\4de
.
I don't get it..
I was thinking that it might work by making two passes looking for and
converting backslashes, like this:
(7):
first pass : (\\)(\\)(\\)(\4)
\ \ \ \4
second pass: (\\)(\\)4
result : \\4
But I'm not 100% sure, do you know how it works?
Thanks again.
On 6/6/07, james.d.masters@gmail.com <james.d.masters@gmail.com> wrote:
> On Jun 6, 2:59 pm, "Federico Zagarzazú" <fzagarz...@gmail.com> wrote:
> > Why can't I substitute a single backslash in a string?
> > a = "\\6"
> > ...
> > p a.sub('\', '')
>
> Because you're essentially escaping a single quote in your String#sub
> method call. Double escape that too:
>
> irb(main):001:0> a = "\\6"
> => "\\6"
> irb(main):002:0> a.sub('\\', '')
> => "6"
>
> Why is this? Suppose I wanted to make a string with single quotes in
> it:
>
> irb(main):003:0> a = '\'hello\''
> => "'hello'"
>
>
>