Skip to content

Instantly share code, notes, and snippets.

@soulcutter
Created March 9, 2018 14:37
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 soulcutter/cd01376def3bca33f08b99d2297bf984 to your computer and use it in GitHub Desktop.
Save soulcutter/cd01376def3bca33f08b99d2297bf984 to your computer and use it in GitHub Desktop.
Tap vs Local Variables
def level_descriptions=(input)
write_attribute(:levels_description, input).tap do |_result|
flush_cache :levels_description
end
end
# same number of lines, one less method call, one less block, but pretty much the same as
# far as I'm concerned.
def level_descriptions=(input)
result = write_attribute(:levels_description, input)
flush_cache :levels_description
result
end
# effectively the same behavior barring exceptions, has no variables
# probably harder for folks to read/understand, I would prefer either the two above
def level_descriptions=(input)
write_attribute(:levels_description, input) ensure flush_cache :levels_description
end
# bottom line, I see no compelling reason to choose tap over a local variable or vice-versa.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment