11

I found the below code snippet on Twitter (check post history for source).

[5] pry(main)> Date.parse('3 Dogs')
ArgumentError: invalid date
[6] pry(main)> Date.parse('23 Dogs')
=> Mon, 23 Nov 2015

Is this just an easter egg in pry? If so, why this particular date and result? If it's not an easter egg, why does 23 Dogs parse to a date, but 3 Dogsdoesn't parse?

9
  • Is this just an easter egg in pry? Yes, only pry would produce that result. If you ran that code in a ruby program, or in irb, Date.parse() would produce: "Hello world" for that data. What do you get for Date.parse('23') and Date.parse('3')?
    – 7stud
    Nov 23, 2015 at 20:28
  • 2
    Actually, you should get NameError: uninitialized constant Date in both cases. Nov 23, 2015 at 20:37
  • @7stud I don't know, I don't have pry installed. If only pry gives that result, is that because it was hardcoded like that, or because pry has a different Date parser? And why does it give this result?
    – Nzall
    Nov 23, 2015 at 20:46
  • 1
    @CarySwoveland I edited to account for point 1, 2, 4 and 5. "Easter Egg" is a well-known term in the IT-sector and the internet which indicates something which has been added as an in-joke or a cultural reference. I can't remove this from the question without invalidating part of the below answers.
    – Nzall
    Nov 24, 2015 at 14:44
  • 1
    That's much better, imo, but the important thing is that it was and is a very good question. I hope you get a definitive answer, as I too would like to know what's going on. Nov 24, 2015 at 15:56

2 Answers 2

8

This has nothing to do with Pry. If you examine the doc for Date::parse you'll see, "If the optional second argument [comp] is true [the default] and the detected year is in the range “00” to “99”, considers the year a 2-digit form and makes it full.".

This is admittedly odd. It detects "23" to be in the range "00".."30" (but not "3" or any of "0".."9"), so it concludes it's a date. Note that any value in the range "31".."99" raises an exception as well. I expect the "30" is the high end of the range because it is currently November and November hath 30 days. It then seems to discard that information and use the current year and month, and assume the day is "23" (or raise an exception if, say, "31" had been entered). Can anyone explain the details of what's going on?

9
  • I get that they parse 23 to 23, but why does "Dogs" get parsed to "november 2015"? And why does 23 get parsed as a valid date, but 3 doesn't?
    – Nzall
    Nov 23, 2015 at 21:03
  • 1
    Something to remember about Date.parse() is that it discards unparseable data and parses whatever's left. The 'Dogs' has no effect on the result. You get the same if you try Date.parse('23') or Date.parse('23 cats') or Date.parse('23 Obamas'). Nov 24, 2015 at 14:39
  • For a further example: irb(main):006:0> Date.parse('On 23 March I got 3 new Dogs') ==> #<Date: 2015-03-23 ((2457105j,0s,0n),+0s,2299161j)> The only thing in that example that it parsed was "23 March" Nov 24, 2015 at 14:41
  • @KenB, yes, but we still need to understand (to be able to sleep at night) why Ruby seems to be saying "23" is the year! [brief pause] "23" is the day!". Nov 24, 2015 at 16:06
  • @CarySwoveland I'm not understanding... in all the examples I've seen, both in this Q/A and in my terminal, '23' is interpreted as the day. The original example was Date.parse('23 Dogs') => Mon, 23 Nov 2015. Or do you mean that the Ruby docs say it should be reading it as the year? Your testing seems to demonstrate that this isn't the case, since e.g. it breaks above 30. Nov 24, 2015 at 16:51
3

So it's got nothing to do with pry. I can reproduce your report in ruby 2.2.2 in ruby code that does not load pry at all.

So why the heck is Date.parse willing to parse "23 dogs" and come up with something? I have no idea. I'd say it's some idiosyncracy or even bug in Date's parsing; it attempts to parse all manner of things, but this leads to some odd edge cases.

For more predictable parsing of dates in known fixed formats, use Date#strptime instead. For more sophisticated parsing of natural language dates in unpredictable formats, use the chronic gem.

Personally, I never use straight Date.parse, because it's kind of unpredictable, using one of those two methods instead. (Or specific format parsing methods like Date.iso8601).

I tried to look at the MRI code for Date.parse because I was curious if I could figure out what it was doing. But quickly got lost in C code I wasn't competent to understand or follow, and had to give up.

Interestingly, this DOES reproduce in JRuby 1.7.10 too (I haven't installed jruby 9x yet). "23 dogs" parses to the same thing, "3 dogs" raises. Hm, perhaps the JRuby Java code will be more intelligible to some of us than MRI's C code. But I haven't had time to try to work through/debug just what Date#parse in JRuby is doing. The meat of it maybe begins here, although I might not have found the correct place for current version implementation. You can see that it tries parsing the date according to a number of different formats in sequence, stopping when it succesfully parses according to some format. We can guess there's SOME weird format in that list that somehow succesfuly parses "23 dogs" but not "3 dogs". It's probalby not an easter egg or intentional at all; it's just a weird side effect of trying to parse a date by just trying to guess what format it is in and trying various formats in sequence, not a very sophisticated algorithm.

update Okay, in at least the jruby code I was looking at (which might not be the current implementation, but is some implementation)

  • Eventually, after trying other potential parses which fail, it tries Date._parse_ddd -- for both inputs.

  • Date._parse_ddd("23 dogs", e) returns true, and fills the Date::Parse::Bag with an mday component, but Date._parse_ddd("3 dogs", e) returns false and does not fill the Bag. So everything else follows from here.

  • If we look at the Date._parse_ddd implementation... there's some monster regexes and weird logic. Probably copied from MRI to be consistent with MRI, or otherwise made consistent with MRI behavior.

  • I don't feel like debugging further. You can if you want. The JRuby implementation, as you can see, is actually written in ruby, not even in Java.

You or I or someone could try to debug further (perhaps even with an interactive debugger on JRuby stdlib implementation) to figure out exactly what's going on. But I'm confident the answer is just basically "it's a weird side effect of Date.parse not really knowing what format it's input is in, but just trying a bunch of things, using a not very sophisticated algorithm, sometimes weird things happen"

more update: Note that Date.parse("03 dogs") does parse instead of raising. So two numerals it decides are parseable, one it does not. But of course Date.parse("3 May") works fine. It's not that Date.parse requires two digit dates, it's just that it's trying a whole bunch of different ways of parsing it, and an actual good date will be caught correctly, but a bad date might be caught by one of the ways that thought it seemed good enough, but in this case was wrong.

more thoughts So it's not intentional that it parses like that. It's a byproduct of heuristic rules meant to catch other dates. Since the code isn't commented we can't say exactly what sorts of dates what parts were meant to catch. It's sort of a bunch of cobbled together stuff to try and catch dates in a variety of formats, including international formats.

You could look at the tests to see all the kinds of dates it's meant to catch. Or you could try to go through the code to understand exactly which lines result in the behavior you are seeing. The code is confusing -- especially the C code in MRI, to most of us. The pure ruby code in JRuby is more readable of course to we rubyists. Since it's confusing and time-consuming to go through the code, with little benefit (who cares?), you probably aren't going to get anyone else to do this for you.

3
  • I had a look at the C source as well, and got no further. We need someone familiar with it to step through and tell us what's happening. Nov 23, 2015 at 21:34
  • What I've noticed is that because of the space between 23 and Dogs, the second capture group is actually 3 characters long, not 2. I'm not sure how this changes things. Could you try doing Date.parse("23dogs") and see what response you get then?
    – Nzall
    Nov 23, 2015 at 22:19
  • Yes, in MRI, "23dogs" returns the same date object as does "23 Dogs". Nov 23, 2015 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.