Ralph,
Some time ago I came up with a syntax for method metadata as well.
Assuming a class definition like this:
class Account
def Account.open(first, last)
end
def close
end
def add(amount)
end
def remove(amount)
end
def balance
end
def transfer(amount, account)
end
end
And assuming you wanted to make that accessible for statically typed
systems you would just do this:
require ‘extern’
class Account
extern :Account.open, :return[Account], :first[String], :last[String]
def Account.open(first, last)
end
extern :close
def close
end
extern :add, :amount[Float]
def add(amount)
end
extern :remove, :amount[Float]
def remove(amount)
end
extern :balance, :return[Float]
def balance
end
extern :transfer, :amount[Float], :account[Account]
def transfer(amount, account)
end
end
So, for each method you want to externalize you just include the
‘extern’ call.
Usage: extern , [ [<:return[Classname]>],
<:param[Classname]>, … ]
To reflect on this metadata you do:
Account.each_externalized_method do | method |
puts method.to_s
end
Which outputs:
Account Account.open(String first, String last)
NilClass close()
NilClass add(Float amount)
NilClass remove(Float amount)
Float balance()
NilClass transfer(Float amount, Account account)
You can, of course, inspect the method (ExternalMethodDefinition
instance) instead of printing it out. So this creates a runtime
structure to store Class information about methods returns or
parameters, which again is useful is you want to bridge Ruby with
statically typed languages like Java, .NET, etc.
Of course, since Ruby’s classes are open, you can define this extern
metadata on existing classes:
class ThreadGroup
extern :ThreadGroup.new, :return[ThreadGroup]
extern :add, :return[ThreadGroup], :thread[Thread]
extern :list, :return[Array]
end
I have the code to make this syntax work…which actually adds behavior
to the symbol class (I know that’s a bit odd, but it creates a cool
look…I think). Let me know if you want it.
···
On Jan 19, 2004, at 11:05 PM, Ralph Mason wrote:
I am playing with dynamicaly creating proxy ruby objects, but need
more data than can be provided by a class or function name.
And so am wondering if anyone has a way to do this?
eg - something like
class MyClass
Metadata.new({“returns”=>“int”,“params”=>“int,int”)
def my_function(x)
end
end
Somewhere later I could look at metadata by class and function to get
the hash of information about these things
eg
Metadata.each_class{| data |
data.each_function{ | funct_data |
Here I would have information about the function
and the paramers passrd to the metadata
}
}
Thanks
Ralph
//