Thursday, May 8, 2008

Padding numbers

If you want output as 123.90:
sprintf("%0.2f","123.9")

Tuesday, May 6, 2008

File.new

File.new(file_name).close won't do what I intend to do, i.e. create a file. Instead File.new(file_name,'w').close does the trick. The problem is in the first case the second argument defaults to 'r' which means read. Its strange that parameter is defaulted to read only. When we create file we normally mean we are creating it to write something to the file. So it must default to 'w'.

Saturday, May 3, 2008

DangerousAttributeError

I came across this weird sounding exception while creating a record. At first I didn't know what was wrong with my table. After a little probing I figured it was being caused by an attribute called 'frozen' in my table. I was using it to indicate if that record is frozen or not. But it appears Rails uses this keyword internally for the same purpose I was using that attribute for. I had to change this attribute to a different name (I named it 'frosen') for it to work.

This is what Rails api says about the error:
Raised when attribute has a name reserved by ActiveRecord (when attribute has name of one of ActiveRecord instance methods).

http://api.rubyonrails.com/classes/ActiveRecord/DangerousAttributeError.html