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
You can check it by dateTime.is_a?(DateTime) it will return boolean
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With