独立方法本身就是另一种方法

时间:2017-01-11 13:17:37

标签: ruby learn-ruby-the-hard-way

好的,我非常想把方法传递给方法,只要我想从方法中返回一些东西。你能解释我怎么去传递它吗? 这是我的哈希

$choosen_gun = {}
$Weapon = {
    :Bazoka => ["Bazoka",5],
    :Machine_gun => ["Machine_gun",1000],
    :Hand_gun => ["Hand_gun",24,2],
    :Double_Hand_gun => ["Double_Hand_gun",24,4],
    :Sniper => ["Sniper",12,1],
    :Shot_gun => ["Shot_gun",8,2]
}

这是我的方法武器代码

    def Weapon
        puts "Now it's time to select your weapon."
        puts "Please choose a weapon that is good throughout the game."
        puts "Whenever you are shortage of bullets, please reload it."
        puts "Please avoid last minute of reloading of weapon."
        puts "Now choose your weapon based on your own preferences."
        print  "\n"

        puts "Type 1"
        puts "Gun Name: Bazoka"
        puts "Description: A powerful gun that is strong with only 5 bullets."
        puts "Rating: ★ ★ ★ ★"
        num = gets.chomp.to_i

       case num 
          when 1 
          puts "Selection of Bazoka is chosen"
          puts "Loaded 5 bullets only"
          $choosen_gun[num] = $Weapon[:Bazoka]
       end      
     return num
end

调用方法后。用户将选择他的武器,并将其添加到$ choosen_gun散列中,并使用它的数字,并将其返回给用户输入的数字

这是我的方法ZombieRoom

的代码
    def ZombieRoom(w)
    zombie = {
        :Construcied => [5],
        :Invader => [5],
        :Damned => [5],
        :Steampunk => [5],
        :Stoner => [5],
        :Wasted => [5],
        :Romero => [5]
    }
             puts "Welcome to the worst night mare of Zombie Room"
             puts "You will be fighting with a random zombie"


             while true 
             puts ".........."
             puts "Selecting a random zombie"
             puts "Selecting your prefered gun...."
             case w 
                   when 1 
                   $choosen_gun[1]
                   puts "Your selected gun is #{$choosen_gun[1][0]}"
                   #values = zombie.values
                   #puts values[rand(values.size)]
                   #random_zombie = zombie.keys.sample(1)
                   #puts random_zombie[   
                    random_zombie = zombie.to_a.sample(1).to_h
                    random_zombie.each do |key,value|
                    puts "Your random zombie is #{key}"
                    puts "With a health value of #{value[0]}"


                    puts "Time to take down that zombie now."
                    while true
                    puts "Type Shoot to knock it down or quit."
                    choice = gets.chomp
                    if $choosen_gun[1][1] >= 1
                        health = value[0] -= 1
                        $choosen_gun[1][1] -= 1 
                    puts "#{key} health now is #{health}"
                    else
                    puts "Please reload your gun"
                    puts "Reloading......"
                    $choosen_gun[1][1] += 5  
                    end 

                    if health == 0 
                        puts "You have defeated #{key}"
                        puts "Congrats!!!"
                        puts "We are happy for you"
                        puts "Lets begins to collect your prize"
                         CollectPrize()
                     else
                        puts "You did not defeat the #{key} yet"
                    end

                    end

                    end
       end
     end
   end

这是我的方法CollectPrize

的代码
def CollectPrize
      puts "Congratulations on defeating"
      puts "We would now like to give you some case prizes"

      print "\n"

      puts "Please choose only 1 prize for yourself"
      print "\n"
      puts "Type 1"
      puts "$50,000"
      print "\n"
      puts "Type 2"
      puts "$25,000"
      print "\n"
      puts "Type 3"
      puts "$55,000"
      hoho = gets.chomp.to_f


      if hoho == 1
            puts "hehe"
      end
end

这是我如何调用我的方法

ZombieRoom(Weapon())
CollectPrize()

现在的问题是,无论何时调用CollectPrize方法并输入我的输入来收集奖品示例1,它都会打印出$ 50,000"。而不是结束问题,它回到了ZombieRoom并继续循环在"类型拍摄以击倒或退出。"至少有人能告诉我一个解决这个问题的正确方法,或者还有其他方法来传递方法吗?

2 个答案:

答案 0 :(得分:1)

在ruby常量中以大写字母开头。 方法总是以小写形式定义。

在irb

中尝试此操作
irb(main):001:0> def Weapon
irb(main):002:1> end
=> :Weapon
irb(main):003:0> Weapon
NameError: uninitialized constant Weapon

使用ruby的命名约定解决问题名称方法:
zombie_roomcollect_prize等。

然后这段代码将起作用:
zombie_room(weapon())

你在做什么并没有真正传递方法武器到方法僵尸室。 真正发生的是执行方法武器,然后返回一个值,并将该值的结果传递给方法zombie_room。

我认为这就是你想要的。

如果您需要传递方法,请查看proclambda的文档,或者只使用块。

答案 1 :(得分:1)

您的代码位于一个较大的while true循环中。由于true始终为true,因此它永远不会结束,因此在调用CollectPrize()之后,它只会返回while语句。

你可以通过在break之后插入CollectPrize()行来摆脱它,但是围绕这一行有另一个while true循环。

我认为您需要密切关注如何退出while循环。

puts "Time to take down that zombie now."
while true # <---------------- this is ALWAYS going to loop, without end
  puts "Type Shoot to knock it down or quit."
  choice = gets.chomp
  if $choosen_gun[1][1] >= 1
    health = value[0] -= 1
    $choosen_gun[1][1] -= 1 
    puts "#{key} health now is #{health}"
  else
    puts "Please reload your gun"
    puts "Reloading......"
    $choosen_gun[1][1] += 5  
  end 
  if health == 0 
    puts "You have defeated #{key}"
    puts "Congrats!!!"
    puts "We are happy for you"
    puts "Lets begins to collect your prize"
    CollectPrize()
  else
    puts "You did not defeat the #{key} yet"
  end
end