Skip to content

Instantly share code, notes, and snippets.

@ianhenrysmith
Created January 21, 2013 16:20
Show Gist options
  • Select an option

  • Save ianhenrysmith/4587161 to your computer and use it in GitHub Desktop.

Select an option

Save ianhenrysmith/4587161 to your computer and use it in GitHub Desktop.
Ruby refactoring
# from szaby (https://github.com/icebreaker)
# "Sometimes it's nice and cleaner to just use a block instead of having an explicit if statement
# when caching an operation that has multiple statements in a member variable."
# original
def client
if @client.blank?
@client = Client.new
@client.turn_on_x()
end
@client
end
# This can be rewritten as:
def client
@client ||= begin
c = Client.new
c.turn_on_x()
c
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment