From: Trans Date: 2006-09-16T11:16:32+09:00 Subject: Re: Ruby keywords vs methods Chris Roos wrote: > Hi, > > I attended Jim Weirich's talk at Rails Conf on Friday. It was about > thoughtful library design and contained a section on ways to mitigate > risk when re-opening standard libraries. > > Rake uses the following pattern when adding to standard library classes... > > class BaseClass > unless instance_methods.include? "foo" > def foo > # meth body here > end > end > end > > It struck me that it might be quite useful to wrap this up and apply > it to all methods defined for [a] given class[es]. Unfortunately, it > appears not to be (completely) possible. I thought we might be able > to use the method_added hook, but this gets fired after the method > definition. We need something to fire right before the method > definition. As def is a keyword and not a method, we can't override > and add to the default behaviour. Note, that we can, however, do this > with define_method. Well, the Extensions project does this. And another way to avoid it is to use a module instead of adding the method directly, a la ActiveSupport. But, I can't say I see the point exactly. Maybe I missing a usecase. But first of all it would only really matter if you're bringing in a whole mess of methods all at once and thus are not sure what you are getting, otherwise you would have avoided the clash yourself. Plus it shouldn't matter al all if you require the extensions FIRST. After all, it's presumed you're going to use the extensions but if you need to use one of the already used method names you're code can just overwrite the extension definition --rather than the other way around. Also, what if you actaully want it to override the method? > So, my actual question is, why are some things keywords and most > things method calls? I think it's io (language) that doesn't have any > keywords at all (so I guess it must be possible). In addition, I'm > interested in whether there are there any plans to increase or > decrease the number of keywords in ruby? Seems like there's rather few as it is, but in anycase it's unlikely to increase. Of course that's up to Matz. T.