A Newbie's System Date-Related Problem

Hi. I’m new to Ruby and I like it. But even after digesting all the good
documentation, when I started a small project for planning my day, I ran
into a few problems. Let me explain.

I want a script that I can run any time which will basically give
messages based on system time and date. For each day, I have two
“sessions”. So, if it’s the 10AM-12AM session of Monday, the message
will be “WPD work”. If it is 5PM-7PM session of Friday, the program will
message “WordPress work” and so on.

It’s basically like a database.

Now, for this, I want to use conditional if statements (suggest if
there’s a better way ). So I don’t know much about how to play with time
and date, so here’s a rough sketch of what I want to do:

if day=Monday do puts “WPD work” end

I know it’s not that simple. I also tried other approaches like:

time=Time.new
puts"\t\t\tToday:"+time.strftime("%a")

But then I don’t know how to use this returned data value for further
processing, like the below snippet doesn’t work:

if time.strftime("%a")=“Friday” do puts “WordPress work” end

Using this in this way doesn’t even make sense anyway. I just tried. As
I said, I’m completely unfamiliar with handling system date and type.
Suppose I opt for user-input:

day=gets.chomp

So you see, it’s SO simple. All I need is a time-range and day name to
proceed further. That’s ALL. But I seem to be unable to get that
automatically from system.

Hi, welcome.
You should have RDoc at hand, I mean the API documentation: core API
and standard libraries which come packed with Ruby.
You can do something like this:

now = Time.now # now is a Time object

(10…12) is a Range object

if((now.monday?) && ((10…12).cover(now.hour)))
puts(‘WDP works’)
elsif((now.friday?) && ((17…19).cover(now.hour)))
puts(‘WordPress work’)
end

Note that I’ve used many parenthesis so you can understand what’s
happening, also is my usual way of write code.
You can’t use case-switch in this particular case because you have to
deal with two conditions.
Hope this can help you, see you around.

Damián M. González wrote in post #1166460:

now = Time.now # now is a Time object

(10…12) is a Range object

if((now.monday?) && ((10…12).cover(now.hour)))
puts(‘WDP works’)
elsif((now.friday?) && ((17…19).cover(now.hour)))
puts(‘WordPress work’)
end

Thanks! That solved my problem!