On Wed, Jan 16, 2013 at 1:58 PM, John Sampson <[email protected]> wrote:
> I am trying to find a way of removing escaped characters in input strings
> from a file made by another program. That is to say, two-character sequences
> in which the first character is a backslash.
>
> I would have thought in my naivete that gsub(/\\./,"") would do this, but
> no. I am using Ruby 1.9 hence Oniguruma.

Why not?  This works as expected for me:

irb(main):001:0> s = 'a\\bc'
=> "a\\bc"
irb(main):002:0> puts s, s.length
a\bc
4
=> nil
irb(main):003:0> x = s.gsub(/\\./, '')
=> "ac"
irb(main):004:0> puts x, x.length
ac
2
=> nil
irb(main):005:0>

> Firstly, are the strings input from a text file treated as single- or
> double-quoted?

Neither.  Single and double quotes only have meaning inside program code.

> Secondly, are there alternatives to gsub?

Well, you can code it up yourself.  You can use

irb(main):006:0> s.delete '\\'
=> "abc"

> Thirdly, are there any *clear* and *exhaustive* treatments of the question
> of escaped backslashes in Ruby - in input strings in programs, in IRB, with
> single-quoted strings, double-quoted strings and other situations? I guess a
> multidimensional table might be useful to determine how to escape a
> backslash, if it can be done at all.

The topic comes up frequently here.  Other than that:
http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_stdtypes.html#S2

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

-- 
[email protected] | 
https://groups.google.com/d/forum/ruby-talk-google?hl=en


Reply via email to