Created
December 5, 2012 20:06
-
-
Save johana-star/4219023 to your computer and use it in GitHub Desktop.
A gist demoing four valid ways to write do…while functionality in Ruby
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
# Which is the most stylistically correct way to emulate a do…while in Ruby? | |
# option one | |
begin | |
mark = gets.chomp | |
end unless mark == "A2" # loops again when the mark is A2. | |
# option two | |
loop do | |
mark = gets.chomp | |
break unless mark == "A2" | |
end | |
# option three | |
1.times do | |
mark = gets.chomp | |
retry if mark == "A2" | |
end | |
# option four – suggested by @radar | |
while true | |
mark = gets.chomp | |
break unless mark == "A2" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also discussed on Stack Overflow.