From: Jos Backus Date: 2007-01-20T02:17:40+09:00 Subject: Re: Scoping and locating definitions Hi Eric, On Fri, Jan 19, 2007 at 05:03:16PM +0900, Eric Hodel wrote: > On Jan 18, 2007, at 15:53, Jos Backus wrote: > > >Consider the following: > > > >lizzy:~% cat r > >require 'lib' > ># from 'lib' import 'Foo' > > > >f = Foo.new("string") > >p f.bar > > > >p 'abc'.rot13 > >lizzy:~% cat lib.rb > >class String > > def rot13 > > tr 'A-Za-z', 'N-ZA-Mn-za-m' > > end > >end > > > >class Foo > > def initialize(s) > > @s = s > > end > > > > def bar > > @s.rot13 > > end > >end > >lizzy:~% ruby r > >"fgevat" > >"nop" > >lizzy:~% > > > >What if I wanted String#rot13 to _not_ be visible in file > >`r' (because I > >didn't ask for it)? How do I contain the change to String to > >lib.rb only? > > Currently you can't. Right. I think it's important that the language supports this though. `require' can pollute the caller's code in uncontrollable ways, and it would be very useful to have control over this behavior. > >Or perhaps to Foo? > > Define Foo#rot13 > > >On a related note: how does one go about finding _where_ > >String#rot13 was > >defined? One possible way would be for .methods to return an Array of > >BoundMethods instead of Strings and have file and linenumber > >information be > >part of BoundMethod (if possible). > > > >Thoughts? > > grep? Heh. The interpreter knows where it saw the most recent method definition; why not make this information available? Python, with its closed classes, doesn't have this "issue" because method definitions only live in one place per class. A colleague at work considers the fact that it's hard to find out where methods are defined a weakness, and I'd like to present him with a better answer than `grep'. It sounds like some more introspection could solve this "issue". Thanks, -- Jos Backus jos at catnook.com