On Thu, 2002-04-11 at 12:06, Ian Macdonald wrote:
> 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.

Ah, got it.

> 
> > 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.

Hmm, odd.  Everything in Ruby is a class... so a variable is a class,
too, right?  Shouldn't there be a method for the variable class to
retrieve its value without eval()'ing it?  I don't play much with
referenced variables.  ^,^

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

I would hope there's a better way.  I'm not a Ruby expert, tho. (I use
it mostly for smaller scripts and utilities, not big applications).

> 
> 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 
>                             | 
>                             |