Reputation: 1018
I'm getting pretty confused with the keyword end. For example, in the following code, I'm tried to define a my own helper method but I got too many SyntaxErrors because I missed some ends. I added some and it's works but... I don't understand why I have to put them, which block they close.
I marked them with multiple questions marks.
Thanks guys!
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\">×</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
Upvotes: 0
Views: 102
Reputation: 34
As Graeme stated, be careful to use elsif
instead of else if
.
However, indentation may also have played a role in the confusion. If you're using Sublime Text, check the Preferences.sublime-settings file and make sure there's a line that says "tab_size": 2
or "tab_size": n
(n being any number you're comfortable with, though 2 is de facto industry standard). Be careful to add a comma after the code if it's in the middle of the hash.
To access the preferences file, in Sublime Text, press Shift + Command + p (on an Apple) and type 'user,' or find the Preferences menu option and select 'Settings - User.'
Upvotes: 0
Reputation:
Your problem is in the if
block. Ruby syntax is elsif
not 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
Your two else if
lines are actually starting two new if
statements, hence why you needed a couple of extra end
s.
Upvotes: 7