"Time.now" is not enough



  •  Let's say a Ruby on Rails developer is concerned with a following problem : he needs a current time. Now someone might say, no big deal, just call Time.now. But that's just so incredibly boring, there has to be a more creative way of doing it. And so he ends up the line I'm looking at right now:

         0.days.from_now



  • I don't know ROR, but is it possible that the person is new to it and knows the days object, but not the time object?

    Otherwise, wow!

     



  • That doesn't sound that weird to me. If he's trying to create an interval, like scheduleTask(0.days.from_now) as a series of scheduled tasks, this notation might make more sense than scheduleTask(Time.now) 



  •  Meh, it's interesting, and possibly redundant, but not really a WTF.

    Who knows, maybe it's a side effect from a feature change. "Able to make [insert thing here] from x number of days in the future" => "Only do it from the day the [item] was made"

    So, 

    num_days.days.from_now => 0.days.from_now



  • That's fascinating. Does the zero object also have parameters for everything else in the universe?



  • Out of curiosity, what does 1.5.days.from_now return?  or 1/3.days.from_now?



  •  Maybe it's like spanish, where the noun comes before the adjectives instead of after.



  •  Well, if nobody is going to say it, then I will: needs moar Minerva!



  • This seemed kind of curious, so I google a bit and found http://brian.maybeyoureinsane.net/blog/2007/01/25/rails-duration/

    The really funny bit is this:

    1. >> Time.now
    2. => Thu Jan 25 21:01:31 -0800 2007
    3. >> 1.month.from_now
    4. => Sun Feb 24 21:01:34 -0800 2007
    Hah!

     



  • @Eric Shinn said:

    This seemed kind of curious, so I google a bit and found http://brian.maybeyoureinsane.net/blog/2007/01/25/rails-duration/

    The really funny bit is this:

    1. >> Time.now
    2. => Thu Jan 25 21:01:31 -0800 2007
    3. >> 1.month.from_now
    4. => Sun Feb 24 21:01:34 -0800 2007
    Hah!
     
     

    Wow!  Just think of the overhead associated with extending every number with time methods!



  • @AccessGuru said:


    Wow!  Just think of the overhead associated with extending every number with time methods!

    Um, none at all.  It's called Open Classes, and is an integral part of the Ruby language. It's quite simple, really:

    class Fixnum; def days; ... end; end

    Run that code, you've just added the #days method to every integer you use in your code.

    Out of curiosity, what does 1.5.days.from_now return?  or 1/3.days.from_now?

    (1.5).days.from_now gives you a NoMethodError. As you see above #days is defined in Fixnum, while 1.5 is a Float.

    (1/3).days.from_now == 0.days.from_now, which is easy to see when you know that Ruby doesn't coerce anything. If you have to Fixnums, you get a Fixnum. If one or more of the numbers is a Float, THEN you'll get a Float back.



  • @JamesKilton said:

    Um, none at all.  It's called Open Classes, and is an integral part of the Ruby language. It's quite simple, really:

    class Fixnum; def days; ... end; end

    Run that code, you've just added the #days method to every integer you use in your code.

    #days and #from_now are both part of Facets.
    @JamesKilton said:
    (1.5).days.from_now gives you a NoMethodError. As you see above #days is defined in Fixnum, while 1.5 is a Float.

    In Facets, #days and #from_now are both defined as part of Numeric, so it works just fine. (1.5).days.from_now is equivalent to Time.now + (1.5 * 24 * 60 * 60).

    With that said, the original code is still a WTF because 0.days.from_now uses Time.now as the default argument. So instead of just making the line Time.now, they waste their time multiplying a bunch of numbers by 0.



  • @bstorer said:

    ]#days and #from_now are both part of Facets.

    Maybe so, but for Rails they are defined in ActiveSupport: http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Numeric/Time.html#M000420

    @bstorer said:

    In Facets, #days and #from_now are both defined as part of Numeric, so it works just fine. (1.5).days.from_now is equivalent to Time.now + (1.5 * 24 * 60 * 60).

     

    Heh, teach me to not try it before posting. Thanks for the correction.


Log in to reply