Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4039350
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:35:54+00:00 2026-05-20T12:35:54+00:00

Have been hacking together a couple of libraries, and had an issue where a

  • 0

Have been hacking together a couple of libraries, and had an issue where a string was getting ‘double escaped’.

for example:
Fixed example

 > x = ['a']
 => ["a"] 
 > x.to_s
 => "[\"a\"]" 
 > 

Then again to

\"\[\\\"s\\\"\]\"

This was happening while dealing with http headers. I have a header which will be an array, but the http library is doing it’s own character escaping on the array.to_s value.

The workaround I found, was to convert the array to a string myself, and then ‘undo’ the to_s. Like so:
formatted_value = value.to_s

if value.instance_of?(Array)
  formatted_value = formatted_value.gsub(/\\/,"") #remove backslash                                                 
  formatted_value = formatted_value.gsub(/"/,"")  #remove single quote
  formatted_value = formatted_value.gsub(/\[/,"") #remove [                                                                                                     
  formatted_value = formatted_value.gsub(/\]/,"") #remove ]   
end
value = formatted_value

… There’s gotta be a better way … (without needing to monkey-patch the gems I’m using). (yeah, this break’s if my string actually contains those strings.)

Suggestions?

** UPDATE 2 **

Okay. Still having troubles in this neighborhood, but now I think I’ve figured out the core issue. It’s serializing my array to json after a to_s call. At least, that seems to be reproducing what I’m seeing.

[‘a’].to_s.to_json

I’m calling a method in a gem that is returning the results of a to_s, and then I’m calling to_json on it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:35:55+00:00Added an answer on May 20, 2026 at 12:35 pm

    I’ve edited my answer due to your edited question:

    I still can’t duplicate your results!

    >> x = ['a']
    => ["a"]
    >> x.to_s
    => "a"
    

    But when I change the last call to this:

    >> x.inspect
    => "[\"a\"]"
    

    So I’ll assume that’s what you’re doing?

    it’s not necessarily escaping the values – per se. It’s storing the string like this:

    %{["a"]}
    

    or rather:

    '["a"]'
    

    In any case. This should work to un-stringify it:

    >> x = ['a']
    => ["a"]
    >> y = x.inspect
    => "[\"a\"]"
    >> z = Array.class_eval(y)
    => ["a"]
    >> x == z
    => true
    

    I’m skeptical about the safe-ness of using class_eval though, be wary of user inputs because it may produce un-intended side effects (and by that I mean code injection attacks) unless you’re very sure you know where the original data came from, or what was allowed through to it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been hacking around with Ruby a little and I have been wondering if
Have been trying to encrypt an xml file to a string so that I
Have been working on this question for a couple hours and have come close
Have been searching how to convert a dictionary to a string. But the results
Alright, I've been hacking around with GD Image for a couple of months, and
Hi i have a problem and ive been hacking at it for hours i
I've implemented a gridview and I've been hacking it together from examples to get
I'm hacking away at some code which seems to have been started, at least
I have been hacking away at this all day with only a successful 'UPDATE'
I am new to nginx and I have been hacking up my nginx.conf file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.