Access variables of a class in another file

Hi,

I'm new to Ruby. I want to access variables defined in a class of A.rb
file from another ruby file B.rb, how can this be done.

Appreciate your response.

Thanks,
#M

···

--
Posted via http://www.ruby-forum.com/.

Thanks, I'm doing the same but it doesn't the value of the variable.
I've defined the variable in the initialize method of the class, is this
correct?

···

--
Posted via http://www.ruby-forum.com/.

The following is the content of file A.rb and B.rb

A.rb

class GoogleHomePage

attr_reader :shaik

def initialize(browser)
  puts "in INITIALIZE"
  shaik = "Ruby"
  @browser = browser
end

end

···

===========================================================

B.rb

require "rubygems"
require "watir"
require 'rspec'
require File.dirname(__FILE__) + '/../pages/B'; #requiring the file B.rb

$googlehomepageobject = GoogleHomePage.new(@browser)
$googlehomepageobject.waitforgooglesearchbutton

@sh = $googlehomepageobject.shaik
puts "-----------------------------"
puts @sh # this line prints blank space rather printing the value
'Ruby'
puts "-----------------------------"
end

--
Posted via http://www.ruby-forum.com/.

You need to require the file, as shown

http://csnipp.com/s/13

···

On Wed, Oct 23, 2013 at 12:16 PM, shaik s. <lists@ruby-forum.com> wrote:

Hi,

I'm new to Ruby. I want to access variables defined in a class of A.rb
file from another ruby file B.rb, how can this be done.

Appreciate your response.

Thanks,
#M

--
Posted via http://www.ruby-forum.com/\.

--
Harisankar P S
https://twitter.com/coder_hsps | http://tech.hsps.in

Most of the time you need to write as accurate as possible.

In your example you used:

  $googlehomepageobject = GoogleHomePage.new(@browser)

But in that file, the variable @browser did not exist, so
basically you passed nil here.

  $googlehomepageobject = GoogleHomePage.new(nil)

You should always check if the variables are properly
initialized.

Ruby is elegant but you need to make sure that the small
parts you use in it are as accurate as possible. It will
help a lot in the long run, especially when you write
larger scripts which unavoidably will have more
complexity.

···

--
Posted via http://www.ruby-forum.com/.

Can you show what you are trying to do?
If you are defining a class in a file with an instance variable (@x),
you need to create an object of that class, and it will contain that
variable "inside", so to speak. The instance variable is part of an
object.

If you define the variable as a class instance variable, you will need
to provide accessors to see the variable from the outside.

Can you explain what you are trying to achieve, and what code do you have?

Jesus.

···

On Wed, Oct 23, 2013 at 9:24 AM, shaik s. <lists@ruby-forum.com> wrote:

Thanks, I'm doing the same but it doesn't the value of the variable.
I've defined the variable in the initialize method of the class, is this
correct?

The following is the content of file A.rb and B.rb

A.rb

class GoogleHomePage

attr_reader :shaik

def initialize(browser)
  puts "in INITIALIZE"
  shaik = "Ruby"

@shaik = "Ruby"

This makes @shaik an instance variable, instead of a local variable.

  @browser = browser
end

end

===========================================================

B.rb

require "rubygems"
require "watir"
require 'rspec'
require File.dirname(__FILE__) + '/../pages/B'; #requiring the file B.rb

I suppose you mean /pages/A

$googlehomepageobject = GoogleHomePage.new(@browser)
$googlehomepageobject.waitforgooglesearchbutton

Do you need this variable to be global? (variables that start with $
are global).
I also guess @browser was initialized somewhere else, but as this
seems to be a toplevel script I'm not sure you need an instance
variable here.

@sh = $googlehomepageobject.shaik

same here about using @sh, you don't need an instance variable.

puts "-----------------------------"
puts @sh # this line prints blank space rather printing the value
'Ruby'
puts "-----------------------------"
end

This "end" makes me think this is part of a bigger structure, maybe a
class, so maybe you actually need the instance variables. Probably not
the globals, though.

Jesus.

···

On Wed, Oct 23, 2013 at 9:54 AM, shaik s. <lists@ruby-forum.com> wrote:

"Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> wrote in post
#1125311:

···

On Wed, Oct 23, 2013 at 9:54 AM, shaik s. <lists@ruby-forum.com> wrote:

  shaik = "Ruby"

@shaik = "Ruby"

This makes @shaik an instance variable, instead of a local variable.

require "watir"
require 'rspec'
require File.dirname(__FILE__) + '/../pages/B'; #requiring the file B.rb

I suppose you mean /pages/A

$googlehomepageobject = GoogleHomePage.new(@browser)
$googlehomepageobject.waitforgooglesearchbutton

Do you need this variable to be global? (variables that start with $
are global).
I also guess @browser was initialized somewhere else, but as this
seems to be a toplevel script I'm not sure you need an instance
variable here.

@sh = $googlehomepageobject.shaik

same here about using @sh, you don't need an instance variable.

puts "-----------------------------"
puts @sh # this line prints blank space rather printing the value
'Ruby'
puts "-----------------------------"
end

This "end" makes me think this is part of a bigger structure, maybe a
class, so maybe you actually need the instance variables. Probably not
the globals, though.

Jesus.

Thanks Dear, you were a great help, your suggestions worked, I'm able to
access/print the value of the A.rb file variable in B.rb file.

Again, thanks a lot.

#M

--
Posted via http://www.ruby-forum.com/\.