Attr_constructor

I’ve been writing a couple of very struct-like classes, and got annoyed
at having to write:

class MyStructLikeThing
attr_accessor :one, :two, :three, :four, :five
def initialize (one, two, three, four, five)
@one, @two, @three, @four, @five = one, two, three, four, five
end
end

And how ugly that looked in the code, so I decided to try my hand at
whipping up an attr_constructor statement.

class Module
private
def attr_constructor(*args)
self.class_eval %Q{
def initialize(#{args.join(’, ')})
#{args.collect{ |setting|
“instance_variable_set(”@#{setting}", #{setting})" }}
end
}
end
end

class MyStructLikeThing
attr_accessor :one, :two, :three, :four, :five
attr_constructor :one, :two, :three, :four, :five
end

It strikes me as a little cludgy. Is there a better way of doing this?
Is there a way to get the args variable so that it can be read from
within a module_eval block?

(Note also, that I know about the Struct module, this is just a
simplified example)

Daniel Sheppard
http://jroller.net/page/soxbox

···

#####################################################################################
This email has been scanned by MailMarshal, an email content filter.
#####################################################################################

i like this:

class Foo

OPTIONS = :foo, :bar, :fobar
OPTIONS.each{|opt| attr opt, true}

def initialize arg0, arg1, opts
  @arg0, @arg1 = arg0, arg1
  opts.each{|opt, value| send "#{ opt }=", value}
end

end

f = Foo.new 42, 42.0

f = Foo.new 42, 42.0, :foo => ‘forty’

f = Foo.new 42, 42.0, :foo => ‘forty’,
:bar => ‘two’

f = Foo.new 42, 42.0, :foo => ‘forty’,
:bar => ‘two’,
:foorbar => ‘fortytwo’

or

class Foo

INIT_ARGS = :foo, :bar, :fobar
INIT_ARGS.each{|arg| attr arg, true}

def initialize(*args)
  INIT_ARGS.each{|arg| self.send "#{ arg }=", args.shift}
end

end

it’s easy to extend this or make validators

-a

···

On Tue, 30 Mar 2004, Daniel Sheppard wrote:

I’ve been writing a couple of very struct-like classes, and got annoyed
at having to write:

class MyStructLikeThing
attr_accessor :one, :two, :three, :four, :five
def initialize (one, two, three, four, five)
@one, @two, @three, @four, @five = one, two, three, four, five
end
end

And how ugly that looked in the code, so I decided to try my hand at
whipping up an attr_constructor statement.

class Module
private
def attr_constructor(*args)
self.class_eval %Q{
def initialize(#{args.join(', ')})
#{args.collect{ |setting|
“instance_variable_set("@#{setting}", #{setting})” }}
end
}
end
end

class MyStructLikeThing
attr_accessor :one, :two, :three, :four, :five
attr_constructor :one, :two, :three, :four, :five
end

It strikes me as a little cludgy. Is there a better way of doing this?
Is there a way to get the args variable so that it can be read from
within a module_eval block?

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
URL :: Solar-Terrestrial Physics Data | NCEI
TRY :: for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done
===============================================================================

“Daniel Sheppard” daniels@pronto.com.au schrieb im Newsbeitrag
news:EE0A2C5DE8D7CA4792891604D4FED52E016749EB@mlbmail.pronto.com.au…

I’ve been writing a couple of very struct-like classes, and got annoyed
at having to write:

class MyStructLikeThing
attr_accessor :one, :two, :three, :four, :five
def initialize (one, two, three, four, five)
@one, @two, @three, @four, @five = one, two, three, four, five
end
end

And how ugly that looked in the code, so I decided to try my hand at
whipping up an attr_constructor statement.

class Module
private
def attr_constructor(*args)
self.class_eval %Q{
def initialize(#{args.join(', ')})
#{args.collect{ |setting|
“instance_variable_set("@#{setting}", #{setting})” }}
end
}
end
end

class MyStructLikeThing
attr_accessor :one, :two, :three, :four, :five
attr_constructor :one, :two, :three, :four, :five
end

It strikes me as a little cludgy. Is there a better way of doing this?

Well, you could at least save the attr_accessor line for those fields
contained in the constructor:

class Module
private
def attr_constructor(*args)
self.class_eval %Q{
def initialize(#{args.join(', ')})
#{args.collect{ |setting| “@#{setting} = #{setting}\n” }}
end
}

args.each do |sym|
  attr_accessor sym
end

end
end

class MyStructLikeThing
attr_constructor :one, :two, :three, :four, :five
end

mst = MyStructLikeThing.new 1,2,3,4,5

Is there a way to get the args variable so that it can be read from
within a module_eval block?

(Note also, that I know about the Struct module, this is just a
simplified example)

Why don’t you use Struct then? You can add arbitrary methods after
creating the struct like this:

MyStruct = Struct.new( :one, :two, :three, :four, :five )

class MyStruct
def test
p “test”
end
end

Regards

robert