Forum: Ruby Access variables of a class in another file

462c96e85144024df16705e088c0d638?d=identicon&s=25 shaik s. (shaik_s)
on 2013-10-23 08:46
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
820e01609a9f8f0fc0d5167680625b93?d=identicon&s=25 Harisankar P S (coderhs)
on 2013-10-23 09:15
(Received via mailing list)
You need to require the file, as shown

http://csnipp.com/s/13
462c96e85144024df16705e088c0d638?d=identicon&s=25 shaik s. (shaik_s)
on 2013-10-23 09:24
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?
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 "Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> (Guest)
on 2013-10-23 09:43
(Received via mailing list)
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?

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.
462c96e85144024df16705e088c0d638?d=identicon&s=25 shaik s. (shaik_s)
on 2013-10-23 09:54
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
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 "Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> (Guest)
on 2013-10-23 10:40
(Received via mailing list)
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.
462c96e85144024df16705e088c0d638?d=identicon&s=25 shaik s. (shaik_s)
on 2013-10-23 11:53
"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
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2013-10-23 14:50
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.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.