How to access variable from outside class in another file
up vote
2
down vote
favorite
My file structure is as follows:
Main/
Games/
roulette.rb
casino.rb
wallet.rb
player.rb
I have a wallet class that holds a money value in the class like so.
class Wallet
attr_accessor :money
def initialize
@money = 0
end
end
I then have a player class that inherits from the Wallet class
class Player < Wallet
attr_accessor :name
def initialize
super()
@name = nil
get_user_info
end
I then have a Casino class that inherits from Player like so
class Casino < Player
def initialize
binding.pry
puts @money, @name
end
I have also used require_relative
to pull in both files thinking that would give me access to their global variables @money, @name
.
If I am inside the roulette.rb
file here is my code I wrote just to see if it would have a value.
require_relative '../wallet.rb'
class Roulette
def initialize
puts @wallet
end
end
How would I go about getting access to these variables in the casino class? Thanks for the help.
ruby
add a comment |
up vote
2
down vote
favorite
My file structure is as follows:
Main/
Games/
roulette.rb
casino.rb
wallet.rb
player.rb
I have a wallet class that holds a money value in the class like so.
class Wallet
attr_accessor :money
def initialize
@money = 0
end
end
I then have a player class that inherits from the Wallet class
class Player < Wallet
attr_accessor :name
def initialize
super()
@name = nil
get_user_info
end
I then have a Casino class that inherits from Player like so
class Casino < Player
def initialize
binding.pry
puts @money, @name
end
I have also used require_relative
to pull in both files thinking that would give me access to their global variables @money, @name
.
If I am inside the roulette.rb
file here is my code I wrote just to see if it would have a value.
require_relative '../wallet.rb'
class Roulette
def initialize
puts @wallet
end
end
How would I go about getting access to these variables in the casino class? Thanks for the help.
ruby
1
Ok sorry, edited the original
– Daniel Bailey
Nov 11 at 16:45
1
Aside from your actual question, this seems like a major misuse of inheritance. If classA
inherits from classB
, then it should make sense to say "A is a type of B". In this case, is a Player a type of Wallet? No. Is a Casino a type of Player? No. I'd advise rethinking your class architecture.
– Tom Lord
Nov 11 at 17:19
Yea so wallet inherits from player, and casino inherits from player, so I just called super from casino to instantiate each class. Which creates the wallet.
– Daniel Bailey
Nov 11 at 17:20
1
@DanielBailey "which creates the wallet" - again, not quite. "casino = Casino.new". Thiscasino
is a casino and a player and a wallet, all three in the same single object. I agree with Tom Lord, this is a misuse of inheritance. There's no chance this is needed in your app
– Sergio Tulentsev
Nov 11 at 17:33
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
My file structure is as follows:
Main/
Games/
roulette.rb
casino.rb
wallet.rb
player.rb
I have a wallet class that holds a money value in the class like so.
class Wallet
attr_accessor :money
def initialize
@money = 0
end
end
I then have a player class that inherits from the Wallet class
class Player < Wallet
attr_accessor :name
def initialize
super()
@name = nil
get_user_info
end
I then have a Casino class that inherits from Player like so
class Casino < Player
def initialize
binding.pry
puts @money, @name
end
I have also used require_relative
to pull in both files thinking that would give me access to their global variables @money, @name
.
If I am inside the roulette.rb
file here is my code I wrote just to see if it would have a value.
require_relative '../wallet.rb'
class Roulette
def initialize
puts @wallet
end
end
How would I go about getting access to these variables in the casino class? Thanks for the help.
ruby
My file structure is as follows:
Main/
Games/
roulette.rb
casino.rb
wallet.rb
player.rb
I have a wallet class that holds a money value in the class like so.
class Wallet
attr_accessor :money
def initialize
@money = 0
end
end
I then have a player class that inherits from the Wallet class
class Player < Wallet
attr_accessor :name
def initialize
super()
@name = nil
get_user_info
end
I then have a Casino class that inherits from Player like so
class Casino < Player
def initialize
binding.pry
puts @money, @name
end
I have also used require_relative
to pull in both files thinking that would give me access to their global variables @money, @name
.
If I am inside the roulette.rb
file here is my code I wrote just to see if it would have a value.
require_relative '../wallet.rb'
class Roulette
def initialize
puts @wallet
end
end
How would I go about getting access to these variables in the casino class? Thanks for the help.
ruby
ruby
edited Nov 11 at 16:45
asked Nov 11 at 16:29
Daniel Bailey
6710
6710
1
Ok sorry, edited the original
– Daniel Bailey
Nov 11 at 16:45
1
Aside from your actual question, this seems like a major misuse of inheritance. If classA
inherits from classB
, then it should make sense to say "A is a type of B". In this case, is a Player a type of Wallet? No. Is a Casino a type of Player? No. I'd advise rethinking your class architecture.
– Tom Lord
Nov 11 at 17:19
Yea so wallet inherits from player, and casino inherits from player, so I just called super from casino to instantiate each class. Which creates the wallet.
– Daniel Bailey
Nov 11 at 17:20
1
@DanielBailey "which creates the wallet" - again, not quite. "casino = Casino.new". Thiscasino
is a casino and a player and a wallet, all three in the same single object. I agree with Tom Lord, this is a misuse of inheritance. There's no chance this is needed in your app
– Sergio Tulentsev
Nov 11 at 17:33
add a comment |
1
Ok sorry, edited the original
– Daniel Bailey
Nov 11 at 16:45
1
Aside from your actual question, this seems like a major misuse of inheritance. If classA
inherits from classB
, then it should make sense to say "A is a type of B". In this case, is a Player a type of Wallet? No. Is a Casino a type of Player? No. I'd advise rethinking your class architecture.
– Tom Lord
Nov 11 at 17:19
Yea so wallet inherits from player, and casino inherits from player, so I just called super from casino to instantiate each class. Which creates the wallet.
– Daniel Bailey
Nov 11 at 17:20
1
@DanielBailey "which creates the wallet" - again, not quite. "casino = Casino.new". Thiscasino
is a casino and a player and a wallet, all three in the same single object. I agree with Tom Lord, this is a misuse of inheritance. There's no chance this is needed in your app
– Sergio Tulentsev
Nov 11 at 17:33
1
1
Ok sorry, edited the original
– Daniel Bailey
Nov 11 at 16:45
Ok sorry, edited the original
– Daniel Bailey
Nov 11 at 16:45
1
1
Aside from your actual question, this seems like a major misuse of inheritance. If class
A
inherits from class B
, then it should make sense to say "A is a type of B". In this case, is a Player a type of Wallet? No. Is a Casino a type of Player? No. I'd advise rethinking your class architecture.– Tom Lord
Nov 11 at 17:19
Aside from your actual question, this seems like a major misuse of inheritance. If class
A
inherits from class B
, then it should make sense to say "A is a type of B". In this case, is a Player a type of Wallet? No. Is a Casino a type of Player? No. I'd advise rethinking your class architecture.– Tom Lord
Nov 11 at 17:19
Yea so wallet inherits from player, and casino inherits from player, so I just called super from casino to instantiate each class. Which creates the wallet.
– Daniel Bailey
Nov 11 at 17:20
Yea so wallet inherits from player, and casino inherits from player, so I just called super from casino to instantiate each class. Which creates the wallet.
– Daniel Bailey
Nov 11 at 17:20
1
1
@DanielBailey "which creates the wallet" - again, not quite. "casino = Casino.new". This
casino
is a casino and a player and a wallet, all three in the same single object. I agree with Tom Lord, this is a misuse of inheritance. There's no chance this is needed in your app– Sergio Tulentsev
Nov 11 at 17:33
@DanielBailey "which creates the wallet" - again, not quite. "casino = Casino.new". This
casino
is a casino and a player and a wallet, all three in the same single object. I agree with Tom Lord, this is a misuse of inheritance. There's no chance this is needed in your app– Sergio Tulentsev
Nov 11 at 17:33
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Those are not global variables. They are called "instance variables" and to access them you need to create instances of your casinos and players. Looks like this.
player = Player.new
player.money # => 0
player.money += 10
player.money # => 10
In your Casino class you don't call parent initializers (a simple oversight, I think), so it doesn't initialize @name
and @money
.
And Roulette doesn't do anything at all to obtain a wallet. So it stays at default value nil.
yea JS lingo I can't drop ha. Ok so I am creating an instance of the wallet inside of the player, and creating an instance of the casino inside of the player. How would I get access to those instance variables inside of casino?
– Daniel Bailey
Nov 11 at 16:50
@DanielBailey: "I am creating an instance of the wallet inside of the player" - no you don't. In your hierarchy a casino IS a player, which is ALSO a wallet. Doesn't make any sense, right? :)
– Sergio Tulentsev
Nov 11 at 16:52
yea I'm so lost. Only a week in learning ruby, so struggling with how each class inherits what. Guess I need to do more reading
– Daniel Bailey
Nov 11 at 16:53
1
@DanielBailey: "How would I get access to those instance variables inside of casino" - call super in casino's initializer, this should set name/money.
– Sergio Tulentsev
Nov 11 at 16:53
1
@DanielBailey: yeah, JS object model does that to people :) Look up a book called "practical object-oriented design in ruby" (AKA POODR). It's excellent. Might be overwhelming at first (at your skill level), but on a second or third read you'll get a level-up, guaranteed :)
– Sergio Tulentsev
Nov 11 at 16:54
|
show 3 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Those are not global variables. They are called "instance variables" and to access them you need to create instances of your casinos and players. Looks like this.
player = Player.new
player.money # => 0
player.money += 10
player.money # => 10
In your Casino class you don't call parent initializers (a simple oversight, I think), so it doesn't initialize @name
and @money
.
And Roulette doesn't do anything at all to obtain a wallet. So it stays at default value nil.
yea JS lingo I can't drop ha. Ok so I am creating an instance of the wallet inside of the player, and creating an instance of the casino inside of the player. How would I get access to those instance variables inside of casino?
– Daniel Bailey
Nov 11 at 16:50
@DanielBailey: "I am creating an instance of the wallet inside of the player" - no you don't. In your hierarchy a casino IS a player, which is ALSO a wallet. Doesn't make any sense, right? :)
– Sergio Tulentsev
Nov 11 at 16:52
yea I'm so lost. Only a week in learning ruby, so struggling with how each class inherits what. Guess I need to do more reading
– Daniel Bailey
Nov 11 at 16:53
1
@DanielBailey: "How would I get access to those instance variables inside of casino" - call super in casino's initializer, this should set name/money.
– Sergio Tulentsev
Nov 11 at 16:53
1
@DanielBailey: yeah, JS object model does that to people :) Look up a book called "practical object-oriented design in ruby" (AKA POODR). It's excellent. Might be overwhelming at first (at your skill level), but on a second or third read you'll get a level-up, guaranteed :)
– Sergio Tulentsev
Nov 11 at 16:54
|
show 3 more comments
up vote
1
down vote
accepted
Those are not global variables. They are called "instance variables" and to access them you need to create instances of your casinos and players. Looks like this.
player = Player.new
player.money # => 0
player.money += 10
player.money # => 10
In your Casino class you don't call parent initializers (a simple oversight, I think), so it doesn't initialize @name
and @money
.
And Roulette doesn't do anything at all to obtain a wallet. So it stays at default value nil.
yea JS lingo I can't drop ha. Ok so I am creating an instance of the wallet inside of the player, and creating an instance of the casino inside of the player. How would I get access to those instance variables inside of casino?
– Daniel Bailey
Nov 11 at 16:50
@DanielBailey: "I am creating an instance of the wallet inside of the player" - no you don't. In your hierarchy a casino IS a player, which is ALSO a wallet. Doesn't make any sense, right? :)
– Sergio Tulentsev
Nov 11 at 16:52
yea I'm so lost. Only a week in learning ruby, so struggling with how each class inherits what. Guess I need to do more reading
– Daniel Bailey
Nov 11 at 16:53
1
@DanielBailey: "How would I get access to those instance variables inside of casino" - call super in casino's initializer, this should set name/money.
– Sergio Tulentsev
Nov 11 at 16:53
1
@DanielBailey: yeah, JS object model does that to people :) Look up a book called "practical object-oriented design in ruby" (AKA POODR). It's excellent. Might be overwhelming at first (at your skill level), but on a second or third read you'll get a level-up, guaranteed :)
– Sergio Tulentsev
Nov 11 at 16:54
|
show 3 more comments
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Those are not global variables. They are called "instance variables" and to access them you need to create instances of your casinos and players. Looks like this.
player = Player.new
player.money # => 0
player.money += 10
player.money # => 10
In your Casino class you don't call parent initializers (a simple oversight, I think), so it doesn't initialize @name
and @money
.
And Roulette doesn't do anything at all to obtain a wallet. So it stays at default value nil.
Those are not global variables. They are called "instance variables" and to access them you need to create instances of your casinos and players. Looks like this.
player = Player.new
player.money # => 0
player.money += 10
player.money # => 10
In your Casino class you don't call parent initializers (a simple oversight, I think), so it doesn't initialize @name
and @money
.
And Roulette doesn't do anything at all to obtain a wallet. So it stays at default value nil.
answered Nov 11 at 16:48
Sergio Tulentsev
178k29288304
178k29288304
yea JS lingo I can't drop ha. Ok so I am creating an instance of the wallet inside of the player, and creating an instance of the casino inside of the player. How would I get access to those instance variables inside of casino?
– Daniel Bailey
Nov 11 at 16:50
@DanielBailey: "I am creating an instance of the wallet inside of the player" - no you don't. In your hierarchy a casino IS a player, which is ALSO a wallet. Doesn't make any sense, right? :)
– Sergio Tulentsev
Nov 11 at 16:52
yea I'm so lost. Only a week in learning ruby, so struggling with how each class inherits what. Guess I need to do more reading
– Daniel Bailey
Nov 11 at 16:53
1
@DanielBailey: "How would I get access to those instance variables inside of casino" - call super in casino's initializer, this should set name/money.
– Sergio Tulentsev
Nov 11 at 16:53
1
@DanielBailey: yeah, JS object model does that to people :) Look up a book called "practical object-oriented design in ruby" (AKA POODR). It's excellent. Might be overwhelming at first (at your skill level), but on a second or third read you'll get a level-up, guaranteed :)
– Sergio Tulentsev
Nov 11 at 16:54
|
show 3 more comments
yea JS lingo I can't drop ha. Ok so I am creating an instance of the wallet inside of the player, and creating an instance of the casino inside of the player. How would I get access to those instance variables inside of casino?
– Daniel Bailey
Nov 11 at 16:50
@DanielBailey: "I am creating an instance of the wallet inside of the player" - no you don't. In your hierarchy a casino IS a player, which is ALSO a wallet. Doesn't make any sense, right? :)
– Sergio Tulentsev
Nov 11 at 16:52
yea I'm so lost. Only a week in learning ruby, so struggling with how each class inherits what. Guess I need to do more reading
– Daniel Bailey
Nov 11 at 16:53
1
@DanielBailey: "How would I get access to those instance variables inside of casino" - call super in casino's initializer, this should set name/money.
– Sergio Tulentsev
Nov 11 at 16:53
1
@DanielBailey: yeah, JS object model does that to people :) Look up a book called "practical object-oriented design in ruby" (AKA POODR). It's excellent. Might be overwhelming at first (at your skill level), but on a second or third read you'll get a level-up, guaranteed :)
– Sergio Tulentsev
Nov 11 at 16:54
yea JS lingo I can't drop ha. Ok so I am creating an instance of the wallet inside of the player, and creating an instance of the casino inside of the player. How would I get access to those instance variables inside of casino?
– Daniel Bailey
Nov 11 at 16:50
yea JS lingo I can't drop ha. Ok so I am creating an instance of the wallet inside of the player, and creating an instance of the casino inside of the player. How would I get access to those instance variables inside of casino?
– Daniel Bailey
Nov 11 at 16:50
@DanielBailey: "I am creating an instance of the wallet inside of the player" - no you don't. In your hierarchy a casino IS a player, which is ALSO a wallet. Doesn't make any sense, right? :)
– Sergio Tulentsev
Nov 11 at 16:52
@DanielBailey: "I am creating an instance of the wallet inside of the player" - no you don't. In your hierarchy a casino IS a player, which is ALSO a wallet. Doesn't make any sense, right? :)
– Sergio Tulentsev
Nov 11 at 16:52
yea I'm so lost. Only a week in learning ruby, so struggling with how each class inherits what. Guess I need to do more reading
– Daniel Bailey
Nov 11 at 16:53
yea I'm so lost. Only a week in learning ruby, so struggling with how each class inherits what. Guess I need to do more reading
– Daniel Bailey
Nov 11 at 16:53
1
1
@DanielBailey: "How would I get access to those instance variables inside of casino" - call super in casino's initializer, this should set name/money.
– Sergio Tulentsev
Nov 11 at 16:53
@DanielBailey: "How would I get access to those instance variables inside of casino" - call super in casino's initializer, this should set name/money.
– Sergio Tulentsev
Nov 11 at 16:53
1
1
@DanielBailey: yeah, JS object model does that to people :) Look up a book called "practical object-oriented design in ruby" (AKA POODR). It's excellent. Might be overwhelming at first (at your skill level), but on a second or third read you'll get a level-up, guaranteed :)
– Sergio Tulentsev
Nov 11 at 16:54
@DanielBailey: yeah, JS object model does that to people :) Look up a book called "practical object-oriented design in ruby" (AKA POODR). It's excellent. Might be overwhelming at first (at your skill level), but on a second or third read you'll get a level-up, guaranteed :)
– Sergio Tulentsev
Nov 11 at 16:54
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250784%2fhow-to-access-variable-from-outside-class-in-another-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Ok sorry, edited the original
– Daniel Bailey
Nov 11 at 16:45
1
Aside from your actual question, this seems like a major misuse of inheritance. If class
A
inherits from classB
, then it should make sense to say "A is a type of B". In this case, is a Player a type of Wallet? No. Is a Casino a type of Player? No. I'd advise rethinking your class architecture.– Tom Lord
Nov 11 at 17:19
Yea so wallet inherits from player, and casino inherits from player, so I just called super from casino to instantiate each class. Which creates the wallet.
– Daniel Bailey
Nov 11 at 17:20
1
@DanielBailey "which creates the wallet" - again, not quite. "casino = Casino.new". This
casino
is a casino and a player and a wallet, all three in the same single object. I agree with Tom Lord, this is a misuse of inheritance. There's no chance this is needed in your app– Sergio Tulentsev
Nov 11 at 17:33