Posts for: #Ruby

Ruby Time marshaling bug in pre-1.9

Ruby’s Time has a bug: when try to serialize an timezoned time, then unserialize it back, the result will use the server local time, instead of use the original time zone.
>> utc = Time.now.utc => Fri Aug 29 09:07:37 UTC 2008 >> marshaled = Marshal.dump utc => "\004\bu:\tTime\r\251\037\e\200\344\254T\036" >> Marshal.load marshaled => Fri Aug 29 11:07:37 +0200 2008

This bug doesn’t affects Ruby 1.9, but we still don’t use that version for production purpose. If you use 1.8.x with Rails 2.1.0, your cached timestamps (including ActiveRecord objects), are probably wrong.
>> comment = Comment.first => #<Comment id: 865423346, post_id: 1, text: "Nice post.", created_at: "2008-08-29 09:27:48", updated_at: "2008-08-29 09:27:48"> >> Rails.cache.write('comment', comment) => true >> Rails.cache.read('comment') => #<Comment id: 865423346, post_id: 1, text: "Nice post.", created_at: "2008-08-29 09:27:48", updated_at: "2008-08-29 09:27:48"> >> comment.update_attributes :text => "Nice post!" => true >> Rails.cache.write('comment', comment) => true >> Rails.cache.read('comment') => #<Comment id: 865423346, post_id: 1, text: "Nice post!", created_at: "2008-08-29 09:27:48", updated_at: "2008-08-29 11:28:42">

Look at the last updated_at attribute, it uses local time instead of UTC time zone.
The first time everything goes right, because #updated_at wasn’t invoked and casted to a Time instance. It’s a string, and the marshaling is ok.
But, when I update the object, ActiveRecord changes the value of that timestamp, but before, it cast it to a Time, and everything goes wrong.

[]

Released Sashimi 0.1.6

I just released a new version of Sashimi, with tiny fixes and a new home!

~

In fact the project is also hosted on RubyForge, now you can install the gem with:
$ (sudo) gem install sashimi
or with:
$ (sudo) gem install jodosha-sashimi --source=http://gems.github.com

If you wish, you can visit the project pages on GitHub and on RubyForge.

UPDATE The release 0.1.6 is broken, you are strongly encouraged to update your gem with the newest 0.1.7.

[]

Ruby: Class Methods Proxy

Did you ever used class methods inside your instance methods? If yes, you probably noticed how frustrating can be to use the self.class.my_class_method syntax. A solution could be to create a private method which encapsulates the class one.
class Repository def self.path @@path end

def print_path puts path end

private def path self.class.path end end
In the above example, #print_path can print the @@code value, without worrying about it’s a class value, because we have wrapped it.

[]