From: Paul Brannan Date: 2002-09-13T04:40:24+09:00 Subject: Re: General ?s about a couple of Ruby features On Fri, Sep 13, 2002 at 02:34:33AM +0900, Matt Gushee wrote: > Hi, Folks-- > > I am preparing a presentation on Ruby for a local Linux Users' Group. > Describing the language is easy enough, but being fairly new to it, > there are certain things I can't really provide a good rationale for > based on personal experience--and I will almost certainly be asked to do > so, since many in the audience are experienced Perl and Python > programmers. So I'd like to hear what people think about the following: > > * The ability to modify existing class and module definitions is > obviously interesting, but what is it *useful* for? I can see > that this feature allows you to change the behavior of all instances > of a class; what, if any, other benefits are there? Does anyone have > some real-world application scenarios where this feature is useful? Let's say we have a class A and a class B, and we want to know why we want to modify class A. Modifying class A is useful when: a) there is a bug in class A and we want to fix the bug without shutting down the application. b) class A is missing a method that a method in class B needs to have defined in order to accept an object of class A as a parameter. c) for creating delegate objects (objects that intercept method calls and pass them onto another object) without using method_missing. There are probably some other cases as well. > * As above, for adding instance-specific methods. (b) applies here, too. This is also useful for attaching arbitrary data to exceptions and other objects as they are passed through the system. > * Regarding iterators: it is clearly often more elegant to use an > iterator rather than a for or while loop. Does the existence of > iterators provide any benefit to end users (e.g. performance)? The for keyword is implemented by calling each(), so in order for it to work, an each() method must be defined. The advantage of iterators is that you separate the iterating code from what is done to each element. It doesn't matter whether you are iterating over an array or a range or a binary tree; the syntax is exactly the same. Paul