Robert Klemme
8/13/2008 8:53:00 PM
On 13.08.2008 22:05, Jason Lillywhite wrote:
> Sebastian Hungerecker wrote:
>> Jason Lillywhite wrote:
>>> What if I want to redefine @x inside method "foo" but only in there?
>> Why?
>
> The reason is because I have big, long algorithms inside these methods
> 'foo' and 'bar' and I don't want to re-write "@z = @x + @y" when I get
> to method 'bar'. I need to evaluate the function inside 'bar' with all
> the same variables except for @x. that one variable needs to be
> different when I run bar. Maybe I have to do this:
>
> def initialize(x, y)
> @x, @y = x, y
> end
>
> def foo
> @z = @x + @y
> end
>
> def bar(new_x)
> @z = new_x + @y
> @z * 2
> end
That is the most reasonable solution if I understand you correctly.
Basically you do not want to use the instance variable but an arbitrary
other value. Note that you can even call that parameter "x". It won't
interfere with "@x".
Kind regards
robert