ruby - rspec - 如何与哈希进行比较?语法错误,意外 =>
ruby - rspec - how to compare to a hash? Syntax error, unexpected =>
我的哈希似乎没问题,为什么我会收到语法错误?
得到
SyntaxError:
.../x_and_o_spec.rb:14: syntax error, unexpected =>, expecting '}'
expect(board).to eq {0 =>"-", 1 =>"-", 2 =>"-", 3 =>"
^
.../x_and_o_spec.rb:14: syntax error, unexpected ',', expecting keyword_end
expect(board).to eq {0 =>"-", 1 =>"-", 2 =>"-", 3 =>"-",
^
.../x_and_o_spec.rb:14: syntax error, unexpected ',', expecting end-of-input
oard).to eq {0 =>"-", 1 =>"-", 2 =>"-", 3 =>"-", 4 =>"-",
如果我注释掉我的期望并打印我得到的哈希值:
{0=>"-", 1=>"-", 2=>"-", 3=>"-", 4=>"-", 5=>"-", 6=>"-", 7=>"-", 8=>"-", 9=>"-"}
那么为什么我的 expect 会给出这些错误消息?
代码
class Grid
attr_accessor :board
def initialize
@board = {}
(0..9).each do |key|
@board[key] = "-"
end
end
end
测试
it 'Grid has 9 elements, each element is a value of nil, o or X' do
board = Grid.new.board
expect(board).to eq {0 =>"-", 1 =>"-", 2 =>"-", 3 =>"-", 4 =>"-", 5 =>"-", 6 =>"-", 7 =>"-", 8 =>"-", 9 =>"-"}
end
程序员(我)对解析器规则的无知,如果期望是一个散列。
并没有意识到使用 {}
's
的散列/块问题
用括号解决它!
expect(board).to eq ({0 =>"-", 1 =>"-", ... 9 =>"-"})
# added these parens: /\ /\
我的哈希似乎没问题,为什么我会收到语法错误?
得到
SyntaxError:
.../x_and_o_spec.rb:14: syntax error, unexpected =>, expecting '}'
expect(board).to eq {0 =>"-", 1 =>"-", 2 =>"-", 3 =>"
^
.../x_and_o_spec.rb:14: syntax error, unexpected ',', expecting keyword_end
expect(board).to eq {0 =>"-", 1 =>"-", 2 =>"-", 3 =>"-",
^
.../x_and_o_spec.rb:14: syntax error, unexpected ',', expecting end-of-input
oard).to eq {0 =>"-", 1 =>"-", 2 =>"-", 3 =>"-", 4 =>"-",
如果我注释掉我的期望并打印我得到的哈希值:
{0=>"-", 1=>"-", 2=>"-", 3=>"-", 4=>"-", 5=>"-", 6=>"-", 7=>"-", 8=>"-", 9=>"-"}
那么为什么我的 expect 会给出这些错误消息?
代码
class Grid
attr_accessor :board
def initialize
@board = {}
(0..9).each do |key|
@board[key] = "-"
end
end
end
测试
it 'Grid has 9 elements, each element is a value of nil, o or X' do
board = Grid.new.board
expect(board).to eq {0 =>"-", 1 =>"-", 2 =>"-", 3 =>"-", 4 =>"-", 5 =>"-", 6 =>"-", 7 =>"-", 8 =>"-", 9 =>"-"}
end
程序员(我)对解析器规则的无知,如果期望是一个散列。
并没有意识到使用 {}
's
用括号解决它!
expect(board).to eq ({0 =>"-", 1 =>"-", ... 9 =>"-"})
# added these parens: /\ /\