Using a \ in gsub

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???

On 7/15/06, brandon coleman [email protected] wrote:

any suggestions???
File.expand_path($0).gsub(%r{/}) { “\” }

-austin

Austin Z. wrote:

On 7/15/06, brandon coleman removed_email_addres[email protected] 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…

On Saturday 15 July 2006 23:05, brandon coleman wrote:

Austin Z. wrote:

On 7/15/06, brandon coleman [email protected] 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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs