def initialize(arg1, arg2, arg3, arg4) @arg1, @arg2, @arg3, @arg4 = arg1, arg2, arg3, arg4
end
end
Wouldn't it be nice if we (ok, I) could instead write
def initialize(arg1, arg2, arg3, arg4)
initargs
end
and have initargs create the attr_accessor as well as the assignments.
I know I can do this in 1.9.2
class X
def initialize(arg1)
self.class.send(:attr_accessor, :arg1) @arg1 = arg1
end
end
So is it possible in Ruby to introspect on the _names_ of arguments passed in to a function so that I can build the attr_accessor as well as the assignments?
It is much simpler and IMO makes much more sense not to figure out
variable names while already having variables, but to figure out their
contents while having names. Like this:
class Class
def attr_accessor_init *meths
attr_accessor *meths
define_method :initialize do |*args|
meths.each_with_index do |m, i|
self.send :"#{m}=", args[i]
end
end
end
end
irb(main):013:0> class A
irb(main):014:1> attr_accessor_init :a, :b, :c
irb(main):015:1> end
Monday, February 20, 2012, 10:19:41 AM, you wrote:
What I want to do is not change the interface but the implementation. That is, I want to get a list of the names of the arguments by introspection.
It is much simpler and IMO makes much more sense not to figure out
variable names while already having variables, but to figure out their
contents while having names. Like this:
class Class
def attr_accessor_init *meths
attr_accessor *meths
define_method :initialize do |*args|
meths.each_with_index do |m, i|
self.send :"#{m}=", args[i]
end
end
end
end
irb(main):013:0>> class A
irb(main):014:1>> attr_accessor_init :a, :b, :c
irb(main):015:1>> end