Skip to content

Instantly share code, notes, and snippets.

@jsanders
Created April 2, 2012 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsanders/2284806 to your computer and use it in GitHub Desktop.
Save jsanders/2284806 to your computer and use it in GitHub Desktop.
Prettier (?) elsif
puts case true
when true == false
"nope"
when false == true
"also nope"
when [ :a ].empty?
"nope as well"
when nil.nil?
"yep!"
when false.nil?
"nopers"
else
"uh-uh"
end
@jsanders
Copy link
Author

jsanders commented Apr 2, 2012

Came across this Go idiom that I had never actually used in Ruby, but seemed like it would work just as well. Has everyone been using this already and I just missed the boat?

@cschneid
Copy link

cschneid commented Apr 2, 2012

So it does true === (true == false)?

@jsanders
Copy link
Author

jsanders commented Apr 2, 2012

Yeah, which raises an interesting point - where if/elsif tests the truthiness of its parameter, this actually tests against the true singleton versus anything else. So don't use it with .xhr? (rails/rails#5329) :)

@jsanders
Copy link
Author

jsanders commented Apr 2, 2012

Honestly I think the reason it isn't an idiom in ruby is that elsif strings read just as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment