Published by marco on 2009-06-02
This should sound like a very easy task, but it took me a while to sort this out, so I thought I’d share it. If you want to install the mysql gem with Ruby1.9 try this:
sudo gem1.9 install kwatch-mysql-ruby
--source=http://gems.github.com/
-- with-mysql-config=/usr/local/mysql/bin/mysql_config
Where in the –with-mysql-config option you have to specify the path to mysql_config in the bin directory in your mysql installation path.
In Mobile Interactive Technology we are trying to make our new applications compatible with Rails 2.3 and Ruby1.9. Not an easy task though. An invaluable source of info on compatibility of gems with Ruby1.9 is isitruby19.com.
Published by marco on 2009-02-03
This small hack adds attribute accessors to a Perl class. The idea is to add to a Perl package behavior similar to a Ruby class, when you can specify attribute accessors (getters and setters) like this:
Here is the code we have written. Basically all you need to do is add the sub attr_accessor to your Perl class, and then call this method with the attribute you want the getter and setter for.
package MyPackage;
sub new {
my $class = shift;
bless {}, $class;
}
sub attr_accessor {
$attr = shift;
eval <<ATT;
sub $attr {
my \$self = shift;
my \$value = shift;
if( \$value ) {
my \$old_value = \$self->{ $attr };
\$self->{ $attr } = \$value;
return \$old_value;
} else {
return \$self->{ $attr };
}
}
ATT
}
attr_accessor 'name';
In the code above we have added accessors methods for the attribute ‘name’. We can then set and get the ‘name’ attribute as in the following code:
my $t = new MyPackage;
$t->name('marco'); # setter method
print $t->name # this will get and print 'marco'
Enjoy!