[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Associating argument descriptions with method

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
--
(-, /\ \/ / /\/



1 Answer

Robert Klemme

10/22/2004 2:10:00 PM

0


"Gavin Kistner" <gavin@refinery.com> schrieb im Newsbeitrag
news:08B3F283-2430-11D9-A186-000A959CF5AC@refinery.com...
> 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:

>> def log(o) print o.id, ":", o.inspect, "\n" end
=> nil
>> log String.instance_method( :length )
134619252:#<UnboundMethod: String#length>
=> nil
>> log String.instance_method( :length )
135146692:#<UnboundMethod: String#length>
=> nil
>> log String.instance_method( :length )
135088144:#<UnboundMethod: String#length>
=> nil
>> log String.instance_method( :length )
135066784:#<UnboundMethod: String#length>
=> nil
>> log String.instance_method( :length )
135039148:#<UnboundMethod: String#length>
=> nil
>> log String.instance_method( :length )
135027784:#<UnboundMethod: String#length>
=> nil

Kind regards

robert