lundi 24 octobre 2016

How to treat "\n" as new line in ruby variable

I have a string

       x = "hello\nHi"

When i do puts x, its working fine i.e the output becomes

    hello
    hi

but my objective is to when i type the variable name i.e

    input = x
    output should be
    hello
    hi

Is there any way to achieve this?

Reason: I am doing a API call, the response is xml but in the xml it has "\n" before each entry. Resonse from API is

     <?xml version='1.0' encoding='iso-8859-1'?>\n<!DOCTYPE data [\n  <!ELEMENT data (record+)>\n  <!ELEMENT record (first_element,last_element)>\n   <!ELEMENT first_element (#PCDATA)>\n   <!ELEMENT last_element (#PCDATA)>\n]>\n<data>\n    <record>\n <first_element>hello</first_element>\n <last_element>hi</last_element>\n
    </record>\n</data>       

But I want in the following format

<?xml version='1.0' encoding='iso-8859-1'?>
<!DOCTYPE data [
  <!ELEMENT data (record+)>
  <!ELEMENT record (first_element,last_element)>
   <!ELEMENT first_element (#PCDATA)>
   <!ELEMENT last_element (#PCDATA)>
]>
<data>
    <record>
        <first_element>hello</first_element>
        <last_element>hi</last_element>
    </record>
..
..
</data>

So that I can parse it using some gems such as nokogiri or something else.

Any lead in this regard would be of great help

Aucun commentaire:

Enregistrer un commentaire