Skip to content

Instantly share code, notes, and snippets.

@davidphasson
Created April 8, 2009 03:42
Show Gist options
  • Save davidphasson/91613 to your computer and use it in GitHub Desktop.
Save davidphasson/91613 to your computer and use it in GitHub Desktop.
ERB and the case statement
# 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>
@ZempTime
Copy link

ZempTime commented Nov 4, 2016

Eight years later, this is still relevant. Thanks!

@barrettclark
Copy link

Thank you 🚀

@luckymike
Copy link

LOL, just used this tip last week.

@hoffmanilya
Copy link

Thanks!

@y-chiasson
Copy link

wow

@adis-io
Copy link

adis-io commented Oct 4, 2017

Ruby 2.3.1 ERB works without this hack ;) Didn't tested on previous versions

@Victoriosu
Copy link

Thanks a lot!

@philihp
Copy link

philihp commented Jun 15, 2018

Thanks!

@ravicious
Copy link

ravicious commented Sep 17, 2018

@adis-io Hmm, I still had to use the workaround for Ruby 2.3.3:

$ rails c
Running via Spring preloader in process 90217
Loading development environment (Rails 5.1.2)
>> ERB.version
=> "erb.rb [2.1.0 2015-12-20]"

$ ruby -v
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-darwin17]

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.

@jeanmerlet
Copy link

jeanmerlet commented Jun 1, 2019

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

@klondikemarlen
Copy link

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]

@kennyfrc
Copy link

Oh man thanks for this

@sandstrom
Copy link

In case someone is curious, here is an issue on the ERB repo: ruby/erb#4

@richjdsmith
Copy link

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