A trap of delegation

April 6th 2011

Beware of proxy objects. Here is an edge case I fell into and want to warn you.

Let’s consider an ultimate proxy class:

It delegates virtually everything to a given object. And one would think (yeahl, including me) that we can use this proxy object everywhere instead of the genuine object.

Counterexample

That’s not true and here is the trap:

A genuine object will pass this check:

But proxy object will not:

Why?

Because case internally uses #=== to determine if a given object belongs to a certain “when-block”. Look:

It’s easier to track down in Rubinius:

Object::kind_of_p at vm/builtin/object.cpp:428
Module::case_compare at vm/builtin/module.cpp:66

Module#=== at kernel/common/module.rb:572

You see? Not a single chance to have an influence on this process from a Ruby level.

git-sh and git-wtf together

December 13th 2010

git-sh is awesome. As well as git-wtf. I bet most people are not aware of git-wtf, but they certainly should, cause it is an extremely convenient and useful tool when you are working with several branches and/or remotes. A little example of git-wtf output:

Local branch: heads/master [x] in sync with remote Remote branch: origin/master (git@github.com:account/project.git) [x] in sync with local Feature branches: [x] ticket_827 is merged in [ ] ticket_831 is NOT merged in (1 commit ahead; 18 commits behind) - [dependencies] Depend on library X, version Y [bfda321]

(see http://github.com/michaelklishin/git-wtf)

The problem

If you are using git-sh, you have to type

branch!repo> command git-wtf 

which is long and annoying.

The solution

This post shows how to call git-wtf  just by typing

branch!repo> wtf 

in git-sh console.

Clone the fork of git-wtf, which contains a pretty Makefile (I hope it will be merged into main repo soon):

$ git clone http://github.com/DanielVartanov/git-wtf 

Run make to configure correspondent aliases for git and git-sh:

$ make git config --global alias.wtf '!git-wtf' echo -e "\n# git-wtf\ngitalias wtf='git wtf'" >>

Beyond the Ruby metaprogramming

December 14th 2010

Here is a collection of very cool Ruby libs which extend Ruby’s metaprogramming and object model operation very much:

object2module — Convert Classes and Objects to Modules so they can be extended/included

remix — A library to give you total control over class and module ancestor chains

prepend — Prepends modules in front of a class; so method lookup starts with the module

local_eval — Object#instance__eval without changing self

include_complete — Use Module#include__complete to bring in singleton classes from modules

mixico — Disable and re-enable mixins