From: "trans. (T. Onoma)" Date: 2004-12-03T08:20:34+09:00 Subject: Re: nested defs, what if... On Thursday 02 December 2004 12:22 pm, Brian Schræ—¦der wrote: | But where is this usefull? It seems only complicated and inefficent to me | (Doesn't it create a new instance method on each call?): | | class A | def a() | def b() | self | end | self | end | end | ==>nil | A.new.b | NoMethodError: undefined method `b' for # | from (irb):2 | A.new.a | ==># | A.new.a.b | ==># | A.new.b | ==># It is interesting. Does this mean that an _object_ could dynamically change the state of all objects of its class? I have to test.... class T def a def b puts "Hello" end end end => nil t = T.new => # t2 = T.new => # t.b => NoMethodError: undefined method `b' for # t2.b => NoMethodError: undefined method `b' for # t.a => nil t.b => nil Hello t2.b => nil Hello Yep. It sure does. This is very strange indeed. I wonder if you could write extentions in this way. class String def use(x) case x when :chars def chars self.split(//) end when :tab # ... end end end Of course, you have to instantiate a string first. Well, it's an idea, but I don't think a very good one. In general I don't think this is useful, and would rather obfuscate code if actually used. I think what would be more useful is if such methods were local methods, like local variables: class A def a def b(x) x + 1 end 10.times{ |i| print b(i) } end end o = A.new o.b => NoMethodError A.a => 12345678910 o.b => NoMethodError This would allow for embedded subroutines --much more useful. T.