我不明白最终关键字的用法

teh*_*wer 0 ruby ruby-on-rails

我对关键字end感到非常困惑.例如,在下面的代码中,我试图定义一个我自己的帮助器方法,但是我得到了太多的SyntaxErrors,因为我错过了一些目的.我添加了一些它的工作但是...我不明白为什么我必须把它们,它阻止它们关闭.

我用多个问号标记了它们.

多谢你们!

 module ApplicationHelper

      def notice_color(notice)
        if notice
            type = type_notice(notice)
            div_tag_head = "<div class=\"alert alert-#{type} alert-dismissable\">"
            cross_button = "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>"
            notice_tag = div_tag_head + cross_button + notice + "</div>"
            notice_tag.to_s.html_safe
        end # If's end.
      end   # Def's end.

    def type_notice(notice)
        downcased = notice.downcase
        if downcased.include? 'error' or downcased.include? 'invalid'
          return'danger'
        else if downcased.include? 'done' or downcased.include? 'success'
          return 'success'
        else if downcased.include? 'hey'
          return 'warning'
        else 
          return 'info'
        end # If's end.
      end #Def's end
    end # ?????? <------ First

      private :type_notice
    end # ??????? <------ Second
    end # Module's end
Run Code Online (Sandbox Code Playgroud)

小智 7

你的问题就在if街区.Ruby语法elsif不是else if:

if downcased.include? 'error' or downcased.include? 'invalid'
  return'danger'
elsif downcased.include? 'done' or downcased.include? 'success'
  return 'success'
elsif downcased.include? 'hey'
  return 'warning'
else 
  return 'info'
end
Run Code Online (Sandbox Code Playgroud)

你的两else if行实际上是开始两个新的if陈述,因此你需要一些额外的ends.