I’m tryind to build a helper to select the next available day from a list.
I have a list of days as a reference (those are the day where I want something to happen)
class_list = ["Monday","Saturday","Sunday"]
I need to check the current day and match it in the list.
If it’s part of the list I select it, if it isn’t I select the next one from the list.
this is what i have so far:
#select current day, get its name value and weekday number value
today = Time.now
today_name = today.strftime("%A")
#not sure which of the 2 following line is better
#today_index = DateTime.parse(today_name).wday
today_index = today.strftime("%u").to_i
Then I do the matching
if class_list.include? today_name
#victory!!!
puts today_name
else
puts "find next day"
class_list.each do |x|
if DateTime.parse(x).wday > today_index
puts "result #{x}"
break
end
end
end
When I run it seems to work fine, but as i’m just learning Ruby i’m always wondering if i’m not overcomplicating things.
Does this code looks alright to you Ruby masters?
1 Answer