Skip to content

Hybrid string date objects' known gotchas

Pedro Fayolle edited this page May 11, 2020 · 1 revision

Date.=== comparison

E.g.:

Date === string_date_instance # => false

The above can affect case conditions, as trying to match a StringDate with:

case obj
when Date
  # ...

...will not work.

Instead one must specify the FmRest::StringDate class:

case obj
when Date, FmRest::StringDate
  # ...

StringDate#eql? only matches other strings, not dates.

TODO: Verify if this can affect hash indexing when a StringDate is used as a key.

StringDate#succ and StringDate#next always return a String

StringDate#succ and StringDate#next return a String, despite Date#succ and Date#next also existing.

Workaround: Use StringDate#next_day or string_date + 1

StringDate#to_s returns the original string, not the Date's string

representation

Workaround: Use strdate.to_date.to_s

StringDate#hash returns the hash for the string, not the Date

This is of course important when using a StringDate as a hash key.

TODO: Verify.

StringDate#as_json returns the string

Workaround: Use strdate.to_date.as_json

Asymmetrical equality (==) with Date

str_date == date #=> true
date == str_date #=> false

Calling string-transforming methods (e.g. upcase) returns a StringDate

instead of a String

TODO: Fix this.