Post by Peter Cooper on May 25th, 2006
Initializing instance variables from the parameter list

- Applying conditions to attribute setters
- Dynamically adding methods to classes through their objects
- PX: A quick and silly Ruby obfuscation system


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: ruby

Click here to add on del.icio.us








