2

I'm trying to make a array out of a string and then get rid of the quotes surrounding the strings.

This is what I tried:

hg = "'Erra', 'Erra-Network', 'Discovery'".split(",")
hg2 = hg.each { |n| n.delete_prefix("'").delete_suffix("'") }
print(hg2)

but doesn't work.

Output:

["'Erra'", " 'Erra-Network'", " 'Discovery'"]
8
  • what is the expected output? Commented Feb 5, 2020 at 14:13
  • an array without the single quotes Commented Feb 5, 2020 at 14:19
  • change each to map and try again Commented Feb 5, 2020 at 14:20
  • "'Erra', 'Erra-Network', 'Discovery'" – where does that string come from? Commented Feb 5, 2020 at 14:45
  • @Mefistofeles : You calculate inside your block a new string, and then throw it away. You never actually modify one of the array elements. Look up in the docs the difference between delete_prefix and delete_prefix!. Commented Feb 5, 2020 at 14:50

4 Answers 4

4

Try map instead of each, it will return a new updated Array:

hg = "'Erra', 'Erra-Network', 'Discovery'".split(",")
hg2 = hg.map { |n| n.delete_prefix("'").delete_suffix("'") }
print(hg2)

each will execute for each element in the Array, but will then return the original unmodified Array. map actually returns the modified Array: https://stackoverflow.com/a/5254192/44733

Sign up to request clarification or add additional context in comments.

1 Comment

A space in the delimiter is required in order for this solution to work split(", ").
1

You're working on hg but then printing hg2 to check if the operation succeeded. Although that's not the problem, it may lead to confusion.

Also, as you're using delete_prefix and delete_suffix in their non-destructive versions, the changes applied return a new object, which isn't "persisted" anywhere.

If you want to see the changes that that produces you can use their destructive version delete_prefix!, delete_suffix!:

hg2 = hg.each do |n|
  n.delete_prefix!("'")
  n.delete_suffix!("'")
end

hg # ["Erra", " 'Erra-Network", " 'Discovery"]

Or rather use map and yield the result of every operation into a new object:

hg2 = hg.map { |n| n.delete_prefix("'").delete_suffix("'") }

p hg2 # ["Erra", " 'Erra-Network", " 'Discovery"]

Although this results in ["Erra", " 'Erra-Network", " 'Discovery"] and doesn't go according to the title of the question "delete the quotes surrounding the strings".

2 Comments

Aaah I got it, totally.
As per the last comment, the title is a bit misleading as it doesn't correspond itself with what's being shown in the code. The fact of using delete_prefix and/or delete_suffix doesn't mean you're going to delete the quotes surrounding strings.
0

You could use CSV to get your result:

require 'csv'

string = "'Erra', 'Erra-Network', 'Discovery'"
result = CSV.parse_line(string, col_sep: ', ', quote_char: "'")
#=> ["Erra", "Erra-Network", "Discovery"]

Note that the above assumes that the separator is ', '. This means that a comma must always be followed by a space.

Comments

0

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.

Comments

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.