Gavin Kistner
10/22/2004 1:41:00 PM
SUMMARY
Why does the code at the end return nil for .arguments?
DETAILS
I'm trying to come up with a system of describing arguments for certain
public methods, so that my GUI can provide reasonable input widgets and
constraints. My first idea was to associate this information with each
method, so that I could discover the public methods and then have each
method know how to describe itself. However, the following attempt at
this is leaving me with an empty array.
I'm happy to go another way with this (store the descriptions in an
instance variable of the class, rather than on each method) but was
wondering why this doesn't do what I expected.
class UnboundMethod
attr_accessor :arguments
end
class Method
class Argument
attr_accessor :name, :type, :default_value, :required, :min, :max,
:values
def initialize( name, type, default_value=nil, required=true,
min=nil, max=nil, *values )
@name = name
@type = type
@default_value = default_value
@required = required
@values = values
end
end
end
module MethodDescriptions
@@describe_method = lambda { |id, name, help, return_type, *arguments|
self.instance_method( id ).arguments = arguments
}
def self.extend_object( obj )
obj.send( :define_method, :describe_method, @@describe_method )
end
end
class Foo
self.extend( MethodDescriptions )
def calculate_area( length, width )
length * width
end
nil && describe_method( :calculate_area, 'Calculate Area',
'Calculates the area of a rectangle',
Method::Argument.new( :return, :float ),
Method::Argument.new( :length, :float, 0, true, 0 ),
Method::Argument.new( :width, :float, 0, true, 0 )
)
end
m = Foo.instance_method( :calculate_area )
p m, m.arguments
#=> #<UnboundMethod: Foo#calculate_area>
#=> nil
--
(-, /\ \/ / /\/