On Thu 11 Apr 2002 at 22:07:03 +0900, you wrote:

> On Thu, 2002-04-11 at 01:40, Ian Macdonald wrote:
> > 
> > So what is a better way to do this?
> > 
> >     def each
> >       self.instance_variables.each { |attr| yield(eval attr) }
> >     end
> 
> Sorry if it should be obvious, but what is the purpose of eval on
> attr? 

Without the eval, the yield method will pass the variable's name to
the calling block, rather than its value.

For example, if self.foo == 1 and self.bar == 2, then the eval will
pass 1 on the first iteration over attr and 2 on the second
iteration. Without the eval, we'll pass the literal references @foo
and @bar without dereferencing them.

> Also, that isn't in any way calling the parser, is it?  (or is attr a
> string being sent to the parser?)

Yes, attr is essentially just a string containing the name of an
instance variable here. I needed a method that would provide the
values of each of an object's instance variables, but all I could find
was instance_variables, which provides their names, not their values.

I'm pretty new to Ruby, so if there's a better way to do this, please
let me know.

Ian
-- 
Ian Macdonald               | I can mend the break of day, heal a broken 
ian / caliban.org             | heart, and provide temporary relief to
                            | nymphomaniacs.   -- Larry Lee 
                            | 
                            |