Last active
September 5, 2016 21:36
-
-
Save izelnakri/9ed0d0c0f5fb632d731c to your computer and use it in GitHub Desktop.
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
| class FirstTry | |
| attr_accessor :data | |
| def default_content | |
| @data ||= begin | |
| puts "First function call" | |
| puts "second function call" | |
| 5 | |
| end | |
| end | |
| end | |
| class SecondTry | |
| attr_accessor :data | |
| def default_content | |
| @data ||= method_wrap | |
| end | |
| def method_wrap | |
| puts "First function call" | |
| puts "second function call" | |
| 5 | |
| end | |
| end | |
| class ThirdTry | |
| attr_accessor :data | |
| def default_content | |
| @data ||= lambda { | |
| puts "First function call" | |
| puts "second function call" | |
| 5 | |
| }.call() | |
| end | |
| end | |
| a = FirstTry.new | |
| b = SecondTry.new | |
| c = ThirdTry.new | |
| puts a.default_content | |
| puts a.default_content | |
| a.data = 'hi' | |
| puts a.default_content | |
| puts 'B gives the same behavior' | |
| puts b.default_content | |
| puts b.default_content | |
| b.data = 'hi' | |
| puts b.default_content | |
| puts 'C gives the same behavior' | |
| puts c.default_content | |
| puts c.default_content | |
| c.data = 'hi' | |
| puts c.default_content |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That's correct, and from those three, the first one wins in my opinion.