I’ve pretty new to ruby and still getting to know how ruby works. I want
to associate some metadata to a function. I saw a similar post down this
line but didn’t get the real answer.
In javascript I’m used to associating metadata with a function as
follows.
foo.bar = “foobar”;
function foo(){
}
Whats the normal ruby practice to do this kind of a thing. Whats the
ruby way of doing this.
The example is kind of artificial. Can you disclose more detail on
what you are trying to achieve? There are millions of ways to answer
your question and meta data for methods might not even be the best
solution.
We are trying to define a way of writing web services in ruby (Code
First approach). And we expect to keep it as simple as possible. In
order to do this we need to define some metadata that we can use to
generate a pleasant WSDL. This metadata may include things like
documentation and details about input and output Types. Note that ruby
is a Dynamic language whereas xml schema is not. And although a
parameter taken into a function could be of anyType in most cases it
operates on it assuming its of a certain type. And getting this
information into the WSDL matters in that case.
Assume that I have a function as follows,
def foo(bar, foobar)
end
I need to get the information on the types expected by this function
across to the WSDL. I basically need a way to say that bar is a String
and foobar is an integer. This is where the matadata that I’m talking
about comes into play.
It will be nice if the Ruby community can help us figure out a nice
rubish way of doing this. The most important thing is that I want this
all to fit in with ruby people so this should look rubish as much as
possible.
Once we get this done we have more cool stuff in mind…
The example is kind of artificial. Can you disclose more detail on
what you are trying to achieve? There are millions of ways to answer
your question and meta data for methods might not even be the best
solution.
information into the WSDL matters in that case.
about comes into play.
It will be nice if the Ruby community can help us figure out a nice
rubish way of doing this. The most important thing is that I want this
all to fit in with ruby people so this should look rubish as much as
possible.
Well, then it sounds as if you need structured data for a method.
Extending my last proposed solution a bit you could do this:
Of course you can use a Hash instead of the OpenStruct - but that’s
probably rather a matter of taste. If you need automatic discovery of
meta data set I’d probably rather use a Hash - if you know which meta
data you expect I’d probably use an OpenStruct or a Struct.
Cheers
robert
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.