Doel
Doel

Reputation: 1004

How to escape slashes in ruby string

I am getting a response from the params in a rhodes application, like this "\"HOO123Wow\"" But I need it, in a simple string format, like "HOO123Wow". I have tried with gsub and delete but none of them seem to work for my application.

mystring = "\"HOO123Wow\""
mystring.gsub("\"", "")

Any suggestion will be really helpful.

Upvotes: 0

Views: 1471

Answers (4)

Nakilon
Nakilon

Reputation: 35112

irb(main)> eval mystring
=> "HOO123Wow"

or

irb(main)> mystring.delete ?"
=> "HOO123Wow"

Upvotes: 1

kiddorails
kiddorails

Reputation: 13014

Simple and elegant way:

string = "\"HOO123Wow\""
=> "\"HOO123Wow\""
string.tr('"','')
=> "HOO123Wow"

Done. :)

Upvotes: 1

Rohit
Rohit

Reputation: 5721

The value received in parameters == "\"HOO123Wow\""

Actual value to be persisted == "HOO123Wow"

Here's what I did:

irb(main):001:0> mystring = "\"HOO123Wow\""
=> "\"HOO123Wow\""
irb(main):002:0> puts mystring.gsub("\\\"","")
"HOO123Wow"
=> nil

Hope it works for you.

Note : I am using ruby 1.9.2p0 (2010-08-18) [i386-mingw32]

Upvotes: 0

Pablo
Pablo

Reputation: 2854

Use \\ for escaping slashes. Try this:

mystring = "\"HOO123Wow\"" 
mystring.gsub("\\\"", "")

Upvotes: 0

Related Questions