I'm new to Ruby and working on a CLI application that parses some of my reports. I want to know the most effective way to get the following from this line:
MAXCONN: 2000, MAXSSL_CONN: 500, PLAINCONN: 34, AVAILCONN: 1966, IIDAON: 28, SSLCONN: 0, AVAILSSL: 500 I want to map it according to a hash accordingly:
{: maxconn = & gt; 2000 ,: maxssl_conn = & gt; 500 ,: plaincon => 34,: availconn = & gt; 1966 ,: Idolon = & gt; 28,: SSLcon = gt; 0 ,: availssl = & gt; 500} The only way I can think of doing this is to split into a comma and then on the half-colon and map them.
I have a suspicion to secretly have some ruby magic so that it can be obtained in a more efficient and less cumbersome way.
Any input or trick / tips will be appreciated because I think that I am having problems like this relatively often.
We unite the techniques to convert a group of key-value pairs to a hash
hash [[[1 k1, v1], [k2, v2], ...]] # = & gt; {K1 = & gt; V1, k2 = & gt; V2, ...} with regular expression method string # scan , which passes through a string and to collect matches in an array, hash [reports.scan (/ (\ w +): (\ w +) /). Map {| (First, second). [First.downcase.to_sym, second.to_i]}] also uses a block with the numerical # map , which gives the arrays to Explains as a pair of (first, second) element in the argument list, these are extracted into new elements, and apply the conversions to them so that the resultant hash to enter the specifications of your example (Otherwise, you get a hash of string mapping for the stars).
Comments
Post a Comment