How-To

Colorize on Github

Aqui les dejo una pequeña y muy simple gema, Colorize,que nos permite colorear en la terminal de linux, osx o *nix como podemos observar en el siguiente ejemplo.

Test.rb Code:

  1. require "Colorize"
  2.  
  3. class Klass
  4.   include Colors
  5.   def initialize
  6.     system("echo #{red("hola")} This is a #{blue("test")}")
  7.     system("echo #{red on_white("Hola1")}")
  8.     puts "#{red on_blue("Hola2")}"
  9.   end
  10. end
  11.  
  12. Klass.new
  13.  
  14. html = "<html>
  15. <body>
  16. <dl>
  17.  <dt>the first term</dt>
  18.  <dd>its definition</dd>
  19.  
  20.  <dt>the second term</dt>
  21.  <dd>its definition</dd>
  22.  
  23.  <dt>the third term</dt>
  24.  <dd>\"its definition\"</dd>
  25.  <img src=\"test.html\">
  26. </dl>
  27. </body>
  28. </html>"
  29.      
  30. c = Colorize.new( :red => /<.*html>|<.*body>/,
  31.                   :blue => /".*"/,
  32.                   :cyan => /src/)
  33. puts c.paint(html)

Por ultimo, se aprecian mucho comentarios, ideas y/o preguntas sobre cosas que les gustaría ver en Colorize, etc ...

Remover items del Menubar en Leopard

Ayer al abrir la Macbook Pro me apareció, por algún motivo que desconozco, un nuevo icono en el menubar. Quise eliminarlo de las maneras que conocía y no pude. A pesar de que solo era un iconito, ocupaba un lugar y me molestaba. Decidi buscar en Google una solucion y encontré una muy practica que solo involucra una combinación de teclas y un movimiento con el mouse.

La acción es la siguiente:
Mantenemos presionada la tecla "command ()" mientras arrastramos el icono fuera del menubar.

Bueno, queria compartirlo con ustedes por si se cruzan con el mismo problema.

RGTE - Ruby Email filter

According to the developer, RGTE is a small, opinionated email filter which processes and filters incoming email into Maildirs. The current version is 0.0.3. This project looks like it has a lot of potential, but it is still to young to be sure.
If you want to try it you will need to install it with gem but providing a source:

  1. $ [sudo] gem install --source http://rubyi.st/gems/ rgte

The developers reminds us not to forget that we need Tmail
and we need to trail slashes, otherwise it won't work.

Try it and tell me what you think.
Project's site : http://rubyi.st/rgte/

Peach = Thread + each

Parallel Each (for ruby with threads)

It is pretty common to have iterations over Arrays that can be safely run in parallel. With multicore chips becoming pretty common, single threaded processing is about as cool as Pog. Unfortunately, standard Ruby hates real threads pretty hardcore at the present time; however, for some ruby projects alternate VMs like JRuby do give multicores some lovin'. Peach exists to make this power simple to use with minimal code changes.

Functions like map, each, and delete_if are often used in a functional, side-effect free style. If the operation in the block is computationally intense, performance can often be gained by multithreading the process. That's where Peach comes in. In the simplest case, you are one letter away from harnessing the power of parallelism and unlocking the secret of a guilt-free tan. At this stage, the goggles are purely optional.
Using Peach

Suppose you are going about your day job hacking away at code for the WOPR when you stumble upon the code:

  1. cities.each {|city| thermonuclear_war(city)}

Clearly, the only winning move is to declare war in parallel. With Peach, the new code is:
  1. require 'peach'
  2.  
  3. cities.peach {|city| thermonuclear_war(city)}

Requiring peach.rb monkey patches Array into submission. Currently Peach provides peach, pmap, and pdelete_if. Each of these functions takes an optional argument n, which represents the desired number of worker threads with the default being one thread per Array element. For cheaper operations on a large number of elements, you probably want to set n to something reasonably low.

  1. (0...10000).to_a.pmap(4) {|x| process(x)}

Constructing the threads and adding on a few layers of indirection does add a bit of overhead to the iteration especially on MRI. Keep this in mind and remember to benchmark when unsure.
Syntax (without all the words)

  1. require 'peach'
  2.  
  3. [1,2,3,4].peach{|x| f(x)} #Spawns 4 threads, => [1,2,3,4]
  4. [1,2,3,4].pmap{|x| f(x)} #Spawns 4 threads, => [f(1),f(2),f(3),f(4)]
  5. [1,2,3,4].pdelete_if{|x| x > 2} #Spawns 4 threads, => [3,4]
  6.  
  7. [1,2,3,4].peach(2){|x| f(x)} #Spawns 2 threads, => [1,2,3,4]
  8. [1,2,3,4].pmap(2){|x| f(x)} #Spawns 2 threads, => [f(1),f(2),f(3),f(4)]
  9. [1,2,3,4].pdelete_if(2){|x| x > 2} #Spawns 2 threads, => [3,4]

Extracted from the Developers's web site: http://peach.rubyforge.org/

UPDATE: Gracias!, Irlandes Borracho por la correccion en el titulo.

Syndicate content