Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

Friday, August 20, 2010

Installing the MySQL RubyGem on Ubuntu

I have successfully installed the MySQL driver for Ruby on Windows and Mac OS X but I had not installed it on Ubuntu until today.

I am assuming that you have already installed the MySQL server.  I am using MySQL 5.1.
$ sudo apt-get install mysql-server

Install the MySQL database client library.  I am not certain of this but I believe that the number '15' in 'libmysqlclient15-dev' indicates the version.  This is apparently the most current one and the default version on Ubuntu 10.4 as of today.
$ sudo apt-get install libmysqlclient15-dev

Install RubyGems if you have not done so already.  I am using Ruby 1.9.1.
 $ sudo apt-get install rubygems1.9

If everything was successfuly, then you should be able to use the mysql RubyGem in  your Ruby scripts.

Thursday, August 12, 2010

Formatting Hex Strings in Ruby

This method converts a 1 or 2 character string representing a hex number into a 2 character string representing a hex number.

Usage examples:

irb(main):001:0>format("a")
=> "0A"
irb(main):002:0>format("2D")
=> "2D"


Implementation:

def format_hex_string(byte)
    byte.upcase
    byte.to_i(16) >= 16 ? byte : "0" + byte
end