Associating argument descriptions with method

G

Gavin Kistner

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
 
R

Robert Klemme

Gavin Kistner said:
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.

The reason is most likely, that you always get new instances of
UnboundMethod:
135027784:#<UnboundMethod: String#length>
=> nil

Kind regards

robert
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top