Changing an instance variable

if I have these methods in a class:

def initialize(x, y)
  @x, @y = x, y
end

def foo
  @z = @x + @y
end

def bar
  foo
  @z * 2
end

What if I want to redefine @x inside method "foo" but only in there?

is there a simple way to do this?

···

--
Posted via http://www.ruby-forum.com/.

Jason Lillywhite wrote:

What if I want to redefine @x inside method "foo" but only in there?

Why?

is there a simple way to do this?

There is no way to do that. You could change @x and then just change it back
at the end of the method, but that will still affect the value of @x for any
other method you call during the method.

HTH,
Sebastian

···

--
Jabber: sepp2k@jabber.org
ICQ: 205544826

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

??? - Please note that my algorithms are much larger so efficiency is my
friend in this case. Thank you!

···

--
Posted via http://www.ruby-forum.com/\.

Maybe you could try using a default parameter?
Like so:
def foo(x = @x)
  @z = x + @y
end

Example from irb:

irb(main):001:0> @x = 1
=> 1
irb(main):002:0> @y = 2
=> 2
irb(main):003:0> def foo(x = @x)
irb(main):004:1> @z = x + @y
irb(main):005:1> end
=> nil
irb(main):006:0> foo
=> 3
irb(main):007:0> foo(2)
=> 4
irb(main):008:0> @x
=> 1

You could then re-write your bar method as such:

def bar(new_x)
  foo(new_x)
  @z * 2
end

Also, if you've got really long complicated methods, that's usually a good
sign that you should refactor your code out a bit.

Hope that helps,

--Tommy M.

···

On Wed, Aug 13, 2008 at 4:05 PM, Jason Lillywhite < jason.lillywhite@gmail.com> 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

??? - Please note that my algorithms are much larger so efficiency is my
friend in this case. Thank you!
--
Posted via http://www.ruby-forum.com/\.

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

···

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

exactly what I needed. Thank you for helping me in my feeble quest to do
just what you suggest: refactor my code a bit.

···

--
Posted via http://www.ruby-forum.com/.