未定义的方法错误 - 之后修改案例语句中的哈希值

时间:2014-03-26 23:52:16

标签: ruby

所以,我做了一个名为"面对"的哈希。我之后调用一个case语句,如果用户输入L,我想从值中减去1,或者(我还没到这里)如果输入=' R'则加1。 / p>

class Rover
  attr_accessor :orientation
end

#facing = Hash.new

facing = {
  0 => 0
}

bot = Rover.new
bot.orientation = "N"

puts "Bot's current orientation is: " + bot.orientation 
puts "What direction to turn ('L' or 'R')?"

input = gets.chomp.to_s.capitalize

case input
  when input = 'L'
    facing do |key, value|
      value - 1
    end
end

问题是我得到一个方法未定义(面对)错误消息。我做错了什么?

1 个答案:

答案 0 :(得分:0)

Have a look at the documentation for case

此声明:input = 'L'表示“将变量input赋予值'L'”,而不是“检查它是否等于'L'”。

但这不是你的错误来自哪里。使用facing do...你给一个哈希块,这会让编译器感到困惑并导致它寻找facing方法。

你究竟想用哈希做什么?