How to see the class creation time in Ruby?

In Ruby any chance to see the last-modified time of a specific class?

Say the following class I have created on 27/03/2012:

class Foo
end
=> nil

After that I reopened the same class to add more methods into it on
10/11/2012:

class Foo
def show
p “hi”
end
end
=> nil

Any chance to track the those time for the class “Foo”
pro-grammatically?

Sure

irb(main):001:0> class Object
irb(main):002:1> def self.method_added method_name
irb(main):003:2> @last_modified = Time.now
irb(main):004:2> end
irb(main):005:1> def self.last_modified
irb(main):006:2> @last_modified
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):009:0> class Foo
irb(main):010:1> def bar
irb(main):011:2> puts “bar”
irb(main):012:2> end
irb(main):013:1> end
=> nil
irb(main):014:0> Foo.last_modified
=> 2013-02-23 12:30:57 -0500

Keep in mind, this is only during execution, you would need to store the
data externally (flat files, database, etc.) if you needed to know this
information across different executions.

To predict your next question you can use

ObjectSpace’s define_finalizer to automatically write out these files.
See:

And if you have further questions make sure you try some stuff out
first.

Matt M. wrote in post #1098601:

Sure

irb(main):001:0> class Object
irb(main):002:1> def self.method_added method_name
irb(main):003:2> @last_modified = Time.now
irb(main):004:2> end
irb(main):005:1> def self.last_modified
irb(main):006:2> @last_modified
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):009:0> class Foo
irb(main):010:1> def bar
irb(main):011:2> puts “bar”
irb(main):012:2> end
irb(main):013:1> end
=> nil
irb(main):014:0> Foo.last_modified
=> 2013-02-23 12:30:57 -0500

Does the @last_modified is being updated on the time of “Foo” class
creation?

One more thing to say here is below:

foo = Array.new(2,15)
=> [15, 15]

foo.last_modified
NoMethodError: undefined method last_modified' for [15, 15]:Array from (irb):15 from /usr/bin/irb:12:in

Why the above error coming,whereas Array.methods showing it present in
ruby?

Array.methods
=> [:[], :try_convert, :method_added, :last_modified, :allocate, :new,
:superclass, :freeze, :===, :==, :<=>, :<, :<=, :>, :>=, :to_s,
:included_modules, :include?, :name, :ancestors, … etc

Array != Array.new

That’s why

Ohh! here I tried but none of them worked.

foo.object_id.last_modified
NoMethodError: undefined method last_modified' for 10535580:Fixnum from (irb):18 from /usr/bin/irb:12:in

ObjectSpace._id2ref(foo.object_id).last_modified
NoMethodError: undefined method last_modified' for [15, 15]:Array from (irb):19 from /usr/bin/irb:12:in

ObjectSpace._id2ref(foo.object_id)
=> [15, 15]

Array.try_convert(foo)
=> [15, 15]

Array.try_convert(foo).last_modified
NoMethodError: undefined method last_modified' for [15, 15]:Array from (irb):22 from /usr/bin/irb:12:in

Could you tell me the trick?

There’s an easy way to find out.

Xavier R. wrote in post #1098609:

Could you tell me the trick?

irb(main):052:0> class Array
irb(main):053:1> def leech
irb(main):054:2> end
irb(main):055:1> end
=> nil
irb(main):056:0> Array.last_modified
=> 2013-02-23 13:15:52 -0500

Matt M. wrote in post #1098610:

Xavier R. wrote in post #1098609:

Could you tell me the trick?

irb(main):052:0> class Array
irb(main):053:1> def leech
irb(main):054:2> end
irb(main):055:1> end
=> nil
irb(main):056:0> Array.last_modified
=> 2013-02-23 13:15:52 -0500

Yeah! its good but not one I am looking for, may be what I am looking
for is not also possible.

Xavier R. wrote in post #1098612:

Yeah! its good but not one I am looking for, may be what I am looking
for is not also possible.

no you are not, it was the other user that was looking for that, not
you, or is the the proof that you two are the same user?

the problem is that you dont understand the difference between an object
of a class and the class itself

in this case, last_modified was defined as class method, not as an
instance method so Array.last_modified works,but Array.new.last_modified
does NOT work oO

Hans M. wrote in post #1098615:

Xavier R. wrote in post #1098612:

Yeah! its good but not one I am looking for, may be what I am looking
for is not also possible.

no you are not, it was the other user that was looking for that, not
you, or is the the proof that you two are the same user?

the problem is that you dont understand the difference between an object
of a class and the class itself

Yes,I know this is not my thread.But tried to use the above code,but it
is not possible ever as per the implementation.

But one request to you,please don’t be off-topic. As there is no rule
that I can’t catch other users thread.

Xavier R. wrote in post #1098616:

But one request to you,please don’t be off-topic. As there is no rule
that I can’t catch other users thread.

I wanted to laugh, but I just shook my head instead.

No, you can’t track the last_modified on a plain old ruby object. It
would have to be implemented at the language implementation level and I
don’t know of a single language that supports this.

Guys, “Love U Ruby” is just trolling.

Best ignore him until he gets bored.

Look at his other “questions” - he
is evidently fake. Never a real use case
for any of his question.