Basic tech stuff

Programming and Linux administration

Archive for February, 2007

Hard to find documentation about embedding script languages

Posted by Daniel Brahneborg on 2007 February 27

It was quite easy to find information on how to embed Ruby in a C application. The book “Programming in Ruby”, the ruby.h file and some googling, and it got together quite nicely. At least once I learned to always wrap calls with rb_protect and store everything as globals.

Feeling a bit proud, I started looking for information on how to do the same thing with PHP, Perl, Python and whatnot. For some reason, I could hardly find anything at all. Not even the documentation for Parrot contained enough information.

For PHP, it seems one should use something called SAPI. However, neither php.net nor zend.com contained any information about it. For Python it was slightly better, but not much.

Is it really that unique to want to be able to run scripted code from within an application, being able to pass parameters in, getting values back? Odd. Very odd.

Andra bloggar om: , , ,

Advertisements

Posted in programming | Leave a Comment »

Embedded Ruby was quite easy

Posted by Daniel Brahneborg on 2007 February 26

I spent some time embedding the Ruby interpreter into an application today. It was a bit confusing at first, but once I made sure all method calls were surrounded by rb_protect() and stored all variables as globals to keep the garbage collector from eating them, things progressed quickly.

The fun thing is that the documentation said that it wasn’t written to be easy to embed, but it still looked a lot easier than the infinitely complex mod_perl. That says a lot about Ruby, I think.

Posted in programming | Leave a Comment »

Can you write smarter code?

Posted by Daniel Brahneborg on 2007 February 26

Today the Swedish newspaper Computer Sweden has a job ad with the above headline, and a code snippet:

private int extraRounder(float nr) {
  switch (nr) {
    case Float.parseFloat("4.0"): return 4;
    case Float.parseFloat("4.5"): return 4;
    case Float.parseFloat("5.0"): return 5;
    case Float.parseFloat("5.5"): return 5;
    default: return 0;
  }
}

First, using an unnecessary switch is a bit stupid, yes.

Second, it doesn’t even work, since the comparisons with floats never will match due to rounding errors. Because of this, practically all programmers in the world can write smarter code than this (but see the next paragraph). Therefore, the requirements on the applicants can’t be very high. Not really a company I’d like to work for.

The thing that made this really painful though, is that there was a person in my class at the university who actually wrote a function like this, but converting from characters to character codes.

Posted in funny, programming | Leave a Comment »

Standalone code using a Rails database

Posted by Daniel Brahneborg on 2007 February 22

I’m using a Rails application to update a database, but I also want to run an external program on that database once in a while. While it might be done by creating a new thread within the Rails application, I wanted a standalone program. However, it should still use the same database configuration, Ruby models, etc.

First I need to load the Mysql driver, YAML parser, and ActiveRecord:

require 'yaml'
require 'rubygems'
require_gem 'mysql'
require_gem 'activerecord'

Then the load path for ‘require’ must be extended to include my models:

$: << File.join(File.dirname(__FILE__), '../../app/models')

To connect to the database, make sure ActiveRecord uses this connection, and that it gets closed afterwards, I use code like this. The application code is sent as a block.

def with_database(config)
  dbh = nil
  begin
    dbh = Mysql.real_connect(config["host"],
      config["username"], config["password"], config["database"])
    ActiveRecord::Base.establish_connection config
    yield
  ensure
    dbh.close unless dbh.nil?
  end
end

First I load the database config. The “development” profile is hard coded, but could easily be changed into using an environment variable or something.

filename = File.join(File.dirname(__FILE__), '../../config/database.yml')
dbconfig = YAML.load(File.open(filename))
dev = dbconfig["development"]

Then I can run the application code:

with_database(dev) {
# yadda yadda
}

When the function completes, either normally or by raising an exception, the database is closed.

Posted in programming, rails | Leave a Comment »

RSS -> ping service?

Posted by Daniel Brahneborg on 2007 February 20

Are there any services that can take an RSS feed and ping the servers that I want, not just a list of american ones? It can’t be that difficult, can it?

Posted in blogging, programming | 6 Comments »

SOAP drivers suck

Posted by Daniel Brahneborg on 2007 February 19

I’ve been trying for months to write a SOAP driver for our SMS gateway. I want clients in Ruby and PHP. Modern and popular languages, so one would think that the SOAP support would be good.

Not quite. The drivers that are created by the wsdl2xxxx tools suck. Namespaces aren’t respected, causing name clashes everywhere. Tons of xml schema features aren’t supported. Importing and extending wsdl files doesn’t work at all.

So, now I use a hard coded SOAP driver, only using the HTTP and XML libraries on the client side. I have to be more careful, but at least things work now.

Andra bloggar om: , ,

Posted in programming | 4 Comments »