How the Ruby Language Can Help Captain Morrimento

Systems Development

Hi, everyone. I previously wrote a comparison of Ruby and Pascal, and now I have received a request to help Captain Morrimento select his recruits using software.

Captain Morrimento

The problem is that Captain Morrimento had to go on leave from his battalion, but still wanted to make sure his team stayed in line and keep an eye on the recruits so they would not cause trouble later.

To make matters worse, I discovered that Captain Morrimento is very short-tempered and does not want to waste time with a complex interface. He knows how to use basic office software (just a text editor). So he wants something that comes as close as possible to copying the recruits’ records from his PC and pasting them into a field that checks and diagnoses the recruit.

Knowing that we needed something simple, I decided to use Ruby’s DSL (Domain-Specific Language) concepts and wrote the following class so that he could enter data in the format he uses every day:

    def aspirante(nome, &bloco)
    	 Aspirante.new(nome, &bloco)
    end
    
    
    class Aspirante
    
    def initialize(nome, &bloco)
   	@nome = nome
    	@dados = {
    		:descricoes => [],
   		:numero => nil
    		}

    		instance_eval(&bloco)

    end

    def descricao(dados)
    	@dados[:descricoes] << dados
    end
    
    def numero(numero)
    	@dados[:numero] = numero
    end

    def resultado()
    
    	if @dados[:descricoes].include? "corrupto" then 5.times{ puts "PEDE PRA SAIR!"}
    
    	elsif @dados[:numero] == 2 then 5.times{ puts "PEDE PRA SAIR! SEU 02! Tu e CORRUPTO!"}

   	elsif @dados[:descricoes].include? "PM" then 2.times{ puts "NAO VAI SUBIR NINGUEM!"}

   	elsif @dados[:descricoes].include? "dorminhoco" then 2.times{ puts "TRAGAM A GRANADA!..."}

    	else puts "O senhor e um FANFARRAO seu #{@dados[:numero]}.\n Sera interrogado diretamente pelo CAPITAO."

    	end
    end

    end

Now all he has to do is save this code in a file and load it (load class_aspirante.rb). Then the Captain can use it like this:

    a1 = aspirante "José dos Azóis" do
      numero = 04
      descricao = "Disciplinado"
      descricao = "Competente"
      descricao = "etc"
    end

From there, he can simply call a1.resultado and get the answer he wanted.

This example is still a work in progress, and I’d welcome suggestions for improving it because, as I said, Captain Morrimento is hot-headed and I don’t want to be called a “Fanfarrão” (loudmouth). ;)

Anyone who can contribute new implementation tips to make the DSL language Caveira more intuitive and efficient is welcome to comment.

See you later.