Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate DateTime object in Ruby

A constructor in following code receives a DateTime object in the parameter

class Test
    attr_accessor :dateTime

    # Takes DateTime object as input
    def initialize(dateTime)
        @dateTime = dateTime
    end
end

How can I validate if dateTime parameter passed to the constructor is valid DateTime object or not?

One way to find out is by using:

if dateTime.class == DateTime

But is there a better way to do it?

Remember, dateTime is DateTime object and not a string

like image 439
Trivikram Avatar asked Jun 12 '26 13:06

Trivikram


1 Answers

You can check it by dateTime.is_a?(DateTime) it will return boolean

like image 193
bor1s Avatar answered Jun 15 '26 01:06

bor1s



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!