How to convert this Ruby String to an array?

What is the best way to turn the next Ruby String into an array (I am using ruby ​​1.9.2 / Rails 3.0.11)

Rails Console:

>Item.first.ingredients => "[\"Bread, whole wheat, 100%, slice\", \"Egg Substitute\", \"new, Eggs, scrambled\"]" >Item.first.ingredients.class.name => "String" >Item.first.ingredients.length 77 

Desired Result:

 >Item.first.ingredients_a ["Bread, whole wheat, 100%, slice", "Egg Substitute", "new, Eggs, scrambled"] >Item.first.ingredients_a.class.name => "Array >Item.first.ingredients_a.length => 3 

If I do this, for example:

 >Array(Choice.first.ingredients) 

I get this:

 => ["[\"Bread, whole wheat, 100%, slice\", \"Egg Substitute\", \"new, Eggs, scrambled\", \"Oats, rolled, old fashioned\", \"Syrup, pancake\", \"Water, tap\", \"Oil, olive blend\", \"Spice, cinnamon, ground\", \"Seeds, sunflower, kernels, dried\", \"Flavor, vanilla extract\", \"Honey, strained/extracted\", \"Raisins, seedless\", \"Cranberries, dried, swtnd\", \"Spice, ginger, ground\", \"Flour, whole wheat\"]"] 

I am sure there must be some obvious way to solve this problem.

For clarity, this will be edited in the textarea field of the form, so it should be made as safe as possible.

+4
source share
6 answers
 class Item def ingredients_a ingredients.gsub(/(\[\"|\"\])/, '').split('", "') end end 
  • delete extra characters
  • division into array elements using a separation pattern
+5
source

What do you look like JSON, so you can do:

 JSON.parse "[\"Bread, whole wheat, 100%, slice\", \"Egg Substitute\", \"new, Eggs, scrambled\"]" #=> ["Bread, whole wheat, 100%, slice", "Egg Substitute", "new, Eggs, scrambled"] 

this avoids the many horrors of using eval .

Although you really have to think about why you are storing your data in the first place, and think about a change so you don't have to. Also, it is likely that you should parse it into an array in ingredients so that the method returns something more meaningful. If you almost always perform the same operation with the return value of a method, the method is incorrect.

+8
source

It looks like the ingredients method returns the .inspect output of the resulting returned array. This is not a very useful conclusion. Do you have the option to modify it to return a simple array?

What I would not do is use eval , which will simply increase the hacking of already cracked code.

+1
source

Like Mark Thomas, change your ingredient method if you really don't want two separate methods to return a string and an array, respectively. I assume that you really want to return an array. For the argument, let's say your ingredients method currently returns a variable called ingredients_string . Change your method as follows:

 def ingredients ... ingredients_array = ingredients_string.split('"') ingredients_array.delete_if { |element| %(", "[", "]").include? element } ingredients_array end 
+1
source

Not sure if this comes into play, but what happens if you want to use an array as an attribute, you can consider serialization.

 class Item << ActiveWhatever serialize :ingredients, Array ... end 

Read more about serialization here http://api.rubyonrails.org/classes/ActiveRecord/Base.html

0
source

If your string array is the standard JSON format, just use JSON.parse()

0
source

Source: https://habr.com/ru/post/1411665/


All Articles