使用变量将参数传递给ruby中的函数

时间:2012-03-10 12:31:17

标签: ruby function arguments argument-passing

按照教程Zed A. Shaw,我正在写一个足球比赛。到目前为止,我的代码如下:

$team_a_players = ["Basar", "Mehmet", "Abdullah", "Alpaslan", "Salih", "Recep", "Ibrahim", "Orhan", "Hakki", "Yakup", "Serdar"]
$team_a_substitutes = ["Hasan", "Turgay", "Umit"]
$team_b_players = ["Habib", "Erkan", "Sahin", "Cemal", "Ahmet", "Fikret", "Yucel", "Pergel", "Ali", "Sabri", "Yilmaz"]
$team_b_substitutes = ["Abdulkadir", "Gokhan", "Mustafa"]
$yellow = []
$red = []
$reasons = ["corner", "direct attack", "free kick", "side attack", "speed kick"]
$team_a_attack = 90.0
$team_a_defense = 80.0
$team_b_attack = 70.0
$team_b_defense = 60.0
$team_a_goals = 0
$team_b_goals = 0

def prompt()
  print "> "
end

def dice()
  if rand(2) == 0
    round_team_a()

  else
    round_team_b()
  end
end

def fauls()
  if rand(0) > 0.95 and rand(10) % 2 == 0
    faul_player = $team_a_players[rand(11)]
    if $yellow.include?(faul_player)
      $red.push(faul_player)
      $team_a_players.delete("faulplayer")
      puts "#{faul_player} of Team A gets a red card in #{$i}. minute!"
      puts "Who would you like to substitute in place of #{faul_player}?"
      list_subs_a()
      prompt()
      substitute = STDIN.gets.chomp()
      $team_a_players.push("substitute")
      $yellow.delete(faul_player)

    else
      $yellow.push(faul_player)
      puts "#{faul_player} of Team A gets a yellow card in #{$i}. minute!"
    end

  elsif rand(0) > 0.95 and rand(10) % 2 == 1
    faul_player = $team_b_players[rand(11)]
    if $yellow.include?(faul_player)
      $red.push(faul_player)
      $team_b_players.delete("faulplayer")
      puts "#{faul_player} of Team B gets a red card in #{$i}. minute!"
      puts "Who would you like to substitute in place of #{faul_player}?"
      list_subs_b()
      prompt()
      substitute = STDIN.gets.chomp()
      $team_b_players.push("substitute")
      $yellow.delete(faul_player)

    else
      $yellow.push(faul_player)
      puts "#{faul_player} of Team B gets a yellow card in #{$i}. minute!"
    end

  else
    faul_player = nil
  end
end

def list_subs_a()
  $team_a_substitutes.each {|p| puts p}
end

def list_subs_b()
  $team_b_substitutes.each {|p| puts p}
end

def list_yellow()
  $yellow.each {|p| puts p}
end

def list_red()
  $red.each {|p| puts p}
end

def round_team_a()
  score = $team_a_attack / $team_b_defense * rand(0)
  if score > 1
    goal = 1
    $team_a_goals += 1
    reason = $reasons[rand(5)]
    puts "Team A scored #{goal} goal through a #{reason} in #{$i}. minute!"

  else
    goal = 0
  end
end

def round_team_b()
  score = $team_b_attack / $team_a_defense * rand(0)
  if score > 1
    goal = 1
    $team_b_goals += 1
    reason = $reasons[rand(5)]
    puts "Team B scored #{goal} goal through a #{reason} in #{$i}. minute!"

  else
    goal = 0
  end
end

def match()
  $i = 0
  until $i > 59 do
    dice()
    fauls()
    $i += 1
  end
  puts "Team A scored a total of #{$team_a_goals} goals and Team B scored a total of #{$team_b_goals} goals."
  if $team_a_goals > $team_b_goals
    puts "Team A won against Team B by #{$team_a_goals}:#{$team_b_goals}!"

  elsif $team_b_goals > $team_a_goals
    puts "Team B won against Team A by #{$team_b_goals}:#{$team_b_goals}!"

  else
    puts "It's a tie!"
  end

  if $yellow.length > 0
    puts "Players shown a yellow card are:"
    list_yellow() 

  else
    puts "No yellow cards in the end of the game"
  end

  if $red.length > 0
    puts "Players shown a red card are:"
    list_red()

  else
    puts "No red cards in the end of the game"
  end
end

match()

从这里开始,我想做以下事情:

  • 使用哈希值替换数组$yellow$red,以便我还可以报告黄卡和红卡的分钟数和小组数。
  • 用名称$team_替换带有哈希的数组,以便我可以向玩家添加个性化的攻击和防御力量,以便替换意味着......但在代码变得更复杂之前,我必须解决问题。这类似于this question concerning php
  • listround共有的方式定义函数faula_playersb_players。我尝试过team_#{team}_players代替team_a_players等,但无法实现。

我寻求的是该问题的指南,而不是解决方案,以便我自己解决这个问题。清晰的单词中的链接或长解释比固定代码更受欢迎。请注意,该教程尚未提及类,所以这还不是一个选项。

1 个答案:

答案 0 :(得分:1)

你似乎没有掌握的基本思想是将参数传递给函数。 假设我们有两个全局变量,我们希望对它们执行相同的操作 - 比如将数组的元素乘以2。 你写道:

$a = [1,2,3]
$b = [2,3,4]

def multiply_a
  result = []
  for element in $a do
    result << element * 2
  end
  result
end

def multiply_b
  result = []
  for element in $b do
    result << element * 2
  end
  result
end

但是这段代码非常坏。首先,您应该注意到Ruby $a是一个特殊变量 - 全局变量。你永远不需要使用它们 - 编写包含它们的代码意味着它有问题。那么如何解决呢? 基本思想是将参数传递给函数,而不是硬编码变量,函数操作。因此,您可以按如下方式转换代码:

a = [1, 2, 3]
b = [2, 3, 4]
def multiply(argument)
  result = []
  for element in argument do
    result << element * 2
  end
  result
end
# and then call
multiply(a) # instead of multiply_a
multiply(b) # instead of multiply_b 

这是您应该修改代码的方式。