Everyone of us must be watch someday inside /usr/lib directory at our pretty machine. As you know, there’s a lot of .so files, these files are the famous named “shared libraries”. In fact, those contains reusable code. A practical example of this words can be saw in GNOME and every application built with gtk+. If I need to draw a new window, just call gtk_window_new function and the window is automagicaly generated, i.e., someone else has been written the code to draw a window in the screen, so we just need to use that code. An extra feature is the fact that we don’t put the library at every application that we distribute, just require to user that has the library and the right version of it.
And, what happend if I want to use a shared library inside my application written in Ruby?. Well, that’s easy to do(as everything in Ruby).

The next example uses /usr/lib/libxmms.so.1 file, which came with xmms application.

==== Code begins here ====

#!/usr/bin/env ruby

require ‘dl’

dl = DL::dlopen(‘/usr/lib/libxmms.so.1′)

playpause = dl.sym(“xmms_remote_play_pause”,’0I’)


==== End of fucking fantastic code ====

Let’s explain that code.

  • #!/usr/bin/env ruby just calls ruby interpreter, you’ll see it at most Ruby programs.
  • require ‘dl’ instruction load dl module allowing us to use shared libraries.
  • dl = DL::dlopen(‘/usr/lib/libxmms.so.1’) Opens libxxms.so.1 to use it.
  • playpause = dl.sym(“xmms_remote_play_pause”,’0I’) Uses the “symbol” xmms_remote_play_pause, a defined function and we are telling to ruby that it returns void ‘0’ and the parameters will be just an integer ‘I'(a table of these values will be appear below).
  • playpause.call(0) Invokes the function with 0 as argument.

And that’s it, easy, isn’t it?.

Now you can ask, how the hell you know that libxmms.so.1 has the symbol xmms_remote_play_pause?. That’s simple, you can use ‘nm’ command as follows:

nm –defined-only -D /usr/lib/libxmms.so.1

and print at screen some lines like:

00006816 T xmms_remote_playlist_prev
000061c9 T xmms_remote_play_pause
00006243 T xmms_remote_playqueue_add
00006257 T xmms_remote_playqueue_remove
00006760 T xmms_remote_pl_win_toggle

As you can see, xmms_remote_play_pause is there. More interesting questions comes to our intelectual mindπŸ˜‰, how can I know which arguments receive that function and the values that returns?, well, you can install xmms-dev and check /usr/lib/xmms/xmmsctrl.h and you’ll see the next line:

void xmms_remote_play_pause(gint session);

The function returns ‘void’ and takes gint as argument.

As you remember, we use sym method of DL class, this method takes 2 arguments, first of it gets the name of symbol or function to use, and the next it’s the return and parameters data type values, something like:

playpause = dl.sym(“xmms_remote_play_pause”,’0I’)

We will use xmms_remote_play_pause function and the return and parameter values take the next shape:

‘0I’ The first char always denotes the return data type value of the function, if, like this particular case, the function returns ‘nothing’, we use 0. The ‘n’ next chars denotes the arguments of the function, in this example, just one. ‘I’ says to Ruby that the argument will be an integer.

Next table shows equivalences between C data types and constants that will be used in Ruby.

Data Type

Ruby Value

















double *


float *


long *


int *


short *


char *


Now, you can copy & paste the code of this little app and save it, give it right permissions and execute it.

chmod +x usesofile.rb


As you can imagine, you need to run xmms before execute our small application, then run usesofile.rb and you can note that if xmms is playing it pauses and viceversa.πŸ˜‰

Enjoy your new knowledge.πŸ˜€


One response »

  1. sonny_taz says:


    I would like to learn Ruby =)

    Do you teach me?

    Kisses and hugs my dear.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s