How to pass variables between Ruby classes?

I am creating a card game with several classes. I currently use global variables to store $shuffled_deck , $players_hand and $dealers_hand , but I am worried about using global variables (perhaps unnecessarily) and prefer to use instance variables.

I read, but clicks nothing. Can anyone help point me in the right direction?

Using Instance Variables I was unable to save @players_hand and @dealers_hand to use in other classes. For example, I have @players_hand from the Player class. I have the Dealer class draw a map, but I can not pull this @players_hand into the Dealer class to add them together.

My current code is:

 class Blackjack def initialize @player = Player.new @dealer = Dealer.new end end class Dealer def initialize @deck = Deck.new $dealers_hand = 0 end def hit_dealer @deck.hit_dealer end def hit_player @deck.hit_player end def draw_card @hit = $shuffled_deck end def shuffle @deck.suits end end class Player def initialize $players_hand = 0 end end class Deck def suits #code that shuffled the deck.. $shuffled_deck = @shuffled_deck end def hit_player @hit = $shuffled_deck.pop end def hit_dealer @hit = $shuffled_deck.pop end end 
+6
source share
2 answers

Do you want to use attr_reader , attr_writer or attr_accessor . Here's how they work:

  • attr_reader :players_hand : Lets you write some_player.players_hand to get the value of the players_hand instance players_hand this player
  • attr_writer :players_hand : Lets you write some_player.players_hand = 0 to set the variable to 0
  • attr_accessor :players_hand : Allows you to read and write as if you were using both attr_reader and attr_writer .

By the way, all of these are recording methods for you. If you want, you can do it manually as follows:

 class Player def initialize @players_hand = 0 end def players_hand @players_hand end def players_hand=(new_value) @players_hand = new_value end end 
+4
source

using your example, you can do it as follows

 class Blackjack attr_reader :player, :dealer def initialize @player = Player.new @dealer = Dealer.new end end class Dealer def dealers_hand #the long java way of a getter @dealers_hand end #and now the short ruby way attr_reader :dealers_hand #if you only need to read the attribute attr_writer :dealers_hand #if you only need to write the attribute attr_accessor: dealers_hand #if you need both def initialize @deck = Deck.new @dealers_hand = 5 end def hit_dealer @deck.hit_dealer end def hit_player @deck.hit_player end def draw_card @hit = $shuffled_deck end def shuffle @deck.suits end end class Player attr_reader :players_hand def initialize @players_hand = 0 end end class Deck def suits attr_reader :shuffled_deck @shuffled_deck = @shuffled_deck end def hit_player @hit = $shuffled_deck.pop end def hit_dealer @hit = $shuffled_deck.pop end end game = Blackjack.new p game.dealer.dealers_hand game.dealer.dealers_hand = 4 p game.dealer.dealers_hand 
+5
source

Source: https://habr.com/ru/post/909099/


All Articles