Created
March 16, 2025 09:47
-
-
Save svoop/dc5776fe04e325352815f73395ecbbbc to your computer and use it in GitHub Desktop.
Conditional assignment styles
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
# (1) if with assignment wide indent (original) | |
destination = if to.is_a?(URI) | |
to.to_s | |
elsif to.start_with?("http://") || to.start_with?("https://") | |
to | |
else | |
prefixed_path(to) | |
end | |
# (2) case with assignment wide indent | |
destination = case to | |
when URI | |
to.to_s | |
when %r{\Ahttps?://} | |
to | |
else | |
prefixed_path(to) | |
end | |
# (3) case/then with assignment wide indent | |
destination = case to | |
when URI then to.to_s | |
when %r{\Ahttps?://} then to | |
else prefixed_path(to) | |
end | |
# (4) if with standard indent | |
destination = if to.is_a?(URI) | |
to.to_s | |
elsif to.start_with?("http://") || to.start_with?("https://") | |
to | |
else | |
prefixed_path(to) | |
end | |
# (5) case with standard indent | |
destination = case to | |
when URI | |
to.to_s | |
when %r{\Ahttps?://} | |
to | |
else | |
prefixed_path(to) | |
end | |
# (6) case/then with standard indent | |
destination = case to | |
when URI then to.to_s | |
when %r{\Ahttps?://} then to | |
else prefixed_path(to) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment