0

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

5
  • When you say "the instance variable information is not available", what do you mean? You're passing it's value as the argument variable. Commented Apr 16, 2020 at 5:42
  • 1
    Why can't B#do_something return a value instead? Or A#pass_to_b could pass itself to B#do_something and do_something could call methods on its argument to send information back to the A instance. Commented Apr 16, 2020 at 5:59
  • What you are trying to build is basically a circular dependency which is considered an antipattern and should be avoided. The correct way would be that do_something just returns a result and that A assigns the result to the instance variable on itself. Commented Apr 16, 2020 at 6:31
  • 2
    If you remove (self) from def pass_to_b, your code works just fine: calling a.pass_to_b sets the instance variable @variable to "hello world". Commented Apr 16, 2020 at 6:57
  • Thanks guys for the comments. I realized that one of my instance variables had a typo and so I was having an issue. Much appreciated! Commented Apr 16, 2020 at 13:50

1 Answer 1

1

My MRI throws exeption about

def pass_to_b(self)

because you can't pass self to method as argument.

You need delete 'self' how argument

Run code below and you will see that @variable of instance of Class A has '123hello world' string

class A
  def initialize
    @variable = "123"

    @module_b = B.new(self)
  end

  def pass_to_b
    @module_b.do_something(@variable)
  end

  def set_variable(var)
    # var = empty
    @variable = var
  end
end

# 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)
    set_variable_in_a(variable + "hello world")
  end
end

a = A.new
a.pass_to_b

display variable 'a' and you will see something like this

#<A:0x00007fdaba0f3c90 @variable="123hello world", @module_b=#<B:0x00007fdaba0f3c40 @module_a=#<A:0x00007fdaba0f3c90 ...>>> 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I realized I had a typo in the way I was defining my instance variable. This helped out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.