I was writing some code in an erb template that looked like this:

<% if @user && not @user.errors.empty? -%>
#do stuff
<% end -%>

This resulted in a syntax error. Huh? What’s wrong with this?

Turns out it’s an outstanding Core Ruby bug.

As shown in the ticket if we have a method foo

def foo(parameter)
  puts parameter.to_s
end

using the ! and the not operator on various method calls show us that the ! and not operator are not interchangeable in practice.

foo(!(1 < 2)) # works fine
foo(not(1 < 2)) # generates syntax error
foo(not 1 < 2) # generates syntax error
foo((not 1 < 2)) # works fine

Changing the my code to the following fixed my issue:

<% if @user && !@user.errors.empty? -%>
#do stuff
<% end -%>
Posted in Uncategorized at March 29th, 2008. View Comments.