Search code examples
ruby-on-railsstring-parsing

Parse a string dynamically in ruby on rails


I have a hashmap with just two keys. One key, value is having a pattern like this -

"flownamewkf1_somethingwkf2_something.." => 1"

The second key, value has pattern like this -

"some_name" => "group_name"

Now i want to create a new string in this way:

make-deployment-group --flowname/wkf1_something --flowname/wkf2_something group_name

Sometimes flowname can have only one wkf name and then the first key,value will be "flownamewkf1_something" => 1" and the string should be

make-deployment-group --flowname/wkf1_something group_name

So, I need to dynamically generate the string based on number of wkf, the flowname has and add the group_name at the end and make-deployment-group at the beginning and the folder and wkf names in the middle

I tried string.partition(/wkf/) but the splits the string into three parts flowname, wkf, rest of part

Please help.


Solution

  • I'm not completely sure what you are asking. But maybe you can take this and run with it.

    1.8.7 :049 > flowname, *wkf = "flownamewkf1_somethingwkf2_something".split(/wkf/)
     => ["flowname", "1_something", "2_something"] 
    1.8.7 :050 > params = wkf.collect{|w| "--#{flowname}/wkf#{w}"}
     => ["--flowname/wkf1_something", "--flowname/wkf2_something"] 
    1.8.7 :052 >   "make-deployment-group #{params.join(' ')}"
     => "make-deployment-group --flowname/wkf_something--flowname/wkf_something" 
    

    Then you can tack on that group_name.