我試圖在Ruby中作出函數,告訴如果一個字符串是迴文。我已經檢查了以前的例子,但是我找不到通過我的代碼上運行的測試的解決方案。這個任務以這一小段代碼開始。測試palindromes
def palindrome?(string)
# your code here
end
這就是我所做的。
class String
def palindrome?(string)
y = string.downcase.scan(/\w/)
if y == y.reverse
puts "Palindrome"
else
puts "Not a palindrome"
end
end
end
而這就是測試的結果。
Failure/Error: palindrome?("A man, a plan, a canal -- Panama").should be_true
NoMethodError:
undefined method `palindrome?' for
#<RSpec::Core::ExampleGroup::Nested_1:0x00000002c5e368>
# ./spec.rb:3:in `block (2 levels) in <top (required)>'
# ./lib/rspec_runner.rb:36:in `block in run_rspec'
# ./lib/rspec_runner.rb:32:in `run_rspec'
# ./lib/rspec_runner.rb:23:in `run'
# lib/graders/weighted_rspec_grader.rb:6:in `grade!'
# ./grade:32:in `<main>'
根據問題的性質和措辭和grader'的'在錯誤的存在,我假定這是一個家庭作業的問題,並適當應用的標籤。如果我錯了,請告訴我。 – Phrogz
作業標籤現在正式被棄用。 http://meta.stackexchange.com/questions/147100/the-homework-tag-is-now-officially-deprecated –