0

我应该:

  • 将字符串的第一个字母大写。
  • the除冠词 ( , a, an)、连词 ( and) 和介词 ( ) 之外的每个单词都大写in
  • 大写i(如"I am male.")。
  • 指定字符串的第一个单词(我实际上不知道这意味着什么。我正在尝试运行规范文件来测试其他函数)。

这是我的代码:

class Book
  def initialize(string)
    title(string)
  end
  def title(string)
    arts_conjs_preps = %w{ a an the and
                           but or nor for
                           yet so although
                           because since
                           unless despite
                           in to
                           }
    array = string.downcase.split
    array.each do |word|
        if (word == array[0] || word == "i") then word = word.capitalize
        if arts_conjs_preps !include?(word) then word = word.capitalize
    end
    puts array.join(' ')
  end
end

puts Book.new("inferno")

Ruby 说我搞砸了:

puts Book.new("inferno") <--(right after the last line of code) 

我得到与此测试代码完全相同的错误消息:

def title(string)
    array = string.downcase.split
    array.each do |word|
        if word == array[0] then word = word.capitalize     
    end
  array.join(' ')
end

puts title("dante's inferno")

关于此特定语法错误的唯一其他 Stack Overflow 线程不建议将尾随或丢失ends 或.s 作为问题的根源。最后一条评论建议删除并重新创建 gemset,这听起来很可怕。而且我不知道该怎么做。

有什么想法吗?简单的解决方案?有帮助的资源?

解决方案

class Book
    def initialize(string)
        title(string)
    end

    def title(string)
    arts_conjs_preps = %w{ a an the and
                       but or nor for
                       yet so although
                       because since
                       unless despite
                       of in to
                               }
    array = string.downcase.split
    title = array.map do |word|
          if (word == array[0] || word == "i") || !arts_conjs_preps.include?(word)  
            word = word.capitalize 
          else
            word
          end
        end
      puts title.join(' ')
    end
end

Book.new("dante's the inferno")
4

3 回答 3

2

写为

class Book
  def initialize(string)
    title(string)
  end

  def title(string)
    arts_conjs_preps = %w{ a an the and
                           but or nor for
                           yet so although
                           because since
                           unless despite
                           in to
                           }
    array = string.downcase.split
    array.each do |word|
        word = word.capitalize if (word == array[0] || word == "i")
        word = word.capitalize if !arts_conjs_preps.include?(word)  
    end
    puts array.join(' ')
  end
end

puts Book.new("inferno")
# >> inferno
# >> #<Book:0x9704b7c>

我可以看到 2 个错误:

  • 没有一个ifhasend关键字在结尾。
  • if arts_conjs_preps !include?(word)不是正确的语法。

根据您的最后评论更改代码的某些部分:

    array = string.downcase.split
    array.map! do |word,ar|
        bol = !arts_conjs_preps.include?(word) || word == array[0] || word == "i"
        bol ? word.capitalize : word
    end
    puts array.join(' ')
  end
end

puts Book.new("inferno")
# >> Inferno
# >> #<Book:0x9e50bd8>
于 2013-10-24T09:08:57.707 回答
1

所有if的都应该有匹配end的。你的没有。

if (word == array[0] || word == "i") || !arts_conjs_preps.include?(word)
  word = word.capitalize
end

或者,您可以使用修饰符表示法(因为这可能是您想要的)

word = word.capitalize if (word == array[0] || word == "i")
word = word.capitalize if !arts_conjs_preps.include?(word)

或者

word.capitalize! if (word == array[0] || word == "i")
word.capitalize! unless arts_conjs_preps.include?(word)

此外,您在其中一个中有无效的语法。

if arts_conjs_preps !include?(word)

这不是我所知道的有效语法。(如果您要检查一个元素是否属于数组,请使用 ruby​​ 标准库。否则它是完全有效的 ruby​​ 代码)。

使用工作代码更新

以这种方式工作

array = array.map do |word|
    if (word == array[0] || word == "i") || !arts_conjs_preps.include?(word) 
      word.capitalize
    else
      word
    end
end
于 2013-10-24T09:07:31.293 回答
0

你错过了你写if的地方

于 2013-10-24T09:07:51.930 回答