Try to search your question here, if you can't find : Ask Any Question Now ?

How to pass a instance/accessor variable in ruby to another class and get data stored in respective variable?

HomeCategory: stackoverflowHow to pass a instance/accessor variable in ruby to another class and get data stored in respective variable?
Avatarpushpa asked 5 months ago

Referring to the below code is there a way that I can pass the variable row from class A to class B#kick and get the data stored?

class A
  attr_accessor :row

  def fetch
    B.new.kick(self.row)
    puts row.inspect
  end
end

class B
  def kick(x)
    x = [3,4]
  end
end

@test = A.new.fetch
expect(@test.row).to eql([3,4])

Current O/P => nil

However If I pass self and assign that works , but I dont want to use this approach:
Working Code

class A
  attr_accessor :row

  def fetch
    B.new.kick(self)
    puts row.inspect
  end
end

class B
  def kick(x)
    x.row = [3,4]
  end
end

@test = A.new.fetch
#=> [3, 4]
1 Answers
Best Answer
AvatarArben answered 5 months ago
Your Answer

17 + 3 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com