Based on your code, obj.expireAt
appears to be a simple integer. Normal best practice in Ruby would be to make that attribute of the object an instance of Time
or DateTime
instead. (See this question for discussion of the differences between those two options.) If your data comes in as integers, do the conversion once when storing the value in the instance variable, rather than repeatedly when accessing it for other purposes. You can always cache the integer and make it available as well via another method if you need to get it back out without running the conversion backwards.
This is considered best practice because it makes the attribute self-documenting and self-contained. You can inspect an object and determine what its class is (although doing so programmatically is considered bad form in Ruby, where we generally prefer Duck Typing). If I look at the value I get back from expireAt
and see that it's a Fixnum
, that doesn't tell me anything useful about it. I have to infer that it is a timestamp, and do extra work to convert it to a value with useful time-related semantics. Whereas if it's a Time
, not only do I know what it is, but I have all the methods of that class available to manipulate it.
Since everything in Ruby is an object (and therefore an instance of some class), you might as well use a class designed to be used the way your value is used.