Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions




Ruby Programming
Previous Page Home Next Page

An Example

Okay, now we're ready for a full-size example. Given our vendor's header file above, we write the following code.

#include "ruby.h"
#include "cdjukebox.h"

VALUE cCDPlayer;

static void cd_free(void *p) {   CDPlayerDispose(p); }

static void progress(CDJukebox *rec, int percent) {   if (rb_block_given_p()) {     if (percent > 100) percent = 100;     if (percent < 0) percent = 0;     rb_yield(INT2FIX(percent));   } }

static VALUE cd_seek(VALUE self, VALUE disc, VALUE track) {   CDJukebox *ptr;   Data_Get_Struct(self, CDJukebox, ptr);

  CDPlayerSeek(ptr,                NUM2INT(disc),                NUM2INT(track),                progress);   return Qnil; }

static VALUE cd_seekTime(VALUE self) {   double tm;   CDJukebox *ptr;   Data_Get_Struct(self, CDJukebox, ptr);   tm = CDPlayerAvgSeekTime(ptr);   return rb_float_new(tm); }

static VALUE cd_unit(VALUE self) {   return rb_iv_get(self, "@unit"); }

static VALUE cd_init(VALUE self, VALUE unit) {   rb_iv_set(self, "@unit", unit);   return self; }

VALUE cd_new(VALUE class, VALUE unit) {   VALUE argv[1];   CDJukebox *ptr = CDPlayerNew(NUM2INT(unit));   VALUE tdata = Data_Wrap_Struct(class, 0, cd_free, ptr);   argv[0] = unit;   rb_obj_call_init(tdata, 1, argv);   return tdata; }

void Init_CDJukebox() {   cCDPlayer = rb_define_class("CDPlayer", rb_cObject);   rb_define_singleton_method(cCDPlayer, "new", cd_new, 1);   rb_define_method(cCDPlayer, "initialize", cd_init, 1);   rb_define_method(cCDPlayer, "seek", cd_seek, 2);   rb_define_method(cCDPlayer, "seekTime", cd_seekTime, 0);   rb_define_method(cCDPlayer, "unit", cd_unit, 0); }

Now we have the ability to control our jukebox from Ruby in a nice, object-oriented manner:

require "code/ext/CDJukebox"
p =
puts "Unit is #{p.unit}", 16) {|x| puts "#{x}% done" }
puts "Avg. time was #{p.seekTime} seconds"
Unit is 1
26% done
79% done
100% done
Avg. time was 1.2 seconds

This example demonstrates most of what we've talked about so far, with one additional neat feature. The vendor's library provided a callback routine---a function pointer that is called every so often while the hardware is grinding its way to the next disc. We've set that up here to run a code block passed as an argument to seek. In the progress function, we check to see if there is an iterator in the current context and, if there is, run it with the current percent done as an argument.
Ruby Programming
Previous Page Home Next Page

  Published under the terms of the Open Publication License Design by Interspire