[#4567] Re: What's the biggest Ruby development? — Aleksi Niemel<aleksi.niemela@...>

Dave said:

18 messages 2000/08/23
[#4568] Q's on Marshal — Robert Feldt <feldt@...> 2000/08/23

[#4580] RubyUnit testcase run for different init params? — Robert Feldt <feldt@...> 2000/08/25

[#4584] Re: RubyUnit testcase run for different init params? — Dave Thomas <Dave@...> 2000/08/25

Robert Feldt <feldt@ce.chalmers.se> writes:

[#4623] Re: RubyUnit testcase run for different init params? — Robert Feldt <feldt@...> 2000/08/28

On Sat, 26 Aug 2000, Dave Thomas wrote:

[#4652] Andy and Dave's European Tour 2000 — Dave Thomas <Dave@...>

24 messages 2000/08/30
[#4653] Re: Andy and Dave's European Tour 2000 — matz@... (Yukihiro Matsumoto) 2000/08/30

Hi,

[#4657] Ruby tutorials for newbie — Kevin Liang <kevin@...> 2000/08/30

Hi,

[ruby-talk:4631] Re: Class methods

From: Dave Thomas <Dave@...>
Date: 2000-08-29 04:26:51 UTC
List: ruby-talk #4631
Mark Slagell <ms@iastate.edu> writes:

> Point well taken - though, at the risk of beating a dead wildebeast here,
> what I'd begun to wonder about after posting the first message was a
> consistent default-semantic for instance method names when invoked as class
> methods: "if no class method by that name exists, operate on a new instance
> constructed with the given argument(s), if any" -- isn't it true that where
> we do have duplicate class methods, this is what we generally expect to
> happen anyway? Hmm.

How about this solution, written in Ruby, that seems to do what you
want?

     class Class
       def method_missing(meth, *params)
         if block_given?
           self.new(*params).send(meth) { |*p| yield *p }
         else
           self.new(*params).send(meth) 
         end
       end
     end

This is called using things like:

     File.each("t.rb") { |line| puts line }


By putting the handler in class Class, the technique automatically
applies to every class in the system. And, by writing it in Ruby, you
can make it a module that you require into your code as you
need. Finally, it doesn't add any additional methods to the core.

The downside is that you'll get weird errors if you call a real
unknown method, so we should probably put some better error handling
in here. That's left as an exercise...


Regards


Dave


In This Thread