The Passionate Craftsman

Ruby, PHP, MySql, Software engineering and more.

Monday 11 March 2013

How to list unmerged files during a GIT conflict

Add this to your list of aliases:

alias gunmerged='git diff --name-only --diff-filter=U'

gunmerged will show you the unmerged files.

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.

Friday 16 November 2012

Show hostname in MySql with a query

First way:

mysql> show variables like 'hostname';
+---------------+------------------------+
| Variable_name | Value                  |
+---------------+------------------------+
| hostname      | domU-12-31-39-16-D6-7C |
+---------------+------------------------+
1 row in set (0.00 sec)

Second way:


mysql> SELECT @@hostname;
+-----------------------------+
| @@hostname                  |
+-----------------------------+
| Riccardos-MacBook-Pro.local |
+-----------------------------+
1 row in set (0.00 sec)

There are probably more ways to show the hostname, but two are more than enough for me.

Wednesday 18 July 2012

Install zemomq and Mngrel2 on Debian Squeeze


# as root
apt-get install uuid-dev
apt-get install build-essential
wget http://download.zeromq.org/zeromq-2.2.0.tar.gz
tar -xzvf zeromq-2.2.0.tar.gz
cd zeromq-2.2.0/
./configure
make
sudo make install
ldconfig

apt-get install sqlite3 libsqlite3-dev

apt-get install sudo
wget http://mongrel2.org/static/downloads/mongrel2-1.7.5.tar.bz2
tar -xjvf mongrel2-1.7.5.tar.bz2
cd mongrel2-1.7.5/
make clean all && sudo make install

adduser mongrel
cd ..
mv mongrel2-1.7.5/ /home/mongrel/
cd /home/mongrel
chown -R mongrel mongrel2-1.7.5/
cd mongrel2-1.7.5/

# log out and login as mongrel user
cd mongrel2-1.7.5/
cp examples/configs/sample.conf mysite.conf
m2sh load -config mysite.conf
ls config.sqlite
mkdir run logs tmp
m2sh start -host localhost

# access this url to test it http://your_hostname:6767/tests/sample.html

curl http://your_hostname:6767/tests/sample.html
hi there