The Passionate Craftsman

Ruby, PHP, MySql, Software engineering and more.

Saturday 15 December 2012

Raspberry Pi

Today I received my Raspberry Pi! I bought this book: Raspberry Pi User Guide. It is short, simple and a practical book. Most of the book is written for Linux newbies but the second part has an expalantion on how ot use the GPIO. I realised I can power the board with an iPhone power supply but I need a micro USB to deliver the power... I am using my Kindle USB cable! The box wrapping the Pi could be used as a 'PC case', but I will have to drill it to connect the cables. I downloaded this program to flash the 2GB SD card: http://alltheware.wordpress.com/2012/12/11/easiest-way-sd-card-setup/. The Linux distro to be flashed in the card is available on the Pi web site, I download the Debian image but I think I will try the Arch Linux version too. At the initial boot I set up the keyboard, local time and other settings, the configuration menu is very simple. The Windows manager is lightweight and powerful, I love it. I am having an overall good impression.

Labels: ,

Sunday 2 December 2012

Abstract classes in Ruby

Ruby is not a traditional OOP language like Java and C++. An abstract class provides and interface for a programmer, the implementation of some generic methods (useful for subclasses) and abstract methods (to be implemented by subclasses). Usually with Ruby you use mixins, but sometimes you need to provide a class with some generic methods and some stub methods to be implemented by more specialised subclasses; the template pattern just does this. In a Ruby class you just can do this:

def create_html_header
  raise "You cannot call this abstract method"
end

This will block a programmer to use a method that should be implemented in a subclass, a class specialised in HTML generation. If you have many methods like that one, you could use a module to reduce the amount of methods with a raise statement. One solution could be to declare the abstract methods in this way:

class AClass
   abstract_methods :foo, :bar
end

A solution to block a user to instantiate a class is:

module Abstract
  def self.append_features(klass)
    # access an object's copy of its class's methods & such
    metaclass = lambda { |obj| class << obj; self ; end }

    metaclass[klass].instance_eval do
      old_new = instance_method(:new)
      undef_method :new

      define_method(:inherited) do |subklass|
        metaclass[subklass].instance_eval do
          define_method(:new, old_new)
        end
      end
    end
  end
end

class A
  include Abstract
end
class B < A
end

B.new #=> #
A.new # raises #

So if you just include 'include Abstract' you will prevent that class to be instantiated. For more information read this StackOverflow question: http://stackoverflow.com/questions/512466/how-to-implement-an-abstract-class-in-ruby. The template pattern is explained here: http://designpatternsinruby.com/

Saturday 1 December 2012

PHP and Ruby contractor looking for new projects

I have been working with PHP for many years, around eight and now I have more than 3 years of Ruby experience. If you want to build a web application, or you need a Linux system administrator, send me an email at rtacconi [at] virtuelogic.net.

My business web site is virtuelogic.net

Write code that you need now

Predictions are hard, especially about the future.
Yogi Berra

This aforism tells us that we should write the code for what we need now. If we write a web site with internationalization support we are betting that we will need a web site to support more than one language, but if we want to develop a site only in English we are wasting time. If is possible to add features later. This does not means that we should write software with tightly-coupled objects and with an inflexible design, but we should find a trade-off between pragmatism and flexibility.

If you are interested in this topic, read pace 14 of "Design Patterns in Ruby" by Russ Olsen.