Want to stay on top? Ruby Weekly is a once-weekly e-mail newsletter covering the latest Ruby and Rails news.
     Feed Icon

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 = []
end

But, how about doing it this way?

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

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