独立方法是自己调用另一个方法

Stand alone method is calling another method by itself

好吧,每当我想从方法中 return 传递方法时,我真的很讨厌将方法传递给方法。你们能解释一下我如何通过它吗? 这是我的哈希

$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]
}

这是我的方法 Weapon

的代码
    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 散列中,它是 num,它是 return 用户输入的是 num

这是我的方法 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 ",000"
      print "\n"
      puts "Type 2"
      puts ",000"
      print "\n"
      puts "Type 3"
      puts ",000"
      hoho = gets.chomp.to_f


      if hoho == 1
            puts "hehe"
      end
end

这里是我如何调用我的方法

ZombieRoom(Weapon())
CollectPrize()

现在的问题是,每当调用 CollectPrize 方法并且我键入我的输入以收集奖品示例 1 时,它就会打印“$50,000”。它没有结束问题,而是回到了 ZombieRoom 并继续在 "Type Shoot to knock it down or quit." 循环

在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())

你在那里做的并不是真正的将方法武器传递到方法僵尸房间。 真正发生的是方法 weapon 被执行,然后它 returns 一个值和该值的结果被传递给方法 zombie_room.

我想这就是你想要的。

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

您的代码处于大型 while true 循环中。因为 true 永远是 true,它永远不会结束,所以在它调用 CollectPrize() 之后它就返回到 while 语句。

您可以通过在 CollectPrize() 之后插入 break 行来摆脱它,但是围绕这一行还有另一个 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