Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

Initializing instance variables from the parameter list

By Peter Cooper / May 25, 2006

While looking at NegaPosi, a crazy Ruby implementation of a micro language that only uses unary operators, I discovered a cute way to initialize instance variables. Usually you'd do this:

def initialize
@p = []
@b = []

But, how about doing it this way?

def initialize a=@p=[], b=@b=[]

Initialize doesn't require the parameters, but it forces @p and @b to be initialized as arrays whenever an instance is created. Cute trick, although the readability suffers.

Technorati Tags:

Other Posts to Enjoy

Twitter Mentions