ruby on rails - String to Array and Hash with Regexp -
I would like to turn on a string with the opening hours like this:
"Monday - Friday> 10: 00-18: 00; Saturday &&; 12: 00-17: 00; Sunday> 12: 00-15: 00 "
In:
[[:: duration = & gt; "Monday-Friday" ,: hours = & gt; "10: 00-18: 00"}, {: duration = & gt; "Saturday" ,: hours = & gt; "12: 00-17: 00"}, {: duration = & gt; "Sunday",: hours = & gt; "12: 00-15: 00"}]
I'm trying it with a string. Scan () method but can not understand the Regex.
In addition to this, if you have a suggestion that it is a way to reverse the best way (i.e., when it takes time to open some form.)
Updates - You get all the right solutions! Right now I am using (thanksgjedelen):
str.scan (/ ([\ w]] +)> ([\ d: -] + ) - ([\ D:] +) /). Map {| (P, O, C). {: Duration = & gt; P, opens: = & gt; O, shutdown => C}}
but how is it now reversed =) then given:
[{: opens => "10:00", closes = = Gt; "18:00",: duration = & gt; "Monday-Friday"}, {: opens => "12:00", closes => "17:00" ,: duration = & gt; Saturday "}, {: opens => 12:00 ",: closes => 15:00 ",: period =>" Sunday "}]
I want to merge it:
" Monday-Friday> 10: 00-18: 00; Saturday & gt; 12: 00-17: 00; Sunday & gt; 12: 00-15: 00 "
If you prefer one-liners:
s = "Monday-Friday> 10: 00-18: 00; Saturday & gt; 12: 00-17: 00; Sunday & gt; 12: 00-15: 00 "s.split (/; /). Map {| i | hash [[: duration:, hours], i.split (/ & gt; /)]. Transfer] # # or S.split (/; /). Map {| i | P, h = i.split (/ & gt; /); {: duration = & gt; P,: hours => h}} # = & Gt; [[: :: duration = & gt; "Monday-Friday",: hours => "10: 00-18: 00"}, {: duration => "Saturday" ,: hours = & gt; ; "12: 00-17: 00"}, {: duration => Sunday ",: hours =>" 12: 00-15: 00 "}]
Edit:
Regarding reverse, this should work:
A.map {| i | "# {I [: duration]} & gt; # {I opens:}} - # {i: closes}} "}. Join (';') =>" Monday-Friday "> 10: 00-18: 00; Saturday & gt; 12: 00-17: 00; Sunday & gt; 12: 00-15: 00 "
Comments
Post a Comment