Advanced Rails - Building Industrial-Strength Web Apps in Record Time

(Tuis.) #1
Date and Time

Date and Time | 63

The accessors are defined just as with regular class attributes:
class Parent
class_inheritable_array :log
self.log = []


def initialize
super
log << "#{self.inspect} created"
end
end

Parent.new

Parent.log # => ["#<Parent:0x10a07c4> created"]

The attributes and their values are inherited to children. This attribute inheritance only
happens at the time of class inheritance. After the child class has inherited from the parent
class, the attributes are separate and do not interact.


class Child < Parent
end

Parent.log # => ["#<Parent:0x10a07c4> created"]
Child.log # => ["#<Parent:0x10a07c4> created"]

Parent.new
Child.new

Parent.log # => ["#<Parent:0x10a07c4> created", "#<Parent:0x109fd9c> created"]
Child.log # => ["#<Parent:0x10a07c4> created", "#<Child:0x109fd88> created"]

Date and Time


Conversions core_ext/date/conversions.rb,core_ext/time/conversions.rb



  • Date#to_time,Date#to_time(:utc),Date#to_date,Time#to_date,Time#to_time: These
    methods allow you to use dates and times in a roughly interchangeable way.

  • Date#to_s(format)formats a date in one of several formats (Table 2-1). The:default
    format is used if no format is specified.

  • Time#to_s(format) formats a date and time in one of several formats (Table 2-2). The
    :default format is used if no format is specified.


Table 2-1. Date formats


Format Example
:default 2006-12-28
:short 28 Dec
:long December 28, 2006
Free download pdf