无法将用户输入插值到另一种方法中

时间:2019-06-04 00:33:56

标签: ruby

我从espn.com抓取了所有金州勇士球员的数据,并希望显示每个球员的每个属性。例如他们的身高,薪水和大学。一旦让用户能够选择他们想了解更多的球员。我想将这些信息发布给用户,但我看到该过程中存在错误。

这是我编写的代码。


  attr_accessor :player, :continue

  def intialize
   @continue = true 
  end

  def call 
    start 
    #skips over while loop
    #while @continue
    list_players
    menu
    show_player if @continue == true && @player != nil
  #end
    goodbye
  end

  def start
   puts "Welcome to the Golden State Roster 'The Home Of The Splash Brothers'"
  end

  def goodbye
    puts "See you next time for any players updates!"
  end

  def list_players
    Player.all.clear
    Scraper.scrape_page
    Player.all.each.with_index(1) do |player, index|
      puts "#{index}. #{player.name}"
    end
  end

  def menu
    input = ""
    while input != "exit"
    puts "Choose any player you wish by the 'number', type 'list' to reshow list or type 'exit' when finished."
    input = gets.strip.downcase
      if input.to_i > 0                      
        the_player = @player[input.to_i - 1]  

      puts "#{the_player.name} plays #{the_player.position} for the Golden State Warriors. He is #{the_player.age} years old and #{the_player.height} tall. He comes in weighing #{the_player.weight}. #{the_player.name} graduated from #{the_player.college} and makes honest living of #{the_player.salary} dollars per year."
        "Select another player by typing 'yes' or press 'exit' when you're ready to leave."

        binding.pry
      elsif input == "exit"
        @continue = false
      # elsif input.to_i.between?(1, Player.all.length)
      #   @player = Player.all[input.to_i - 1]
      elsif input == "list"
      list_players
      else
        puts "Error, Choose any player you wish by the number or type 'exit' when finished."

      end
    end
  end  ```

It breaks at "the_player"

```This is the error

```Welcome to the Golden State Roster 'The Home Of The Splash Brothers'
1. Jordan Bell
2. Andrew Bogut
3. Quinn Cook
4. DeMarcus Cousins
5. Stephen Curry
6. Marcus Derrickson
7. Kevin Durant
8. Jacob Evans
9. Draymond Green
10. Andre Iguodala
11. Jonas Jerebko
12. Damian Jones
13. Damion Lee
14. Shaun Livingston
15. Kevon Looney
16. Alfonzo McKinnie
17. Klay Thompson
Choose any player you wish by the 'number', type 'list' to reshow list or type 'exit' when finished.
4
Traceback (most recent call last):
        2: from ./bin/ballers:6:in `<main>'
        1: from /home/Thisforbliss/Development/project/lib/project/cli.rb:17:in`call'
/home/Thisforbliss/Development/project/lib/project/cli.rb:45:in `menu': undefined method `[]' for nil:NilClass (NoMethodError)```

0 个答案:

没有答案