In Ruby I want to do almost the following and print it "changed":
Whatever def change_foo @ foo = "changed" end end = fu = "original" o = whatever. New O. Current_Foo puts "this has changed" if @fu == "replace", "it did not change" if @fu == "original"
The problem is , whatever
, @foo
inside whatever
.
What is the "global" FU to create change_foo
? Another way to ask this is, "What reference can I refer to?" "Can be the owner of" @foo
"?"
I should not have to solve such as "use global variable" or "use a class variable" or "pass @foo"
to change_foo
". I am specifically asking about the scenario above, where I have no control over the original variable nor the way it is called change_foo
.
I have come up with an alternative solution where I have a reference to the global object at the time of construction, but I am not mad about it because for this I want to wait for anything in the proper area.
class whatever def initialize (main) @ main = main end def change_foo @ main.instance_variable_set ("@foo", "change") end up o = whatever. New (self)
It's late for the party, but you can refer to the current reference By this method you can pass, and then remove the operations in the example in that particular context:
class def defaults Fu (reference) eval% q (@foo = "Changed"), reference end end = foo = "original" o = whatever. () O.changeFoo (binding) puts "this changed" if @foo == "replace", "it could not be changed" if @fu == "original snout" # = & gt; This
Comments
Post a Comment