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
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 Mon, 2008-02-25 at 18:33 -0800, Jabbslad wrote:
Beneath is my irb output so you can see how i thought you could use
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