RUBY do循环和if语句执行不正确
我是一名Ruby新手,正在完成与构建循环相关的家庭作业练习。该问题的RSPEC标准如下所示。我尝试了很多方法 我已经能够通过前两个条件,但我完全被困在如何匹配和转换英语文章(a,an,the)上。我似乎没有在方法中正确地循环和使用if-then构造 你能提出我可以解决这个问题的想法吗?或者如果我缺少一个简单的语法问题RUBY do循环和if语句执行不正确,ruby,loops,Ruby,Loops,我是一名Ruby新手,正在完成与构建循环相关的家庭作业练习。该问题的RSPEC标准如下所示。我尝试了很多方法 我已经能够通过前两个条件,但我完全被困在如何匹配和转换英语文章(a,an,the)上。我似乎没有在方法中正确地循环和使用if-then构造 你能提出我可以解决这个问题的想法吗?或者如果我缺少一个简单的语法问题 describe "Title" do describe "fix" do it "capitalizes the first letter of each word" d
describe "Title" do describe "fix" do
it "capitalizes the first letter of each word" do
expect( Title.new("the great gatsby").fix ).to eq("The Great Gatsby")
end
it "works for words with mixed cases" do
expect( Title.new("liTTle reD Riding hOOD").fix ).to eq("Little Red Riding Hood")
end
it "downcases articles" do
expect( Title.new("The lord of the rings").fix ).to eq("The Lord of the Rings")
expect( Title.new("The sword And The stone").fix ).to eq("The Sword and the Stone")
expect( Title.new("the portrait of a lady").fix ).to eq("The Portrait of a Lady")
end
it "works for strings with all uppercase characters" do
expect( Title.new("THE SWORD AND THE STONE").fix ).to eq("The Sword and the Stone")
end
end
以下是我到目前为止提出的代码:
class Title
def initialize(book)
@book=book
end
def fix
#create an array of articles
art = ['a','an','the']
#reformat any string to all lower case to reset to a common format
@book.downcase.split(' ').map {|w| w.capitalize }.join(' ')
#resplit the oll lower case string into an array
arr = @book.split(' ')
#for each element of the array check if in art array if tru
arr.each_with_index do |element, index|
if art.include?(element) then element.downcase
elsif index == 1 then element.upcase
else element.upcase
end
end
arr.join(' ')
end
end
以下是我收到的错误消息:
• RSpec::Expectations::ExpectationNotMetError
expected: "The Great Gatsby" got: ["the", "great", "gatsby"] (compared using ==)
exercise_spec.rb:6:in `block (3 levels) in <top (required)>'
• Title fix works for words with mixed cases
RSpec::Expectations::ExpectationNotMetError
expected: "Little Red Riding Hood" got: ["liTTle", "reD", "Riding", "hOOD"] (compared using ==)
exercise_spec.rb:9:in `block (3 levels) in <top (required)>'
• Title fix downcases articles
RSpec::Expectations::ExpectationNotMetError
expected: "The Lord of the Rings" got: ["The", "lord", "of", "the", "rings"] (compared using ==)
exercise_spec.rb:12:in `block (3 levels) in <top (required)>'
• Title fix works for strings with all uppercase characters
RSpec::Expectations::ExpectationNotMetError
expected: "The Sword and the Stone" got: ["THE", "SWORD", "AND", "THE", "STONE"] (compared using ==)
exercise_spec.rb:17:in `block (3 levels) in <top (required)>'
•RSpec::Expections::ExpectationNotMeter错误
预期:“伟大的盖茨比”得到:[“The”,“Great”,“Gatsby”](使用==进行比较)
练习_规范rb:6:in‘分块(3层)in’
•标题修复适用于大小写混合的单词
RSpec::Expections::ExpectationNotMetError
预期:“小红帽”得到:[“小”、“红”、“骑”、“兜帽”](使用==)
练习_规范rb:9:in‘分块(3层)in’
•标题固定案例文章
RSpec::Expections::ExpectationNotMetError
预期:“魔戒”获得:[“魔戒”、“魔戒”、“of”、“The”、“魔戒”](使用==进行比较)
练习_规范rb:12:in‘分块(3层)in’
•标题修复适用于所有大写字符的字符串
RSpec::Expections::ExpectationNotMetError
预期:“剑和石头”得到:[“剑”,“剑”,“和”,“石头](使用==)
练习_规范rb:17:在“块(3层)中”
这里有几个问题
element.upcase
,没有将element.upcase
分配给element
。为此,您需要说element=element.upcase
,或element.upcase代码>
upcase
用于将整个单词大写,您要使用capitalize
(或capitalize!
)循环的执行是正确的,只是我认为你写的根本不是你想要的。让我们读一本书:
@book
# => "a The b"
您应该使用以下简单方法将格式改为小写:
arr = @book.downcase.split(/\s+/)
# => ['a', 'the', 'b']
要生成新的修改数组,请使用#map
方法:
arr.map.with_index do |element, index|
art.include?(element) && element || element.capitalize
end.join(' ')
# => "A the B"
在代码中有些地方,您正在计算某些内容,但没有在变量中捕获它,因此该语句没有任何效果。一个是
@book.downcase.split(“”).map…
。另一种是ifart.include?(元素),然后是element.downcase
。如果艺术包含?(元素)
为真
,元素
不变。您需要element=element…
或downcase代码>。此外,修改正在迭代的块变量通常是不允许的。