I have a string of values separated by commas, whose order isn’t important, but the uniqueness of the values is. I want to add values to the string, and I end up with something like
jruby-1.6.7 :009 > ("1,2,3,1".split(",").to_set << "1" << "4").to_a.join ","
=> "1,2,3,4"
which is effective, but looks terrible and goes string -> array -> set -> array -> string, which is no doubt not efficient either. What’s the simple way to do this?
you can use | (union) operator.
e.g.
which shall return,
| (union operator) wont work on strings. so in your case, you can use
=> [“1”, “2”, “3”, “4”]