[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

using a \ in gsub

brandon coleman

7/15/2006 8:55:00 PM

In IRB if I type the following:
"te/st".gsub('/','\')
into irb, IRB screws up.
but if I type
File.expand_path($0).gsub('/','\\')
I get te\\st. what gives?? how do I get ruby to come up with te\st?

what I am trying to do is this:
File.expand_path($0).gsub('/','\') because with ruby in windows it uses
a / instead of a \ for path names which is screwing my pathnames up..
any suggestions???

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

4 Answers

Austin Ziegler

7/15/2006 8:58:00 PM

0

On 7/15/06, brandon coleman <metrix1978@gmail.com> wrote:
> In IRB if I type the following:
> "te/st".gsub('/','\')
> into irb, IRB screws up.
> but if I type
> File.expand_path($0).gsub('/','\\')
> I get te\\st. what gives?? how do I get ruby to come up with te\st?
>
> what I am trying to do is this:
> File.expand_path($0).gsub('/','\') because with ruby in windows it uses
> a / instead of a \ for path names which is screwing my pathnames up..
> any suggestions???

File.expand_path($0).gsub(%r{/}) { "\\" }

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

brandon coleman

7/15/2006 9:06:00 PM

0

Austin Ziegler wrote:
> On 7/15/06, brandon coleman <metrix1978@gmail.com> wrote:
>> any suggestions???
> File.expand_path($0).gsub(%r{/}) { "\\" }
>
> -austin

that would give me two c:\\ruby\\ whatever and if I remove a \ it still
gives me an error...

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

Stefan Lang

7/15/2006 10:16:00 PM

0

On Saturday 15 July 2006 23:05, brandon coleman wrote:
> Austin Ziegler wrote:
> > On 7/15/06, brandon coleman <metrix1978@gmail.com> wrote:
> >> any suggestions???
> >
> > File.expand_path($0).gsub(%r{/}) { "\\" }
> >
> > -austin
>
> that would give me two c:\\ruby\\ whatever and if I remove a \ it
> still gives me an error...

Are you testing this in irb? You should know
that \ is used as escape character inside of Ruby string
literals. Thus if you want an actual backslash in the string,
you have to escape it with another backslash.
irb prints a ruby string literal to stdout, thus you see a
double backslash for every actual backslash in the string.

Try:
> puts File.expand_path($0).gsub(%r{/}, "\\")
to see the "real" contents of the resulting string.

BTW: The literal "\" gives an error because the backslash
before the second quote tells Ruby to include the quote
in the string instead of interpreting it as string delimiter.

--
Stefan

Rob Biedenharn

7/19/2006 7:07:00 PM

0