Monthly Archives: November 2012

You are browsing the site archives by month.

What you should know before walk into a job interview for a developer position

You could be an awesome developer or a really bad one, it doesn’t matter, a job interview is something different. It’s like doing a crossword for a writer or a drive license test for a f1 driver, they should do it just ok, but a bit of practice before won’t hurt.

Things I’d recommend:

  • To code something while other people are watching you (if you have a blackboard, the better): I don’t know about you, but I get really nervous if someone is looking at my screen, I start to do a lot of typos, even with really short shell commands and don’t even try to think and write logical stuff. One thing sort of work for me is to (try to) think out loud in those situations.
  • Remember basic data types: Nowadays we got into bad habits of using a lot of libraries so we forgot about really basic stuff, do you know how to write a basic “set” date type from zero? a list? a balanced tree? 10 years ago I knew it, right now probably it would take me much more time.
  • Algorithm’s costs: more of the same. It’s quite easy, but you should remember and take a look into this, again.
  • Learn name of things: once thing is to have an anonymous function without being bound to some identifier and another different thing is to have a lambda. It’s not the same to have a class that exposes the functionality that to have a façade. Well, in fact those things are the same, but it helps if we speak the same language.
  • Do not panic: it’s not about answer quickly, take it easy.

May the source be with you.

Create ruby/jruby bindings of C++/C with ffi

Let’s start with the basic. What are bindings? It’s a way to call low level libraries (normally in C or C++) from another language (high level one, like ruby, java or whatever). This simple step requires two important things: 1) first, to know how we should call the method itself and 2) second, how to map the type from the high level language to C primitives types. To accomplish this, we have what is called foreign function interface which trivialize this task.

In ruby/jruby world we are gonna need ffi gem (jruby also has a compatible ffi gem), besides that the most important part is to have a clear interface in C. You can do bindings for a C++ library if you create a C interface first (because the bindings between C++ and C are free, the compiler knows how to do it by itself). So let’s cut the crap and write some code

First our C++ library which can be this few lines or thousands:

class awesome_object {
  public:
    awesome_object(int a, int b) : a_(a), b_(b) {}
 
    int awesome_method() {
      return a_ + b_;
    }
 
  protected:
    int a_, b_;
}

Now the C bindings which will be our façade for our c++ library:

extern "C" {
  typedef awesome_object_p void *;
 
  awesome_object_p create_awesome_object(int a, int b) {
    return static_cast<void *>(new awesome_object(a, b));
  }
 
  void delete_pointer_to_awesome_object(awesome_object_p p) {
    delete static_cast<awesome_object *>(p);
  }
 
  int awesome_method(awesome_object_p p) {
    awesome_object o = *static_cast<awesome_object *>(p);
    return o.awesome_method();
  }
}

Now ruby code that use ffi gem to be able to call the methods defined in C:

require 'ffi'
 
module AwesomeObject
  include FFI
  extend FFI::Library
  ffi_lib "libawesome.so" # .dll if you are in windows, it doesn't matter the OS
  attach_function "delete_pointer_to_awesome_object", "delete_pointer_to_awesome_object", [:pointer], :void
  attach_function "awesome_method", "awesome_method", [:pointer], :int
  attach_function "create_awesome_object", "create_awesome_object", [:int, :int], :pointer
end

With this you can use your C++ library from ruby code (I’d recommend package everything as a gem) just like this:

class MyTest
  include AwesomeObject
  def test!
    object = create_awesome_object(11, 31)
    awesome_method object
    delete_pointer_to_awesome_object object
  end
end
MyTest.new.test!

So, if you have read until now, probably you just want something working, say no more. Download this tar.gz, and see all this by yourself. In the tar you have the code split into c/h cpp/hpp files as it should be, in the post I’ve put all together to simplify things. Just execute make test and if you have ruby, ffi gem and g++ installed on your system, you’ll see something like:

$ make test
g++ -g -Wall -fPIC -c awesome_object.cpp
g++ -g -Wall -fPIC -c awesome.c
g++ -g -Wall -fPIC -shared -o libawesome.so awesome.o awesome_object.o
ruby test.rb
42