Skip to content

Instantly share code, notes, and snippets.

@johana-star
Created December 5, 2012 20:06
Show Gist options
  • Save johana-star/4219023 to your computer and use it in GitHub Desktop.
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
# 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
@johana-star
Copy link
Author

Also discussed on Stack Overflow.

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