itsme213 wrote:
Not built into Ruby, true. But an app might have its own meta-data, possibly
domain-specific. Perhaps things like param types, or data about how
parameters are interpreted by a method (e.g. "either a list of dates, or a
start date and a list of durations "), etc. A domain-specific Ruby could add
Class#methods to declare such things.
This naturally makes me think of:
def read_dat_file(filename):
"""Read Code Composer data file
File format: ASCII, optional header line followed by data, one word per line
filename -- name of dat file to read
Returns list of data values.
"""
f = open(filename, 'r')
lines = f.readlines()
f.close()
...
AKA Python's docstring inside a function.
I think it would be *amazing* if a docstring like that were available in Ruby, and if it were available via reflection. It could be used in all kinds of ways: from within IRB sessions, as part of error messages, as a "service discovery" kind of tool...
I also really like the idea of some kind of MethodData or MethodDescriptor objects that can describe themselves, list their keyword args, list the default values, etc.
And while we're at it, I don't see a good reason why every arg shouldn't automatically be a keyword arg. Sure, sometimes you really don't need one, like "sqrt(foo)", it's pretty obvious that "foo" must be the number you want to find the square root of, but would it hurt to be able to say sqrt(num: foo)? The only downside I can see, from a user's point of view, is that not all methods may expose sensible names, so the keywords may not make sense. Maybe the person implementing sqrt() was having a bad day and the variable is actually named "headache". sqrt(headache: foo) might be confusing. But if everybody knew their variables would get exposed, it might encourage them to use sensible names for things, which would help everybody in the long run.
I hack *in* Ruby, not *on* Ruby, so maybe there are good technical reasons why this is difficult or not worth doing, but from a user's point of view, I think it would be great.
Ben