Split a value and units in Ruby -


What is the best way to split prices and units, so you have:

  13min.some_method ["value"] = 13 13min.some_method ["unit"] = "min"  

I think it should use unpack or division, but I I can get to work!

How about:

  & gt; & Gt; "13 minutes" .scan (/ \ d + | [^ \ d] + /) = & gt; ["13", "Minimum"]  

Another way:

  & gt; I = "13 minutes" = ~ / [^ \ d] / = & gt; 2 & gt; & Gt; "13 minutes" [0, I] => "13" & gt; & Gt; "13 min" [I, "13 minutes". Lengthy] = & gt; "Minimum"  # string is not the best because it can get rid of the delimiter, but your two strings are against each other. 


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -