You're making it too difficult. I'd do this:
hg = "'Erra', 'Erra-Network', 'Discovery'"
hg.delete(" '").split(',') # => ["Erra", "Erra-Network", "Discovery"]
delete
is doing the clean-up, which should be done before trying to split
the string.
delete_prefix
and delete_suffix
are useful methods, but not when processing strings you're parsing because they force you into iteratively processing the sub-strings. delete
does the entire string in one step, which is faster.
If I was going to iterate, I'd do something like this:
hg.split(/, */).map { |s| s[1..-2] } # => ["Erra", "Erra-Network", "Discovery"]
which takes advantage of split
being able to take a regular expression to automatically break on a comma followed by any number of spaces.
Basically you'll trying to parse a mangled CSV record, so you should use the CSV class. The documentation has many examples for parsing records:
require 'csv'
CSV.parse(hg.delete("' ")).flatten # => ["Erra", "Erra-Network", "Discovery"]
CSV has many options available for handling odd variations of delimiters and quoted strings, so study the documentation if you want to go that way.