Add something new to Virb:

Virb

Are you sure you want to delete that?

or Cancel

 

Posted on Jan 29, 2008

Subclassing Time on Solaris with Ruby

UPDATE: This is a bug with Ruby 1.8.5 -- Ruby 1.8.6 fixes this problem:


On Solaris, Time.parse always returns a Time object, regardless of whether .parse was called from a descendant object or not. On other platforms Time.parse would return an object of the same class as it's caller. However, Time.at which creates a time from a number of seconds does create the proper kind of object. Thus the algorithm Time.at(Time.parse(TIME).to_i) gets us what we want. I tried to measure the performance hit of doing it this way instead, and I'm no expert, but they seem to be pretty negligible.. This is running benchmark on the Time.parse (#1) method versus Time.at(Time.parse(TIME).to_i) (#2)5000 times on a Solaris box:/script/performance/benchmarker 5000 'Time.parse("2007-01-01")' 'Time.at(Time.parse("2007-01-01").to_i)' user system total real#1 0.290000 0.030000 0.320000 ( 0.312148)#2 0.310000 0.030000 0.340000 ( 0.346706)Anyone have a better recommendation? Re-implementing Time.parse in our descendant class isn't really feasible. I'm curious if this is a bug in the Ruby interpreter, or whether there's just no other way on a Solaris box. The internals of the Time class aren't pure Ruby and redefining Time#parse isn't such an easy task.And just changing the class on runtime is of course, evil: http://eigenclass.org/hiki.rb?evil.rb+dl+and+unfreeze

Loading comments...

Likes

Details

Viewed 27 times

© 2008 Reality Technician

virb.com/t/437982
tweet!

Flag this text post!

Flag this text post as:

or Cancel

 

Advertisement

Flag this profile!

Flag this profile as:

or Cancel