Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Ruby
changing an instance variable
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Tommy Morgan, post: 4615487"] [Note: parts of this message were removed to make it a legal post.] 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. 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: [/QUOTE] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Ruby
changing an instance variable
Top