Forum: Ruby how to see the class creation time in Ruby?

Posted by Love U Ruby (my-ruby)
on 2013-02-23 16:37
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?
Posted by Matt Mongeau (halogenandtoast)
on 2013-02-23 18:31
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
Posted by Matt Mongeau (halogenandtoast)
on 2013-02-23 18:32
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.
Posted by Matt Mongeau (halogenandtoast)
on 2013-02-23 18:34
To predict your next question you can use

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

http://www.ruby-doc.org/core-1.9.3/ObjectSpace.html#M001527

And if you have further questions make sure you try some stuff out 
first.
Posted by Xavier R. (arup_r)
on 2013-02-23 18:39
Matt Mongeau 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?
Posted by Matt Mongeau (halogenandtoast)
on 2013-02-23 18:54
There's an easy way to find out.
Posted by Xavier R. (arup_r)
on 2013-02-23 19:01
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 `<main>'

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
Posted by Matt Mongeau (halogenandtoast)
on 2013-02-23 19:03
Array != Array.new

That's why
Posted by Xavier R. (arup_r)
on 2013-02-23 19:09
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 `<main>'
>> 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 `<main>'
>> 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 `<main>'
>>

Could you tell me the trick?
Posted by Matt Mongeau (halogenandtoast)
on 2013-02-23 19:16
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
Posted by Xavier R. (arup_r)
on 2013-02-23 19:44
Matt Mongeau 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.
Posted by Hans Mackowiak (hanmac)
on 2013-02-23 19:53
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
Posted by Xavier R. (arup_r)
on 2013-02-23 19:59
Hans Mackowiak 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.
Posted by Matt Mongeau (halogenandtoast)
on 2013-02-23 21:06
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.
Posted by Marc Heiler (shevegen)
on 2013-02-23 22:14
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.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.