tanay tanay - 1 year ago 61
Ruby Question

2 different classes A and B . variable is declared in class A .I want to get value of variable in B

class A
@i = 2

class B
puts @i

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 Source


require_relative 'a'

class B
  puts A.instance_variable_get(:@i)

Execute the file and you will get the output:

ruby b.rb
#=> 2
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download