如何在Ruby类中初始化类

时间:2019-02-22 16:13:08

标签: ruby class

我正在学习Ruby,但遇到了麻烦,所以我有一个问题。 如何在另一个类中初始化该类,这可能吗? 我尝试编写一个简单的游戏“ Dog's Runs”。 这是课程:

DOG类,显示有关狗的信息。

class DOG
  def initialize(breed,name,weight=0,height=0,speed=0)
    @the_weight = weight
    @the_height = height
    @the_breed = breed.capitalize
    @the_name = name.capitalize
    @the_speed = speed
  end

  def to_s
    "                    DOG INFORMATION
                  His name is: #{@the_name}
                  His breed is: #{@the_breed}
                  His weight is: #{@the_weight} cm
                  His height is: #{@the_height} kg
              His average speed is: #{@the_speed} km/h
        "
  end
end

GAME类

class GAME
    def initialize_game
       luckS = rand(0..10)
       luckR = rand(0..10)
       luckW = rand(0..10)
        if choose_dog == "Steve\n" or choose_dog == "steve\n" or choose_dog == "whippet\n" or choose_dog == "Whippet\n"
            if choose_rate == 3.33
                puts "You choosed , your rate is 3.33!"
                puts "The game starts!"
                if luckS > luckR and luckS > luckW
                    puts "You win!"
                    puts "Take your cash."
                    money = money + (money*choose_rate)
                else
                    puts "You lose"
                    money = money - (money*choose_rate)
                end
            end
            if choose_rate == 6.66
                puts "You choosed , your rate is 6.66!"
                puts "The game is starts!"
                if luckS > luckR and luckS > luckW
                    puts "You win!"
                    puts "Take your cash."
                    money = money + (money*choose_rate)
                else
                    puts "You lose"
                    money = money - (money*choose_rate)
                end
            end
            if choose_rate == 10.00
                puts "You choosed , your rate is 10.00!"
                puts "The game is starts!"
                if luckS > luckR and luckS > luckW
                    puts "You win!"
                    puts "Take your cash."
                    money = money + (money*choose_rate)
                else
                    puts "You lose"
                    money = money - (money*choose_rate)
                end
            end
        end
        if choose_dog == "Richard\n" or choose_dog == "richard\n" or choose_dog == "greyhound\n" or choose_dog == "Greyhound\n"
            puts "Great pick!"
            puts "Choose rate!"
            puts "First rate is 3.33 , please write in 3.33 if you want to pick that rate"
            puts "Second rate is 3.33 , please write in 6.66 if you want to pick that rate"
            puts "Third rate is 3.33 , please write in 10.00 if you want to pick that rate"
            if choose_rate == 3.33
                puts "You choosed , your rate is 3.33!"
                puts "The game is starts!"
                if luckR > luckS and luckR > luckW
                    puts "You win!"
                    puts "Take your cash."
                    money = money + (money*choose_rate)
                else
                    puts "You lose"
                    money = money - (money*choose_rate)
                end
            end
            if choose_rate == 6.66
                puts "You choosed , your rate is 6.66!"
                puts "The game is starts!"
                if luckR > luckS and luckR > luckW
                    puts "You win!"
                    puts "Take your cash."
                    money = money + (money*choose_rate)
                else
                    puts "You lose"
                    money = money - (money*choose_rate)
                end
            end
            if choose_rate == 10.00
                puts "You choosed , your rate is 10.00!"
                puts "The game is starts!"
                if luckR > luckS and luckR > luckW
                    puts "You win!"
                    puts "Take your cash."
                    money = money + (money*choose_rate)
                else
                    puts "You lose"
                    money = money - (money*choose_rate)
                end
            end
        end
        if choose_dog == "Whilley\n" or choose_dog == "whilley\n" or choose_dog == "saluki\n" or choose_dog == "Saluki\n"
            puts "Great pick!"
            puts "Choose rate!"
            puts "First rate is 3.33 , please write in 3.33 if you want to pick that rate"
            puts "Second rate is 3.33 , please write in 6.66 if you want to pick that rate"
            puts "Third rate is 3.33 , please write in 10.00 if you want to pick that rate"
            if choose_rate == 3.33
                puts "You choosed , your rate is 3.33!"
                puts "The game is starts!"
                if luckW > luckS and luckW > luckR
                    puts "You win!"
                    puts "Take your cash."
                    money = money + (money*choose_rate)
                else
                    puts "You lose"
                    money = money - (money*choose_rate)
                end
            end
            if choose_rate == 6.66
                puts "You choosed , your rate is 6.66!"
                puts "The game is starts!"
                if luckW > luckS and luckW > luckR
                    puts "You win!"
                    puts "Take your cash."
                    money = money + (money*choose_rate)
                else
                    puts "You lose"
                    money = money - (money*choose_rate)
                end
            end
            if choose_rate == 10.00
                puts "You choosed , your rate is 10.00!"
                puts "The game is starts!"
                if luckW > luckS and luckW > luckR
                    puts "You win!"
                    puts "Take your cash."
                    money = money + (money*choose_rate)
                else
                    puts "You lose"
                    money = money - (money*choose_rate)
                end
            end
        end
    end
end

以及其他必须运行GAME类的代码:

puts "Hello to our \"Dog's Runs!\""
        money = 1000
        puts "Here's information about our dogs"
        dogs=[DOG.new("whippet","Steve",15,48,50),DOG.new("greyhound","Richard",34,73,70),DOG.new("saluki","Whilley",20,61,60)]
        puts dogs
        puts "Please , write in dog's name or breed:"
        choose_dog = gets.to_s
        puts "Great pick!"
        puts "Choose rate!"
        puts "First rate is 3.33 , please write in 3.33 if you want to pick that rate"
        puts "Second rate is 3.33 , please write in 6.66 if you want to pick that rate"
        puts "Third rate is 3.33 , please write in 10.00 if you want to pick that rate"
        choose_rate = gets.to_f
        GAME.new.initialize_game

我更改了大部分代码,但是我无法初始化该GAME类,我使用class_name和method编写了GAME.new.initialize_game,但这仍然行不通。

2 个答案:

答案 0 :(得分:0)

我在这里猜测您要问的是什么,但是查看您的代码和注释,您正在尝试将您创建的狗(在最后一个片段中)放入您创建的“游戏”中。

通过遵循不同的设计模式,您可以采用许多巧妙的方法,但是在您学习时,我会坚持一些更简单的方法:告诉Game类存在的那只狗:< / p>

let Tree = function(name) {
  this.name = name;
  this.children = [];
}

Tree.prototype.addChild = function(parent, name) {
  const node = lannister.contains(parent);
  node.children.push(new Tree(name));
}

var lannister = new Tree('Tywin');

lannister.addChild('Tywin', 'Jamie');
lannister.addChild('Tywin', 'Cersi');
lannister.addChild('Tywin', 'Tyrion');
lannister.addChild('Cersi', 'Joffery');
lannister.addChild('Cersi', 'Myrcella');
lannister.addChild('Cersi', 'Tomen');
lannister.addChild('Tyrion', 'Lin');
bfs();

function bfs(check) {
  //   console.log('traverseBFS called: ', this);
  bfsArray = [];
  var queue = [this];
  var node;
  var tempAr = [];
  bfsArray.push([this.name]);

  while (queue.length > 0) {
    node = queue.shift();
    callback(node.name, node.children.length);
    console.log('current node is: ', node, node.children.length);

    for (var i = 0; i < node.children.length; i++) {
      queue.push(node.children[i]);
      tempAr.push(node.children[i].name);
      if (i === node.children.length - 1) {
        // console.log('The bfs is ', bfsArray, tempAr);
        bfsArray.push(tempAr);
        tempAr = [];
      }
    }
  }

  function callback(itm, length) {

    // console.log('itm is: ', itm);
    if (itm === check) {
      node;
    }
  }
};

您可以选择在创建游戏时自动创建狗:

class Dog
  attr_reader :name

  def initialize(name)
    @name = name
  end
end

class Game
  attr_reader :dogs

  def initialize(*dogs)
    @dogs = dogs
  end

  def play
    puts "For tonights race we have:"
    dogs.each do |dog|
      puts dog.name
    end
  end
end

d1 = Dog.new('Joe')
d2 = Dog.new('Sally')
d3 = Dog.new('Steve')

game = Game.new(d1, d2, d3)

game.play

然后您可以将您的选择集成到游戏代码中:

class Dog
  attr_reader :name

  def initialize(name)
    @name = name
  end
end

class Game
  attr_reader :dogs

  def initialize
    load_dogs
  end

  def play
    puts "For tonights race we have:"
    dogs.each do |dog|
      puts dog.name
    end
  end

  private

  def load_dogs
    @dogs = []
    @dogs << Dog.new('Joe')
    @dogs << Dog.new('Sally')
    @dogs << Dog.new('Steve')
  end
end

game = Game.new

game.play

答案 1 :(得分:0)

我运行了您的代码,并且有明确的错误消息。也许您以某种不显示错误消息的方式运行代码?

Great pick!
Choose rate!
First rate is 3.33 , please write in 3.33 if you want to pick that rate
Second rate is 3.33 , please write in 6.66 if you want to pick that rate
Third rate is 3.33 , please write in 10.00 if you want to pick that rate
6.66
/Users/schwern/tmp/test.rb:26:in `initialize_game': undefined local variable or method `choose_dog' for #<GAME:0x00007f9a7208a5c0> (NameError)
    from /Users/schwern/tmp/test.rb:171:in `<main>'

Game#initialize_game引用choose_dogchoose_ratemoney。尽管这些变量是主程序中的变量,但其他任何地方都无法使用它们。这使您可以将程序的各个部分相互隔离。如果要在某种方法中使用它们,则需要将它们传递进来。

# In GAME
def initialize_game(choose_dog, choose_rate, money)

# When calling it.
GAME.new.initialize_game(choose_dog, choose_rate, money)