So I have two files, one called a.rb and one called b.rb. Here's the contents in both:
# a.rb class A def initialize @variable = "" @module_b = B.new(self) end def pass_to_b(self) @module_b.do_something(@variable) end def set_variable(var) # var = empty @variable = var end end and
# b.rb class B def initialize(module_a) @module_a = module_a end def set_variable_in_a(data) @module_a.set_variable(data) end def do_something(variable) # variable = empty set_variable_in_a("hello world") end end This is just an example of what I'm dealing with. If I'm trying to start a function in Class A, which is supposed to do something in ClassB and then change an instance variable in Class A, I'm not sure how to do this properly. This is what I've tried, however:
a = A.new a.pass_to_b Class B cannot see the instance variable @variable, and if it tries to set_variable_in_a, that doesn't work either. It's like the do_something function in Class A successfully calls the do_something function in Class B, but the instance variable information is not available. I thought by passing self to Class B, we'd be able to at least call the function
B#do_somethingreturn a value instead? OrA#pass_to_bcould pass itself toB#do_somethinganddo_somethingcould call methods on its argument to send information back to theAinstance.do_somethingjust returns a result and that A assigns the result to the instance variable on itself.(self)fromdef pass_to_b, your code works just fine: callinga.pass_to_bsets the instance variable@variableto"hello world".