Ruby语法错误:意外$ end,期待keyword_end

时间:2013-10-24 09:05:30

标签: ruby ruby-1.9.3

我应该:

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

这是我的代码:

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线程未建议将end.作为问题的根目录尾随或缺失here。最后一条评论建议删除并重新创建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")

3 个答案:

答案 0 :(得分: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个错误:

  • if最后都没有end个关键字。
  • 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>

答案 1 :(得分: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

答案 2 :(得分:0)

您错过了结束,其中您写了如果