#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2015
    Location
    Jharkhand, India
    Posts
    1
    Rep Power
    0

    Smile 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:

    Code:
    if day=Monday do puts "WPD work" end
    I know it's not that simple. I also tried other approaches like:

    Code:
    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:

    Code:
    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:

    Code:
    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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    95
    Rep Power
    42
    == is the equality operator in Ruby and = is the assignment operator. So time.strftime("%a")="Friday" tries to assign to the result of a method call, which is invalid syntax in Ruby (and wouldn't do what you want anyway). Also the do keyword is not used with if statements in ruby. The syntax for if statements is if ... then ... else ... end where the else-part is optional and the then keyword can be left out if it's at the end of a line. After fixing those errors, your aproach should work.

    Another aproach would be to use Time#wday, which gives you an integer from 0 through 6, which represents Sunday through Saturday respectively. So if Time.now.wday == 1 checks whether today is Monday.

IMN logo majestic logo threadwatch logo seochat tools logo