The Passionate Craftsman

Ruby, PHP, MySql, Software engineering and more.

Friday, 23 October 2009

Rails default route

If yuo want to map your Rails' root directory, something like, to a controller, use this directive in your cong/routes.rb:

map.root :controller => 'configurations'

'configurations' is the controller that you might want to map. 'map.root' means 'map the root'.


How to install Mysql/Ruby on Solaris SPARC 64

I found only partial documentation on how to install the Ruby driver for MySql. There are two version of drivers, one is MySql/Ruby and is written in C, the second is Ruby/MySql and is written in Ruby. Ruby/MySql is included in Rails but has slower performance, plus it was not working with MySql 5.0, it does not work with MySql 4.something too!

So I decieded to compile the C driver. Below you can find the commands:

curl -o mysql-gzip -d gzip -d mysql-ruby-2.7.tar.gz
tar xfv mysql-ruby-2.7.tar
cd mysql-ruby-2.7
ruby extconf.rb --with-mysql-include=/usr/local/mysql/include/mysql --with-mysql-lib=/usr/local/mysql/lib/mysql
sudo make install

Make sure that 'make install' will be executed as root. Now you can test the connection with:

ruby test.rb [hostname] [username] [dbpassword]

Of course you have to put the host, username and password. Another nice script to test your MySql connection is:

require 'mysql'

mysql = Mysql.init()
mysql.connect('localhost', 'username', 'password')
results = mysql.query("SELECT now();")
results.each{|row|; puts row;}

Labels: ,

Thursday, 22 October 2009

Ruby database recipes with MySql

First of all I am using Windows as OS (yes, I have to use it here).

Rails has a very nice ORM: ActiveRecord. However ActiveRecord is quite complex and a simple library to manipulate data in a script can be more than enough. In this post I will use MySql as DBMS. First of all you need the MySql adapter and the MySql gem:

gem install mysql

If you have troubles when installing the gem and you have the MySql 5.1 client, download and install the 5.0 instead, there are, at the moment of writing some issues in Windows. Now you can check if you can connect to the database. The following example is the code to manipulate data in a MySql table named users. As you can see it is not so different from the previous example, but it can be use with other DBMSs, this is the beauty of a database abstracion layer, using the adapter design pattern.

require 'dbi'

# Connect to a database, old style
dbh = DBI.connect('DBI:Mysql:test:localhost', 'root', 'asdf')

# Insert some rows, use placeholders
1.upto(13) do |i|
sql = "insert into users (username) VALUES (?)", "user#{i}")

# Select all rows from users
sth = dbh.prepare('select * from users')

# Print out each row
while row=sth.fetch do
p row

# Close the statement handle when done

# Don't prepare, just do it!'delete from users where id > 10')

# And finally, disconnect
Some useful links:

Labels: , ,

Sunday, 18 October 2009

JRuby, a thread safe Ruby implementation for multi cores

Ruby 1.8 was not thread safe, now the problem with 1.9 is ended, but there is the global interpreter lock, that prevents the MRI interpreter to scale on multiple cores. JRuby is not written in C, it is written using Java and uses Java threads. Java threads are thread safe and can scale on multiple cores, as any J2EE application.

Please, have a look this Google Tech video by Ola Bini, a JRuby core developer. He talks about JRuby, his threads, garabage collection and more.

Ola Bini works for ThoughtWorks, he has developed another language called Loke. Loke is built to run on the JVM and borrows many ideas from Lisp.

Labels: , , ,

History of Mathematics on Youtube

Labels: , ,