Why Does Date Exist in Ruby Before It Is Required

Why does Date exist in Ruby before it is required?

I believe that date doesn't come from irb, but from rubygems, specifically the file where Gem::Specification is defined:

class Date; end # for ruby_code if date.rb wasn't required

I believe they needed any Date class defined so that the interpreter doesn't complain further down in the Specification class.

Why can I use the Date class without requiring 'date'?

Rubygems (which is required by default) defines a empty, non-functioning Date class in versions earlier than 2.4.0 (Rubygems version, not Ruby). This has been fixed recently (also see the Ruby bug report). The version of Rubygems with the fix will likely be included in Ruby 2.2.

Why is Ruby's Date class automatically loaded but DateTime is not?

Being a little more curious, I tried:

$ ruby -e 'puts DateTime.class'
-e:1:in `<main>': uninitialized constant Object::DateTime (NameError)
[~, kamilski81@mac]
$ ruby -e 'puts Date.class'
-e:1:in `<main>': uninitialized constant Object::Date (NameError)
$ ruby -e 'puts Time.class'
Class

So it makes me think that it's an irb issue that automatically loads 'date'.

Proc.new in Ruby: when do I need to use it?

What Proc.new (and lambda) does is, save all your statements in their original form (in an anonymous function), and doesn't evaluate them.

Date Validator gem must have some kind of test to check if a Proc was passed, and it evaluates it when it's actually validating the stuff.

Edit: It does this here - https://github.com/codegram/date_validator/blob/master/lib/active_model/validations/date_validator.rb#L47

option_value = option_value.call(record) if option_value.is_a?(Proc)

A quick example :

pry(main)> time_now = Time.now
=> 2011-06-19 21:07:07 +0530
pry(main)> time_proc = Proc.new { Time.now }
=> #<Proc:0x9710cc4@(pry):1>
pry(main)> time_proc.call
=> 2011-06-19 21:07:28 +0530
pry(main)> time_proc.call
=> 2011-06-19 21:07:31 +0530
pry(main)>

Note that this will only work with libraries that do implement this kind of check, and not every function accepting a Time.

Ruby Date Gem Invalid Date

You need to pass the format of your date, use

Date.strptime('9/13/2017', '%m/%e/%Y').

How to check if date is valid (Ruby on Rails)

If you cannot validate the pesel numbers in the database first then I would just catch the error and handle it in an rescue block.

To do so change

birth_date = Time.strptime("#{day}/#{month}/#{year}", '%d/%m/%Y')

if birth_date.valid?
self.birth_date = birth_date
else
errors.add(:pesel, I18n.t('activerecord.errors.models.profile.attributes.pesel.invalid'))
end

to

begin
birth_date = Time.strptime("#{day}/#{month}/#{year}", '%d/%m/%Y')
self.birth_date = birth_date
rescue ArgumentError => e
errors.add(:pesel, I18n.t('activerecord.errors.models.profile.attributes.pesel.invalid'))
end

Graceful date-parsing in Ruby

My preferred approach these days is to use Dry::Types for type coercions and Dry::Monads for representing errors.

require "dry/types"
require "dry/monads"
Dry::Types.load_extensions(:monads)
Types = Dry::Types(default: :strict)

Types::Date.try("2021-07-27T12:23:19-05:00")
# => Success(Tue, 27 Jul 2021)

Types::Date.try("foo")
# => Failure(ConstraintError: "foo" violates constraints (type?(Date, "foo"))


Related Topics



Leave a reply



Submit