Skip to content

Instantly share code, notes, and snippets.

@svoop
Created March 16, 2025 09:47
Show Gist options
  • Save svoop/dc5776fe04e325352815f73395ecbbbc to your computer and use it in GitHub Desktop.
Save svoop/dc5776fe04e325352815f73395ecbbbc to your computer and use it in GitHub Desktop.
Conditional assignment styles
# (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