Skip to content

Instantly share code, notes, and snippets.

@izelnakri
Last active September 5, 2016 21:36
Show Gist options
  • Select an option

  • Save izelnakri/9ed0d0c0f5fb632d731c to your computer and use it in GitHub Desktop.

Select an option

Save izelnakri/9ed0d0c0f5fb632d731c to your computer and use it in GitHub Desktop.
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
@daxhuiberts
Copy link
Copy Markdown

That's correct, and from those three, the first one wins in my opinion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment