Default constructor

P

Pablo Lorenzzoni

Hello!

class Klass
attr_accessor :att1, :att2
def initialize(att1, att2)
@att1 = att1
@att2 = att2
end
....
end

Is it just me or the construction above is rather common? Is there such a
thing as a default cosntructor? Something that could turn that into

class Klass
attr_accessor :att1, att2
initializer(att1,att2)
end

or even

class Klass
attr_accessor :att1, :att2
initializer
end

Since we have short cuts as attr_accessor, attr_writer and attr_reader,
shouldn't we have a constructor short cut?

Anyway... just a crazy thought that just hit me now. What do you think about
it?

[]s

Pablo
 
P

Phil Tomson

Hello!

class Klass
attr_accessor :att1, :att2
def initialize(att1, att2)
@att1 = att1
@att2 = att2
end
....
end

Is it just me or the construction above is rather common? Is there such a
thing as a default cosntructor? Something that could turn that into

class Klass
attr_accessor :att1, att2
initializer(att1,att2)
end

or even

class Klass
attr_accessor :att1, :att2
initializer
end

Since we have short cuts as attr_accessor, attr_writer and attr_reader,
shouldn't we have a constructor short cut?

This is an idea that's been floating around:

class Klass
attr_accessor :att1, :att2
def initialize(@att1,@att2)
end
end

....but alas, Matz doesn't like it :-( says it's ugly.
I think it looks quite nice actually. Clean, concise, no duplications.

Phil
 
R

Ryan Pavlik

Hello!

class Klass
attr_accessor :att1, :att2
def initialize(att1, att2)
@att1 = att1
@att2 = att2
end
....
end

Is it just me or the construction above is rather common? Is there such a
thing as a default cosntructor? Something that could turn that into
<snip>

Yes it is. It's pretty easy to add, too, if you think Lisp. This is a
little ugly... macros would make this WAY nicer... but here we go:

def definit(*args, &block)
s = ""
args.each { |a| s << "@#{a.to_s} = #{a}\n" }
arglist = args.join(', ')

module_eval <<-END
class << self; attr_reader :_post_init; end
@_post_init = block
def initialize(#{arglist})
#{s}
instance_eval &self.class._post_init if self.class._post_init
end
END
end #m:definit

And of course, it wouldn't be complete without an example:

class A
attr_accessor :a, :b

definit:)a, :b) {
@a += 1
}

def show; p @a, @b; end
end #c:A

a = A.new(1, 2)
a.show

This is why macros are cool. Code that writes code is so extremely
useful. ;-)

(Note: I'm using 1.8 CVS, this may not work in 1.6.)
 
M

Mauricio Fernández

Hello!

class Klass
attr_accessor :att1, :att2
def initialize(att1, att2)
@att1 = att1
@att2 = att2
end
....
end

Is it just me or the construction above is rather common? Is there such a
thing as a default cosntructor? Something that could turn that into

class Klass
attr_accessor :att1, att2
initializer(att1,att2)
end

or even

class Klass
attr_accessor :att1, :att2
initializer
end

This is similar to but not exactly what you want (see below)
=> 1

however
(irb):9: warning: instance variable @att1 not initialized
(irb):9: warning: instance variable @att2 not initialized
and
=> nil

so you need to use accessors even inside methods.

--
_ _
| |__ __ _| |_ ___ _ __ ___ __ _ _ __
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Steal my cash, car and TV - but leave the computer!
-- Soenke Lange <[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top