Sunday, December 30, 2007

Understanding include and extend in Ruby

I was watching a presentation by Dave Thomas called, "MetaProgramming - Extending Ruby for Fun and Profit", and realized that my understanding of include and extend is mixed up.

My Wrong Understanding
I thought "extend" will add methods from a module into a class as instance methods and "include" will add them as class methods.

Correct Understand:
"extend" adds methods from a module into a class as class methods.
"include" adds methods from a module into a class as instance methods.

It is quite easy to demonstrate this actually.

module SomeModule
def hi
puts "hello"
end
end

class ExtendSample
extend SomeModule
end

ExtendSample.hi

class IncludeSample
include SomeModule
end

IncludeSample.new.hi

Monday, December 17, 2007

AUTO_INCREMENT MySQL

There is a bug in MySQL that reset AUTO_INCREMENT if an index is added and the table is empty. I found a ticket regarding this issue in Ruby on Rails' bug tracker but it won't be fix because it is a MySQL bug.

Anyway, I reset AUTO_INCREMENT to my desire value after adding indexes to the table and that seems to work. Just remember to not add anymore indexes to the table after you set the AUTO_INCREMENT value or else it will be reset again.

Here is how to set the value manually.

execute "ALTER TABLE table1 AUTO_INCREMENT = 100"

Friday, December 14, 2007

added method to help create and drop foreign key

We have a naming convention at my work place for foreign key so I made two methods to help in the creation and dropping of foreign key. I made them because I got tired of typing out the query everytime.

We name our foreign key like this: fk_table_column_reference_table_column. So a foreign key from a "users" table to the "employers" table would look like this. fk_users_employer_id.

This is how you call the method to create a foreign key.


create_foreign_key(:users, :employer_id, :employers, :id)
drop_foreign_key(:users, :employer_id)



def create_foreign_key(table, column, foreign_table, foreign_column)
execute "ALTER TABLE #{table.to_s} ADD CONSTRAINT fk_#{table.to_s}_#{column.to_s} FOREIGN KEY (#{column.to_s}) REFERENCES #{foreign_table}(#{foreign_column})"
end

def drop_foreign_key(table, column)
execute "ALTER TABLE #{table.to_s} DROP FOREIGN KEY fk_#{table.to_s}_#{column.to_s}"
end

Monday, December 10, 2007

NetBeans 6 - Out of Memory Exception

Lately, I have been getting Out of Memory Exception every time NetBeans load up. This is the exception message, "java.lang.OutOfMemoryError: Java heap space". I am using the daily build of the custom ide for Ruby development.

I fixed the problem by setting the max heap size to a 512 MB. This is how I start the ide.

nbrubyide.exe -J-Xmx512m

UPDATE: I just found out that I can set the max heap size in the nbrubyide.conf file. It is in the etc directory under c:\nbrubyide
I modified it to

default_options="-J-Xms24m -J-Xmx256m"

Friday, December 07, 2007

Rails 2.0.1 is out

I am sure this is all over the web but Rails 2.0.1 is out and here is the announcement. I probably won't use it for my current project until after we do a release.

Monday, December 03, 2007

Mongrel installed in wrong location?

I just reinstall my rails stack to use ruby 1.8.6 and mongrel wouldn't start. I kept getting this error instead.

C:/languages/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require': no such file to load -- C:/languages/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.1-x86-mswin32-60/lib/mongrel/init.rb (MissingSourceFile)

Which is strange since Mongrel is one of the install gem when I do gem list. Here are my install gems.

*** LOCAL GEMS ***

actionmailer (1.3.6)
actionpack (1.13.6)
actionwebservice (1.2.6)
activerecord (1.15.6)
activesupport (1.4.4)
capistrano (2.1.0)
cgi_multipart_eof_fix (2.5.0)
gem_plugin (0.2.3)
highline (1.4.0)
mongrel (1.1.1)
needle (1.3.0)
net-sftp (1.1.0)
net-ssh (1.1.2)
rails (1.2.6)
rake (0.7.3)

I can see mongrel is installed under mongrel-1.1.1-mswin32 but the system was trying to look for mongrel under mongrel-1.1.1-x86-mswin32-60.

I rename my mongrel directory to mongrel-1.1.1-x86-mswin32-60 and everything works. I wonder if I did something wrong or Mongrel just installed in the wrong directory. I am using gem 0.9.5 and it just figures out the correct version of mongrel and install it by itself so I didn't have the chance to pick which version of mongrel I wanted to install.