Home > Software design >  String Manipulation with ruby
String Manipulation with ruby

Time:08-17

I am trying to split a list which is coming from an API, the list is this:

Condition: ["Precursor Cell Lymphoblastic Leukemia-Lymphoma", "Lymphoma, Non-Hodgkin"]

I want the result to be Split like this:

"Precursor Cell Lymphoblastic Leukemia-Lymphoma",
"Lymphoma, Non-Hodgkin"

by the quotes.

But I am getting the output like this:

[
     "precursor cell lymphoblastic leukemia-lymphoma",
      "lymphoma",
      "non-hodgkin"
  ]

I am using ruby Language and the formula is this:

condition.to_s.gsub(",",";").gsub('"',"").gsub("[","").gsub("]","")

Can anyone help me in this?

CodePudding user response:

Do it like this:

[[0, "Precursor Cell Lymphoblastic Leukemia-Lymphoma"], [1, "Lymphoma, Non-Hodgkin"]].to_h

This will give you what you need.

Edit: I misread the question. If the list is different for a particular ID and coming from an API and the list is in an array form and stored in a variable then you can simply do it like -

variable_name.each_with_index.to_h { |x, idx| [x, idx   1] }

CodePudding user response:

 data.scan(/\"[^\"] \"/).map {_1[1...-1]}
  • Related