Created
April 8, 2009 03:42
-
-
Save davidphasson/91613 to your computer and use it in GitHub Desktop.
ERB and the case statement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Doesn't work | |
<p> | |
<% case @request.author_hosted %> | |
<% when "yes" %> | |
The school <b>has</b> hosted an author before. | |
<% when "no" %> | |
The school <b>has not</b> hosted an author before. | |
<% end %> | |
</p> | |
# Does | |
<p> | |
<% case @request.author_hosted | |
when "yes" %> | |
The school <b>has</b> hosted an author before. | |
<% when "no" %> | |
The school <b>has not</b> hosted an author before. | |
<% end %> | |
</p> |
I believe this StackOverflow post explains the need for the above workaround: https://stackoverflow.com/questions/5282585/syntaxerror-using-case-expression-on-sinatra-1-2-0-and-ruby-1-9-2
Edit: Thus <% case @request.author_hosted; when "yes %> should also work
Still relevant 14 years later.
$ rails c
DEBUGGER[spring app | platform | started 1 hour ago | development mode#240953]: Attaching after process 191655 fork to child process 240953
Running via Spring preloader in process 240953
Loading development environment (Rails 7.0.3)
irb(main):001:0> ERB.version
=> "2.2.3"
irb(main):002:0> exit
$ ruby -v
ruby 3.1.1p18 (2022-02-18 revision 53f5fc4236) [x86_64-linux]
Oh man thanks for this
In case someone is curious, here is an issue on the ERB repo: ruby/erb#4
Found that adding a line break also works:
Doesn't work
<p>
<% case @request.author_hosted %>
<% when "yes" %>
The school <b>has</b> hosted an author before.
<% when "no" %>
The school <b>has not</b> hosted an author before.
<% end %>
</p>
Does
<p>
<% case @request.author_hosted
when "yes" %>
The school <b>has</b> hosted an author before.
<% when "no" %>
The school <b>has not</b> hosted an author before.
<% end %>
</p>
Also Does
<p>
<% case @request.author_hosted -%>
<% when "yes" %>
The school <b>has</b> hosted an author before.
<% when "no" %>
The school <b>has not</b> hosted an author before.
<% end %>
</p>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@adis-io Hmm, I still had to use the workaround for Ruby 2.3.3:
Edit: Turns out the vim plugin which runs linters on files must have been using the old (system) version of Ruby, as it indeed works without the hack.