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

时间:2021-02-02 15:58:41

I am supposed to:

我应该:

  • Capitalize the first letter of string.
  • 大写字符串的第一个字母。
  • Capitalize every word except articles (the, a, an), conjunctions (and), and prepositions (in).
  • 大写除了文章(the,a,an),连词(和)和介词(in)之外的每个单词。
  • Capitalize i (as in "I am male.").
  • 资本化我(如“我是男性”)。
  • Specify the first word of a string (I actually have no idea what this means. I'm trying to run the spec file to test other functions).
  • 指定字符串的第一个单词(我实际上不知道这意味着什么。我正在尝试运行spec文件来测试其他函数)。

Here's my code:

这是我的代码:

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 says I'm messing up at:

Ruby说我搞砸了:

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

I get exactly the same error message with this test 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")

The only other Stack Overflow thread regarding this particular syntax error that did not suggest trailing or missing ends or .s as the root of the problem is here. The last comment recommends deleting and recreating the gemset, Which sounds scary. And I'm not sure how to do.

关于这个特定语法错误的唯一其他Stack Overflow线程没有建议尾随或缺少结尾或.s作为问题的根源在这里。最后一条评论建议删除并重新创建gemset,这听起来很吓人。我不知道该怎么做。

Any thoughts? Simple solution? Resources to help?

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

Solution

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 个解决方案

#1


2  

Write as

写为

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>

There are 2 errors I can see :

我可以看到2个错误:

  • None of the if has end keyword at end.
  • 没有if的结尾有关键字。
  • if arts_conjs_preps !include?(word) is not correct syntax.
  • 如果arts_conjs_preps!include?(word)语法不正确。

As per your last comment change some part of the code:

根据您的上一条评论更改代码的某些部分:

    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>

#2


1  

All if's should have matching end's. Yours don't have any.

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

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

Alternatively, you can use modifier notation (since that is what you were probably going for)

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

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

or

要么

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

Besides, you had invalid syntax in one of them.

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

if arts_conjs_preps !include?(word)

This is no valid syntax that I know of. (If you meant to check if an element belongs to the array, using ruby standard library. It is perfectly valid ruby code otherwise).

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

Update with working code

Works this way

这样工作

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

#3


0  

You are missing an end where you have written your if

你错过了写你if的结尾

#1


2  

Write as

写为

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>

There are 2 errors I can see :

我可以看到2个错误:

  • None of the if has end keyword at end.
  • 没有if的结尾有关键字。
  • if arts_conjs_preps !include?(word) is not correct syntax.
  • 如果arts_conjs_preps!include?(word)语法不正确。

As per your last comment change some part of the code:

根据您的上一条评论更改代码的某些部分:

    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>

#2


1  

All if's should have matching end's. Yours don't have any.

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

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

Alternatively, you can use modifier notation (since that is what you were probably going for)

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

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

or

要么

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

Besides, you had invalid syntax in one of them.

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

if arts_conjs_preps !include?(word)

This is no valid syntax that I know of. (If you meant to check if an element belongs to the array, using ruby standard library. It is perfectly valid ruby code otherwise).

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

Update with working code

Works this way

这样工作

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

#3


0  

You are missing an end where you have written your if

你错过了写你if的结尾