Archive for the ‘Ruby’ Category

Transparent caching in Ruby

Tuesday, September 19th, 2006

Found the following code in the ruby-talk archives. It allows to cache what a method returns.

class Class
    alias_method :__new__, :new

    def cached_method(*methods)
        @cached_methods ||= []
        @cached_methods += methods
        @cached_methods.uniq!
    end

    def new(*args, &block)
        obj = __new__(*args, &block)
        klass = obj.class
        @cached_methods ||= []

        @cached_methods.each do |name|
            meth = obj.method(name)
            i = 1

            while klass.instance_variables.member?("@_defc_#{i}")
                i += 1
            end

            wrapped_name = "_defc_#{i}"
            cache = klass.class_eval("@#{wrapped_name} = {}")

            wrapper = Proc.new do |*args_a|
                arg_hash = args_a.hash

                if cache.has_key?(arg_hash)
                    cache[arg_hash]
                else
                    cache[arg_hash] = meth.call(*args_a)
                end
            end

            klass.send(:alias_method, wrapped_name, name)
            klass.send(:define_method, name, wrapper)
        end

        return obj
    end
end

if __FILE__ == $0
    class Klass
        def foo; end
        def bar; end
        cached_method :foo, :bar
    end
end

New tools for the DICT protocol

Monday, June 26th, 2006

So you are interested in the DICT protocol and would like to know where to find some interesting tools and data to use with your favourite DICT client (e.g. Fantasdic…)? I have made a bunch of tools that you should have a look at!

(more…)

Encoding detection in Ruby

Thursday, May 11th, 2006

I’ve just discovered Universal Encoding Detector. You give it a string and it returns its encoding and the confidence in the result! It is very useful. From what I’ve tested, it works very well.

To install it:

gem install chardet

Example :


require 'rubygems'
require 'UniversalDetector'
require 'net/http'
Net::HTTP.version_1_2
Net::HTTP.start( 'yahoo.co.jp' ) {|http|
data = http.get("/").body
p UniversalDetector::chardet(data)
#=> {"encoding"=>"EUC-JP", "confidence"=>0.99}
}

Fantasdic now in the GNOME CVS

Thursday, March 9th, 2006

After a few weeks of evaluation, Fantasdic has finally been accepted by the GNOME accounts team and is now in the GNOME CVS.

To retrieve the source code from the anonymous mirror repository:

cvs -d:pserver:anonymous@anoncvs.gnome.org:/cvs/gnome login
cvs -z3 -d:pserver:anonymous@anoncvs.gnome.org:/cvs/gnome co -P fantasdic

I hope it will help Fantasdic attract more contributors, translators and above all package maintainers. As far as I know, this is the first Ruby program to be hosted on the GNOME CVS. Maybe a first step toward more adoption of that fabulous language in the GNOME community?

Fantasdic 1.0-beta1.1 (fix for Windows)

Sunday, February 19th, 2006

This release fixes a bug that prevented Fantasdic from running under Windows (thanks to Gabriele Renzi for telling me) and some user interface tweaks (thanks to John Spray for the patch).

Screenshot : Fantasdic under Windows

Download : fantasdic-1.0-beta1.1.tar.gz

Fantasdic 1.0-beta1

Saturday, February 18th, 2006

I’m pleased to announce the first release of Fantasdic, a client for the DICT protocol (a dictionary network protocol, RFC 2229).

(more…)