Ruby方法 - 不会采取第二个参数 - 二十一点

时间:2014-10-17 15:58:49

标签: ruby methods parameters

建立二十一点游戏。如果我在display_cards方法中放入第二个参数,一切都会崩溃。如果我从方法中取出参数然后调用它,一切正常。出了什么问题?

def play
  game_deck = initialize_deck
  game_deck = shuffle_deck(game_deck)
  human_cards = []
  computer_cards = []
end

def initialize_deck (number_of_decks = 6)
  deck_values = ["A", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
  suits = ["-Hearts", "-Diamonds", "-Clubs", "-Spades"]
  deck = []
  number_of_decks.times do 
    suits.each do |suit|
      deck_values.each do |value|
        deck << value+suit
      end
    end
  end
  return deck
end

def display_cards (player_cards , player_name)
  card_string = "#{player_name}'s Cards\n-----------------"

  player_cards.each do |card|
    card_string += "\n| #{card}"
    card_string += "\n-----------------"
  end
  puts card_string
end

def shuffle_deck(deck , number_of_shuffles = 6)
  number_of_shuffles.times do
    deck = deck.sample(deck.count)
  end
  return deck
end

def pull_card(game_deck, player_cards)
  player_cards << deck.shift
end
test_cards = ["A-Hearts", "3-Spades", "10-Diamonds"]
name = "ARealHumanBean"
display_cards (test_cards , name)

2 个答案:

答案 0 :(得分:1)

删除此处的空格:

display_cards (test_cards , name)
             ^

Ruby我认为将(test_cards,name)视为块或结构,并尝试将其作为单个参数传递给函数display_cards。

有趣的是,它在jruby中完美无缺。

答案 1 :(得分:1)

由于空间的缘故,Ruby相信&#34;(test_cards,name)&#34;是第一个论点,并抱怨缺少第二个论点。

这些语法将起作用

display_cards(test_cards, name)
display_cards test_cards, name