what does @ stand for in a Ruby function name

The method names for the four unary operators +, -, ~, and ! are +@, -@, ~@, and !@. So the funny looking method definitions:

def +@; _show _test :_pass, :_fail end
def -@; _show _test :_fail, :_pass end
def ~@; _show _pend;               end
def !@; _show _desc;               end

just define overloads for those four unary operators. Then TestRocket is patched into the Proc class using Proc.send :include, TestRocket.

This:

-> { Die.new(2) }

is simply a lambda definition and another way of writing lambda { Die.new(2) }. Then, with TestRocket patched into Proc we can say this:

+-> { Die.new(2) }
# + lambda { Die.new(2) }

and it will run this method:

def +@; _show _test :_pass, :_fail end

as an instance method on that lambda.

Looks like a bit of an abuse of the unary operator overloading to “invent” something that looks like new -->, ~->, … operators.

Leave a Comment