Reputation: 18762
Ruby version: ruby 2.0.0p247 (2013-06-27) [x64-mingw32]
Why does below code able to create Date
object but unable to invoke a valid method on it?
Code version 1
p d = Date.new # Works fine - Prints - #<Date:0x000000027aa628>
p Date.gregorian_leap?(2016) # undefined method `gregorian_leap?' for Date:Class (NoMethodError)
Code version 2 Above code works fine if we add require
statement
require 'date'
p d = Date.new # Prints #<Date: -4712-01-01 ((0j,0s,0n),+0s,2299161j)>
p Date.gregorian_leap?(2016) # Prints true
Which Date
class is getting instantiated in version 1 above? Does Ruby have concept of fully qualified class name which we can inspect to find the difference in two cases?
Ruby version: ruby 2.2.2p95 (2015-04-13 revision 50295) [x64-mingw32]
In Ruby 2.2
, Date.new
fails early - does not misbehave like Ruby 2.0 did
p d = Date.new # uninitialized constant Date (NameError)
p Date.gregorian_leap?(2016) # did not reach here, previous line errored out
Upvotes: 3
Views: 40
Reputation: 18762
As pointed by Marek Lipka (in comments section),
It's because Ruby 2.0 had an empty Date class for compatibility reasons.
Reference: bugs.ruby-lang.org/issues/9890
Upvotes: 2