Elia
Elia

Reputation: 39

Accessing instance variable array using IRB

I'm new to Ruby and am working on an exercise where a sports team (of up to 10 ppl) have to have at least 2 men and 2 women. I've created a Player class where the player's gender is determined and a Team class, where I add these players into an instance variable array of @team (which is created upon initialization of Team).

I have placed my full code towards the bottom of this request.

I'm hoping someone can please help me on the following:

(1) What do I type in IRB to be able to specifically recall/maniuplate the @team instance variable array (which stores all the players). I wish to later iterate over the array or extract the first item in the array (@team.first does not work)

(2) I'm having difficulty writing the code to determine if at least 2 male and female players are in the @team instance variable. The best code I came up in the Team class was the following - but it is reporting that @team is nilclass.

def gender_balance
   @team.select{|player| player.male == true }
end

I have researched the internet and tried various combinations for an answer - with no success.

Directly below are the IRB commands that I have typed to create and add players to teams. Later below is my code for Team (which contains the method to assess whether it has the right gender mix) and Players.

irb(main):001:0> team = Team.new
=> #<Team:0x007fd8f21df858 @team=[]>

irb(main):002:0> p1 = Player.new
=> #<Player:0x007fd8f21d7b08 @male=true>

irb(main):003:0> p1.female_player
=> false

irb(main):004:0> p2 = Player.new
=> #<Player:0x007fd8f21bff58 @male=true>

irb(main):005:0> team.add_player(p1)
=> [#<Player:0x007fd8f21d7b08 @male=false>]

irb(main):006:0> team.add_player(p2)
=> [#<Player:0x007fd8f21d7b08 @male=false>, #<Player:0x007fd8f21bff58 @male=true>]

These two IRB lines are me trying unsucessfully to recall the contents of @team

irb(main):007:0> team
=> #<Team:0x007fd8f21df858 @team=[#<Player:0x007fd8f21d7b08 @male=false>, #<Player:0x007fd8f21bff58 @male=true>]>

irb(main):013:0> @team
=> nil

The code for both classes is below:

class Player
  def initialize
    @male = true
  end

  def female_player
    @male = false
  end

  def male_player
    @male
  end
end


class Team

  def initialize
    @team = []
  end

  def add_player player
    @team << player
  end

  def player_count
    @team.count
  end

  def valid_team?
    player_number_check
    gender_balance
  end

private
  def player_number_check
    player_count > 6 && player_count < 11
  end

 def gender_balance
   @team.select{|player| player.male == true }
 end
end

My github reference for this code is: https://github.com/elinnet/object-calisthenics-beach-volleyball-edition.git

Thank you.

Upvotes: 0

Views: 677

Answers (1)

C. K. Young
C. K. Young

Reputation: 223123

Your Team class does not have an attribute for getting the @team instance variable. So the only way you can extract its value is by using instance_variable_get:

irb(main):029:0> team = Team.new
=> #<Team:0x007fff4323fd58 @team=[]>
irb(main):030:0> team.instance_variable_get(:@team)
=> []

Please don't use instance_variable_get for actual production code though; it's a code smell. But for the purposes of inspecting instance variables in IRB, it's okay.

You'd normally define one using either attr_accessor :team (read/write) or attr_reader :team (read-only) inside the class definition.

Upvotes: 3

Related Questions