How to access variable from outside class in another file










2















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.










share|improve this question



















  • 1





    Ok sorry, edited the original

    – Daniel Bailey
    Nov 11 '18 at 16:45






  • 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 '18 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 '18 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 '18 at 17:33
















2















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.










share|improve this question



















  • 1





    Ok sorry, edited the original

    – Daniel Bailey
    Nov 11 '18 at 16:45






  • 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 '18 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 '18 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 '18 at 17:33














2












2








2








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 '18 at 16:45







Daniel Bailey

















asked Nov 11 '18 at 16:29









Daniel BaileyDaniel Bailey

67110




67110







  • 1





    Ok sorry, edited the original

    – Daniel Bailey
    Nov 11 '18 at 16:45






  • 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 '18 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 '18 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 '18 at 17:33













  • 1





    Ok sorry, edited the original

    – Daniel Bailey
    Nov 11 '18 at 16:45






  • 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 '18 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 '18 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 '18 at 17:33








1




1





Ok sorry, edited the original

– Daniel Bailey
Nov 11 '18 at 16:45





Ok sorry, edited the original

– Daniel Bailey
Nov 11 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 17:33













1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer























  • 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 '18 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 '18 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 '18 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 '18 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 '18 at 16:54











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















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

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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.






share|improve this answer























  • 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 '18 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 '18 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 '18 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 '18 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 '18 at 16:54
















1














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.






share|improve this answer























  • 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 '18 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 '18 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 '18 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 '18 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 '18 at 16:54














1












1








1







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 '18 at 16:48









Sergio TulentsevSergio Tulentsev

181k30289305




181k30289305












  • 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 16:54


















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế