Any easy way to check if a variable / object is of Date
/ Time
/ DateTime
type? Without naming all the types
The first() is an inbuilt method in Ruby returns an array of first X elements. If X is not mentioned, it returns the first element only. Syntax: range1.first(X) Parameters: The function accepts X which is the number of elements from the beginning. Return Value: It returns an array of first X elements.
Ruby | Time now() function The now() is an inbuilt method in Ruby returns the current time.
Another option:
def is_datetime(d)
d.methods.include? :strftime
end
Or alternatively:
if d.respond_to?(:strftime)
# d is a Date or DateTime object
end
you can inspect the class of a object doing
object.class
It should return Date, String or whatever it is. You can also do the reverse and check if an object is an instance of a class:
object.instance_of?(class)
where class is the one you want to check (String, Date), returning true/false
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