【问题标题】:boolean methods in ruby on railsruby on rails 中的布尔方法
【发布时间】:2015-05-17 05:28:55
【问题描述】:
# 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 返回:game_solved

这是我的猜测课

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,我返回 :game_completed 但对我不起作用

【问题讨论】:

  • 这不是初始化变量。这是定义方法。完全不同。您正在学习哪个教程/课程?
  • 这不是在线课程,是学院培训课程
  • 如果他们教你这个,你应该找另一个学院:)

标签: ruby-on-rails ruby rspec


【解决方案1】:

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

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

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

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

&lt;=&gt; 运算符比较两个值并返回 -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

【讨论】:

  • 为我的偷偷摸摸 +1 :)
【解决方案2】:

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

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,如果它不完整(计算结果为假),则返回nil,或者如果它已完成,则返回true。 希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2010-11-28
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多