Ruby: Convert a string to an array by replacing special characters to /

How can I split the below attribute to have "/" instead of "," in between

default[attribute] = jenkins,mrepo,tbd,ios,logic,constant,solver,issuer

There's a few options for manipulating text in Ruby that you can use here. First you'll want that list of values to be a string and from there you can use the gsub method to substitute the ',' values for '/' values.

irb(main):001:0> "jenkins,mrepo,tbd,ios,logic,constant,solver,issuer".gsub(',', '/')
=> "jenkins/mrepo/tbd/ios/logic/constant/solver/issuer"

You can also split that string up into an array, which might be easier to handle in code:

irb(main):002:0> "jenkins,mrepo,tbd,ios,logic,constant,solver,issuer".split(',')
=> ["jenkins", "mrepo", "tbd", "ios", "logic", "constant", "solver", "issuer"]

Check out Class: String (Ruby 2.6) for a large number of examples on text manipulation in Ruby.

-Tim

-Tim

2 Likes

this worked thanks much!