I''m trying to parse ical strings (not that ical strings are all that special) but I''m trying to break them down for date calculations... so if I have a value like this... @events[0].recurrence_rules => ["FREQ=YEARLY;INTERVAL=1;UNTIL=99991231"] this seems so damn clumsy... freq = "yearly" if @events[0].recurrence_rules.to_s =~ /FREQ=YEARLY/ freq = "monthly" if @events[0].recurrence_rules.to_s =~ /FREQ=MONTHLY/ and then the interval, I would really just like the numerical value... how can I cleanly get these values because reg expressions seem so clumsy Craig --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Craig, The following regex will produce a hash for you with key/value pairs as defined in that rules string you provided:- recurrence_rules = "FREQ=YEARLY;INTERVAL=1;UNTIL=99991231".downcase Hash[*recurrence_rules.scan(/([^=;][a-z1-9]*)/).zip.flatten] Beneath is my irb output so you can see how i thought you could use it:->> recurrence_rules = "FREQ=YEARLY;INTERVAL=1;UNTIL=99991231".downcase=> "freq=yearly;interval=1;until=99991231">> ical = Hash[*recurrence_rules.scan(/([^=;][a-z1-9]*)/).zip.flatten]=> {"freq"=>"yearly", "until"=>"99991231", "interval"=>"1"}>> ical.each_key {|key| puts "#{key}=>#{ical[key]}"}freq=>yearly until=>99991231 interval=>1 => {"freq"=>"yearly", "until"=>"99991231", "interval"=>"1"} Hope that is ok? Regards, Jabbslad On Feb 25, 8:56 pm, Craig White <cr...-CnJ8jr4MGtxl57MIdRCFDg@public.gmane.org> wrote:> I''m trying to parse ical strings (not that ical strings are all that > special) but I''m trying to break them down for date calculations... > > so if I have a value like this... > > @events[0].recurrence_rules > => ["FREQ=YEARLY;INTERVAL=1;UNTIL=99991231"] > > this seems so damn clumsy... > > freq = "yearly" if @events[0].recurrence_rules.to_s =~ /FREQ=YEARLY/ > freq = "monthly" if @events[0].recurrence_rules.to_s =~ /FREQ=MONTHLY/ > > and then the interval, I would really just like the numerical value... > > how can I cleanly get these values because reg expressions seem so > clumsy > > Craig--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mon, 2008-02-25 at 18:33 -0800, Jabbslad wrote:> Hi Craig, > > The following regex will produce a hash for you with key/value pairs > as defined in that rules string you provided:- > > recurrence_rules = "FREQ=YEARLY;INTERVAL=1;UNTIL=99991231".downcase > Hash[*recurrence_rules.scan(/([^=;][a-z1-9]*)/).zip.flatten] > > > > Beneath is my irb output so you can see how i thought you could use > it:- > > >> recurrence_rules = "FREQ=YEARLY;INTERVAL=1;UNTIL=99991231".downcase > => "freq=yearly;interval=1;until=99991231" > > >> ical = Hash[*recurrence_rules.scan(/([^=;][a-z1-9]*)/).zip.flatten] > => {"freq"=>"yearly", "until"=>"99991231", "interval"=>"1"} > > >> ical.each_key {|key| puts "#{key}=>#{ical[key]}"} > freq=>yearly > until=>99991231 > interval=>1 > => {"freq"=>"yearly", "until"=>"99991231", "interval"=>"1"} > > Hope that is ok?---- thanks - what I ended up doing (and it turns out to be a little simpler) is this... y = @events[x].recurrence_rules.to_s.split('';'') which gives me an array with 3 elements... freq = y[0].gsub(/FREQ=/,'''') interv = y[1].gsub(/INTERVAL=/,'''').to_i dends = y[2].to_date Thanks Craig --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---