Created
January 21, 2013 16:20
-
-
Save ianhenrysmith/4587161 to your computer and use it in GitHub Desktop.
Ruby refactoring
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
| # 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