tanay tanay - 4 months ago 6
Ruby Question

2 different classes A and B . variable is declared in class A .Get value of variable in B

class A
@i = 2
end

class B
puts @i
end


these are two classes in different files a.rb and b.rb. I want to print @i in class B. can anyone help me with that

Answer

b.rb:

require_relative 'a'

class B
  puts A.instance_variable_get(:@i)
end

Execute the file and you will get the output:

ruby b.rb
#=> 2
Comments