ruby rails 中的布尔方法

boolean methods in ruby on rails

# As a user, you can initialize the guessing game with a number, which is 
the correct guess
# so the initialize method takes in one parameter, and sets game_complete? 
to false
# 
# As a user, I can guess the number, which will 
# return :too_high if its > answer
# return :too_low if its < answer
# return :correct if its = answer
# correct changes the game_complete? to true
# if a user guesses the incorrect number after guessing the correct number, 
it should
# change the game_complete? to false
# return :too_high or :too_low
require_relative 'guess'
describe GuessingGame do 
let(:game) { GuessingGame.new(50) }
describe "#initialize" do
it "expects a single parameter" do
  expect(GuessingGame.instance_method(:initialize).arity).to eq 1
  end
end
describe "#guess" do
it "expects a single parameter" do
  expect(GuessingGame.instance_method(:guess).arity).to eq 1
end

it "returns :too_low when the guess is lower than the answer" do
  expect(game.guess(1)).to eq :too_low
end

it "returns :too_high when the guess is higher than the answer" do
  expect(game.guess(100)).to eq :too_high
end

it "returns :correct when the guess matches answer" do
  expect(game.guess(50)).to eq :correct
end

it "changes game_complete? when the correct guess is made" do
  expect {
    game.guess(50)
    }.to change(game, :game_complete?).from(false).to(true)
end

it "doesn't change game_complete? when an incorrect guess is made" do
  expect {
    game.guess(10)
    }.to_not change(game, :game_complete?).from(false)
end

it "returns :game_solved once you try to guess in a completed game" do
  game.guess(50)
  expect(game.guess(100)).to eq :game_solved
  end
 end

describe "#game_complete?" do
it "returns false in a new game" do
  expect(game.game_complete?).to eq false
end
  end
end

现在,当我 运行 这段代码时,我得到错误 GuessingGame#guess returns :game_solved 一旦你尝试在一个完整的游戏中猜测

这是我的猜测class

class GuessingGame
    def initialize(num)
       @num=num
       def game_complete?
            return false
       end
    end
    def guess(num1)
        if num1<@num
            return :too_low
       elsif num1>@num
           return :too_high
       else
          def game_complete?
                return true
          end
          return :correct
       end
end

结束

我尝试用 false 初始化一个 bool 变量,一旦做出正确的猜测,我就将其设为 true,如果该变量为 true,我 return :game_completed 但对我不起作用

也许你可以尝试这样的事情,而不是定义方法只是将它们变成一个变量。

class GuessingGame
  attr_reader :game_complete
  def initialize(num)
     @num=num
  end

  def guess(num1)
     if num1<@num
          return :too_low
     elsif num1>@num
         return :too_high
     else
        @game_complete = true
     end
     return :correct
  end
end

现在要检查游戏是否完成,您可以使用 game.game_complete 如果游戏未完成(计算结果为 false)或完成则 return nil会 return true。 希望对你有帮助。

您似乎没有初始化主题,除非有更多代码没有显示。这是一个有效的版本:

class GuessingGame

  def initialize(num)
    @num = num
  end

  def guess(num)
    if num < @num
      return :too_low
    elsif num > @num
      return :too_high
    else
      return :correct
    end
  end

end

require 'spec_helper'
require 'foo'

describe GuessingGame do
  let(:foo) { GuessingGame.new(50) }

  describe "when guess is too low" do
    it "returns :too_low" do
      expect(foo.guess(25)).to eq :too_low
    end
  end

  describe "when guess is too high" do
    it "returns :too_high" do
      expect(foo.guess(75)).to eq :too_high
    end
  end

  describe "when guess is correct" do
    it "returns :correct" do
      expect(foo.guess(50)).to eq :correct
    end
  end

end

现在进行一些重构。从方法的中间到 return 通常不是一个好主意。 Ruby 总是 return 是最后一个表达式的值,因此我们可以利用它。

  def guess(num)
    case num <=> @num
    when -1
      :too_low
    when 1
      :too_high
    else
      :correct
    end
  end

<=> 运算符比较两个值和 returns -1、0 或 1。稍微偷偷摸摸,我们可以进一步将 guess 方法重构为一行:

  def guess(num)
    [:correct, :too_high, :too_low][num <=> @num]
  end

编辑

看来你还想定义另一种方法来指示游戏是否完成。这是一种方法:

class GuessingGame

  def initialize(num)
    @num = num
  end

  def compare(num)
    [:correct, :too_high, :too_low][num <=> @num]
  end

  def guess(num)
    @comparison = compare(num)
  end

  def game_complete?
    @comparison == :correct
  end

end