`` inside a string does not escape anything












2














The character is used to escape some characters in a string. I need a string that includes like this:



str = "Lucas Andrade "My name""


When I print str, I should see this:



print str
# >> Lucas Andrade "My name"


I will embed this string directly inside a json param to send an API request using HTTParty like this example:



params {
"name": "Lucas",
"json": {
"address": "some",
"street": "example",
"string": "the custom "string""
}
}


I think HTTParty does not format this string automatically.










share|improve this question
























  • " inside a string does not escape anything" – not true, it escapes the " which would otherwise end the string.
    – Stefan
    Nov 21 at 8:12


















2














The character is used to escape some characters in a string. I need a string that includes like this:



str = "Lucas Andrade "My name""


When I print str, I should see this:



print str
# >> Lucas Andrade "My name"


I will embed this string directly inside a json param to send an API request using HTTParty like this example:



params {
"name": "Lucas",
"json": {
"address": "some",
"street": "example",
"string": "the custom "string""
}
}


I think HTTParty does not format this string automatically.










share|improve this question
























  • " inside a string does not escape anything" – not true, it escapes the " which would otherwise end the string.
    – Stefan
    Nov 21 at 8:12
















2












2








2







The character is used to escape some characters in a string. I need a string that includes like this:



str = "Lucas Andrade "My name""


When I print str, I should see this:



print str
# >> Lucas Andrade "My name"


I will embed this string directly inside a json param to send an API request using HTTParty like this example:



params {
"name": "Lucas",
"json": {
"address": "some",
"street": "example",
"string": "the custom "string""
}
}


I think HTTParty does not format this string automatically.










share|improve this question















The character is used to escape some characters in a string. I need a string that includes like this:



str = "Lucas Andrade "My name""


When I print str, I should see this:



print str
# >> Lucas Andrade "My name"


I will embed this string directly inside a json param to send an API request using HTTParty like this example:



params {
"name": "Lucas",
"json": {
"address": "some",
"street": "example",
"string": "the custom "string""
}
}


I think HTTParty does not format this string automatically.







json ruby string






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 6:15









sawa

129k27196299




129k27196299










asked Nov 20 at 20:02









Lucas Andrade

337316




337316












  • " inside a string does not escape anything" – not true, it escapes the " which would otherwise end the string.
    – Stefan
    Nov 21 at 8:12




















  • " inside a string does not escape anything" – not true, it escapes the " which would otherwise end the string.
    – Stefan
    Nov 21 at 8:12


















" inside a string does not escape anything" – not true, it escapes the " which would otherwise end the string.
– Stefan
Nov 21 at 8:12






" inside a string does not escape anything" – not true, it escapes the " which would otherwise end the string.
– Stefan
Nov 21 at 8:12














3 Answers
3






active

oldest

votes


















3














I am not very fluent in Ruby, but this is commonly done with "\", resulting in ""



So, "Lucas Andrade \"My name\"" should result in what you're looking for, an escaped and an escaped "






share|improve this answer































    3














    When you need to send the string as JSON to an API, why don't you use JSON.generate to escape the string as needed?



    JSON.generate("Lucas Andrade "My name"")





    share|improve this answer

















    • 1




      The OP should probably generate the whole JSON structure out of a Ruby hash.
      – Stefan
      Nov 21 at 8:10



















    1














    You can wrap it in single quote marks.



    str = 'Lucas Andrade "My name"'
    => "Lucas Andrade "My name""





    share|improve this answer





















    • This does not answer the question. He needs the output to include the backslash character and the output of puts 'Lucas Andrade "My name"' does not include a backslash.
      – anothermh
      Nov 20 at 20:27










    • There's a bit of confusion from OP: ` So when I print my str I should see this: "Lucas Andrade "My name""` which my answer does. But OP also says they want the backslash therein.
      – well-i-better-get-rolling
      Nov 20 at 20:28










    • In ruby puts and print are different, that's why I said print. In your answer the are just represented by ruby output syntax, the are not part of the string.
      – Lucas Andrade
      Nov 20 at 20:58










    • You're right. You did say print.
      – well-i-better-get-rolling
      Nov 20 at 21:13










    • puts is like print with newline (if missing).
      – Stefan
      Nov 21 at 7:54











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400700%2finside-a-string-does-not-escape-anything%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    I am not very fluent in Ruby, but this is commonly done with "\", resulting in ""



    So, "Lucas Andrade \"My name\"" should result in what you're looking for, an escaped and an escaped "






    share|improve this answer




























      3














      I am not very fluent in Ruby, but this is commonly done with "\", resulting in ""



      So, "Lucas Andrade \"My name\"" should result in what you're looking for, an escaped and an escaped "






      share|improve this answer


























        3












        3








        3






        I am not very fluent in Ruby, but this is commonly done with "\", resulting in ""



        So, "Lucas Andrade \"My name\"" should result in what you're looking for, an escaped and an escaped "






        share|improve this answer














        I am not very fluent in Ruby, but this is commonly done with "\", resulting in ""



        So, "Lucas Andrade \"My name\"" should result in what you're looking for, an escaped and an escaped "







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 at 8:17









        Stefan

        75.1k894141




        75.1k894141










        answered Nov 20 at 20:07









        David H

        461




        461

























            3














            When you need to send the string as JSON to an API, why don't you use JSON.generate to escape the string as needed?



            JSON.generate("Lucas Andrade "My name"")





            share|improve this answer

















            • 1




              The OP should probably generate the whole JSON structure out of a Ruby hash.
              – Stefan
              Nov 21 at 8:10
















            3














            When you need to send the string as JSON to an API, why don't you use JSON.generate to escape the string as needed?



            JSON.generate("Lucas Andrade "My name"")





            share|improve this answer

















            • 1




              The OP should probably generate the whole JSON structure out of a Ruby hash.
              – Stefan
              Nov 21 at 8:10














            3












            3








            3






            When you need to send the string as JSON to an API, why don't you use JSON.generate to escape the string as needed?



            JSON.generate("Lucas Andrade "My name"")





            share|improve this answer












            When you need to send the string as JSON to an API, why don't you use JSON.generate to escape the string as needed?



            JSON.generate("Lucas Andrade "My name"")






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 20 at 20:23









            spickermann

            58.4k65576




            58.4k65576








            • 1




              The OP should probably generate the whole JSON structure out of a Ruby hash.
              – Stefan
              Nov 21 at 8:10














            • 1




              The OP should probably generate the whole JSON structure out of a Ruby hash.
              – Stefan
              Nov 21 at 8:10








            1




            1




            The OP should probably generate the whole JSON structure out of a Ruby hash.
            – Stefan
            Nov 21 at 8:10




            The OP should probably generate the whole JSON structure out of a Ruby hash.
            – Stefan
            Nov 21 at 8:10











            1














            You can wrap it in single quote marks.



            str = 'Lucas Andrade "My name"'
            => "Lucas Andrade "My name""





            share|improve this answer





















            • This does not answer the question. He needs the output to include the backslash character and the output of puts 'Lucas Andrade "My name"' does not include a backslash.
              – anothermh
              Nov 20 at 20:27










            • There's a bit of confusion from OP: ` So when I print my str I should see this: "Lucas Andrade "My name""` which my answer does. But OP also says they want the backslash therein.
              – well-i-better-get-rolling
              Nov 20 at 20:28










            • In ruby puts and print are different, that's why I said print. In your answer the are just represented by ruby output syntax, the are not part of the string.
              – Lucas Andrade
              Nov 20 at 20:58










            • You're right. You did say print.
              – well-i-better-get-rolling
              Nov 20 at 21:13










            • puts is like print with newline (if missing).
              – Stefan
              Nov 21 at 7:54
















            1














            You can wrap it in single quote marks.



            str = 'Lucas Andrade "My name"'
            => "Lucas Andrade "My name""





            share|improve this answer





















            • This does not answer the question. He needs the output to include the backslash character and the output of puts 'Lucas Andrade "My name"' does not include a backslash.
              – anothermh
              Nov 20 at 20:27










            • There's a bit of confusion from OP: ` So when I print my str I should see this: "Lucas Andrade "My name""` which my answer does. But OP also says they want the backslash therein.
              – well-i-better-get-rolling
              Nov 20 at 20:28










            • In ruby puts and print are different, that's why I said print. In your answer the are just represented by ruby output syntax, the are not part of the string.
              – Lucas Andrade
              Nov 20 at 20:58










            • You're right. You did say print.
              – well-i-better-get-rolling
              Nov 20 at 21:13










            • puts is like print with newline (if missing).
              – Stefan
              Nov 21 at 7:54














            1












            1








            1






            You can wrap it in single quote marks.



            str = 'Lucas Andrade "My name"'
            => "Lucas Andrade "My name""





            share|improve this answer












            You can wrap it in single quote marks.



            str = 'Lucas Andrade "My name"'
            => "Lucas Andrade "My name""






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 20 at 20:10









            well-i-better-get-rolling

            3,42932759




            3,42932759












            • This does not answer the question. He needs the output to include the backslash character and the output of puts 'Lucas Andrade "My name"' does not include a backslash.
              – anothermh
              Nov 20 at 20:27










            • There's a bit of confusion from OP: ` So when I print my str I should see this: "Lucas Andrade "My name""` which my answer does. But OP also says they want the backslash therein.
              – well-i-better-get-rolling
              Nov 20 at 20:28










            • In ruby puts and print are different, that's why I said print. In your answer the are just represented by ruby output syntax, the are not part of the string.
              – Lucas Andrade
              Nov 20 at 20:58










            • You're right. You did say print.
              – well-i-better-get-rolling
              Nov 20 at 21:13










            • puts is like print with newline (if missing).
              – Stefan
              Nov 21 at 7:54


















            • This does not answer the question. He needs the output to include the backslash character and the output of puts 'Lucas Andrade "My name"' does not include a backslash.
              – anothermh
              Nov 20 at 20:27










            • There's a bit of confusion from OP: ` So when I print my str I should see this: "Lucas Andrade "My name""` which my answer does. But OP also says they want the backslash therein.
              – well-i-better-get-rolling
              Nov 20 at 20:28










            • In ruby puts and print are different, that's why I said print. In your answer the are just represented by ruby output syntax, the are not part of the string.
              – Lucas Andrade
              Nov 20 at 20:58










            • You're right. You did say print.
              – well-i-better-get-rolling
              Nov 20 at 21:13










            • puts is like print with newline (if missing).
              – Stefan
              Nov 21 at 7:54
















            This does not answer the question. He needs the output to include the backslash character and the output of puts 'Lucas Andrade "My name"' does not include a backslash.
            – anothermh
            Nov 20 at 20:27




            This does not answer the question. He needs the output to include the backslash character and the output of puts 'Lucas Andrade "My name"' does not include a backslash.
            – anothermh
            Nov 20 at 20:27












            There's a bit of confusion from OP: ` So when I print my str I should see this: "Lucas Andrade "My name""` which my answer does. But OP also says they want the backslash therein.
            – well-i-better-get-rolling
            Nov 20 at 20:28




            There's a bit of confusion from OP: ` So when I print my str I should see this: "Lucas Andrade "My name""` which my answer does. But OP also says they want the backslash therein.
            – well-i-better-get-rolling
            Nov 20 at 20:28












            In ruby puts and print are different, that's why I said print. In your answer the are just represented by ruby output syntax, the are not part of the string.
            – Lucas Andrade
            Nov 20 at 20:58




            In ruby puts and print are different, that's why I said print. In your answer the are just represented by ruby output syntax, the are not part of the string.
            – Lucas Andrade
            Nov 20 at 20:58












            You're right. You did say print.
            – well-i-better-get-rolling
            Nov 20 at 21:13




            You're right. You did say print.
            – well-i-better-get-rolling
            Nov 20 at 21:13












            puts is like print with newline (if missing).
            – Stefan
            Nov 21 at 7:54




            puts is like print with newline (if missing).
            – Stefan
            Nov 21 at 7:54


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400700%2finside-a-string-does-not-escape-anything%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen