Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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>
@moeabdol

This comment has been minimized.

Copy link

commented Apr 16, 2015

Thanks...this solved my problem...weird though!

@passalini

This comment has been minimized.

Copy link

commented Jan 12, 2016

Thanks!

@afirth

This comment has been minimized.

Copy link

commented Mar 23, 2016

I hope I never have to look at this again. Thanks for saving me a few headbangs.

@pabelnl

This comment has been minimized.

Copy link

commented Sep 1, 2016

Thanks man, really helped me

@ZempTime

This comment has been minimized.

Copy link

commented Nov 4, 2016

Eight years later, this is still relevant. Thanks!

@barrettclark

This comment has been minimized.

Copy link

commented Nov 14, 2016

Thank you 🚀

@luckymike

This comment has been minimized.

Copy link

commented Nov 21, 2016

LOL, just used this tip last week.

@hoffmanilya

This comment has been minimized.

Copy link

commented Mar 3, 2017

Thanks!

@y-chiasson

This comment has been minimized.

Copy link

commented Aug 16, 2017

wow

@adis-io

This comment has been minimized.

Copy link

commented Oct 4, 2017

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

@Victoriosu

This comment has been minimized.

Copy link

commented Nov 20, 2017

Thanks a lot!

@philihp

This comment has been minimized.

Copy link

commented Jun 15, 2018

Thanks!

@ravicious

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.