Skip to content

Categories:

How to easy inject dependencies in ruby

class Injector
  def self.register(block_id, &block)
    raise ArgumentError.new('Block not given') unless block_given?
    @@injections ||= { }
    @@injections.merge!(block_id => block)
  end
 
  def self.injections
    @@injections
  end
end
 
class Class
  def inject(hash)
    raise ArgumentError.new('method_name => block_id hash expected') unless hash.instance_of? Hash
    hash.each do |method_name, block_id|
      class_eval do
        define_method(method_name) do |*args|
          Injector.injections[block_id].call(*args)
        end
      end
    end
  end
end
 
class A
  inject :attribute => :value
  inject :attribute_param => :value_with_param
 
  def call_it
    puts respond_to? :attribute
    puts attribute
 
    puts respond_to? :attribute_param
    puts attribute_param('aaaaaa')
  end
end
 
Injector.register(:value) { 'bugabu' }
Injector.register(:value_with_param) { |p| "bugabu -> #{p}" }
 
 
test = A.new
test.call_it

It will output:


true
bugabu
true
bugabu -> aaaaaa

Posted in Ruby, Solving Problems.

Tagged with , , , , .


Ruby Class Initialize by variable dynamically

If you need to initialize a class in ruby but you don’t know the name, or ir will be created dinamically, try this:

class MyClass
  def foo
    puts "bar"
  end
end
 
v = 'MyClass'
klass = Object.const_get(v)
klass.new

Posted in Ruby.

Tagged with , , .


g2c.h missing for linalg ruby in ubuntu 10.11

Recently I was learning some stuff of machine learning and SVG. I found linalg that implements linear algebra library write in fortran: LAPACK.

I got this annoying error:

config ext/lapack
checking for g2c.h... no
A full LAPACK installation was not found.
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options.

Provided configuration options:
–with-opt-dir
–with-opt-include
–without-opt-include=${opt-dir}/include
–with-opt-lib
–without-opt-lib=${opt-dir}/lib
–with-make-prog
–without-make-prog
–srcdir=.
–curdir
–ruby=/home/marco/.rvm/rubies/ruby-1.9.3-p125/bin/ruby
install.rb:152:in `block in config’: configuration failed (RuntimeError)
from install.rb:22:in `block (2 levels) in each’
from install.rb:21:in `chdir’
from install.rb:21:in `block in each’
from install.rb:18:in `each’
from install.rb:18:in `each’
from install.rb:149:in `config’
from install.rb:231:in `auto’
from install.rb:240:in `’
from install.rb:94:in `’

I searched for this g2c.h header file and don’t find it. Well, i find somewhere a tip that is just replace g2c.h to f2c.h. It worked for me. Just remember to install gfortran lib64gfortran3 cfortran packages:

sudo apt-get install gfortran lib64gfortran3 cfortran
marco@macubuntu:~/d/linalg-1.0.1-i686-darwin9-ruby19/ext$ grep "g2c.h" * -R
lapack/mkmf.log:have_header: checking for g2c.h... -------------------- no
lapack/mkmf.log:conftest.c:3:17: fatal error: g2c.h: No such file or directory
lapack/mkmf.log:3: #include <g2c.h>
lapack/extconf.rb:         # these are in g2c.h
lapack/extconf.rb:      unless have_header("g2c.h") and
lapack/rb_lapack.h:#include <g2c.h>
linalg/xmatrix.h.tmpl:#include <g2c.h>
linalg/dcomplex.h:#include <g2c.h>
linalg/extconf.rb:unless have_header("g2c.h") and
linalg/xdata.h:#include <g2c.h>
marco@macubuntu:~/d/linalg-1.0.1-i686-darwin9-ruby19/ext$

Just change every of this files. search g2c.h to f2c.h and it will compile like a charm :D

Posted in Solving Problems.

Tagged with , , .




Easy AdSense Lite by Unreal