Default constructor

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

···


Pablo Lorenzzoni (Spectra) spectra@debian.org
GnuPG: 0x268A084D at pgp.mit.edu/keyring.debian.org
This message is protected by DoubleROT13 encryption
Attempting to decode it violates the DMCA/WIPO acts

Hi –

···

On Sat, 19 Jul 2003, Pablo Lorenzzoni wrote:

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

There was a rejected RCR along these lines (see
http://www.rubygarden.org/article.php?sid=52) – I’m not sure
whether it was the specific syntax or the underlying idea that was
ultimately being rejected, but you’ll also find a lot of discussion of
this from about two years ago in ruby-talk.

David


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

In article 200307182302.26069.spectra@debian.org,

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 :frowning: says it’s ugly.
I think it looks quite nice actually. Clean, concise, no duplications.

Phil

···

Pablo Lorenzzoni spectra@debian.org wrote:

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. :wink:

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

···

On Sat, 19 Jul 2003 11:03:01 +0900 Pablo Lorenzzoni spectra@debian.org wrote:

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


Ryan Pavlik rpav@users.sf.net

“Elfland? Mo’ like pansyland.” - 8BT

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)

Klass = Struct.new(:att1, :att2)
=> Klass
a = Klass.new “foo”, :bar
=> #<Klass att1=“foo”, att2=:bar>
a.att1
=> “foo”
a.att2
=> :bar
a.att1 = 1
=> 1
a.att1
=> 1

however

class Klass
def foo
puts “#{@att1} and #{@att2}”
end
end
=> nil
a.foo
(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.

···

On Sat, Jul 19, 2003 at 11:03:01AM +0900, Pablo Lorenzzoni wrote:


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
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 soenke@escher.north.de