0

I have this array of arrays:

["[]", "[\"https://somewebsite1.com\"]", "[\"https://somewebsite2.com\"]", "[]", "[]", "[]", "---\n- https://somewebsite3.com\n", "--- []\n", "---\n- https://somewebsite4.com\n", "--- []\n", "--- []\n", "---\n- https://somewebsite.com\n", "---\n- https://somewebsite5.com\n", nil, nil, nil, nil, nil]      

How can I:
1- remove all the empty, nil element
2- have an array of string:

["https://somewebsite1.com", "https://somewebsite2.com", "https://somewebsite3.com", "https://somewebsite4.com", "https://somewebsite.com", "https://somewebsite5.com"]

array.select &:present? remove nil
then array.delete_if {|i| i == '[]'} remove []
so I am left with :

["[\"https://somewebsite1.com\"]", "[\"https://somewebsite2.com\"]",  "---\n- https://somewebsite3.com\n", "--- []\n", "---\n- https://somewebsite4.com\n", "--- []\n", "--- []\n", "---\n- https://somewebsite.com\n", "---\n- https://somewebsite5.com\n"]
3
  • 4
    No, that's not array of arrays. That's array of strings, where strings contain what appears to be yaml code Commented Oct 31, 2017 at 15:28
  • A weird mix of YAML and JSON? Or is [] valid YAML, without the leading ---? Commented Oct 31, 2017 at 15:37
  • @meagar: yes, it appears to be valid yaml Commented Oct 31, 2017 at 15:54

2 Answers 2

3

First of all, it is a simple array of strings.

You can remove nil elements using array.compact or array.reject(&:nil?)

And to remove empty yaml entries you can use deserialization

array.compact .reject { |i| YAML.parse(i).children.first.children.empty? } .map { |i| i.gsub('["', '').gsub('"]', '').gsub("---\n- ", '').strip }

So the output would be you stated.

Update:

Another approach, without string manipulation

res = a.compact
deserialized = res.map { |i| YAML.parse(i) }
res = deserialized.reject { |i| i.children.first.children.empty? }
res.map { |i| i.children.first.children.first.value }
Sign up to request clarification or add additional context in comments.

3 Comments

Much gsub, so string manipulation, wow. :) Why not use output of YAML.parse instead? Or, better, YAML.load?
It's fast and dirty solution. Yeah, working with deserialized YAML would be much better.
Just use YAML.load, it'll simplify code substantially. my_array.compact.map(&YAML.method(:load)).reject(&:empty?).flatten
1

To just remove nil elements, you can use the compact method on an array. To remove both nil and empty strings, you can use the select method:

my_array.select! { |element| element&.size.to_i > 0 }

To process the remaining strings, you'll need to iterate over each string and figure out whether it needs YAML parsing or not. The method you'll use to collect the results of that iteration is map:

my_array.map! { |element| clever_string_parsing_on(element) }

2 Comments

Have you tried your first snippet? It's not working.
My bad - I had tried it, but with too simple a test case; the bug didn't show up. I've edited in a fix. Good catch, and many thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.