Skip to content

Instantly share code, notes, and snippets.

@abstractart
Last active June 10, 2020 09:44
Show Gist options
  • Save abstractart/1f28d7c09bc84dd7779f3d0b4743ca9e to your computer and use it in GitHub Desktop.
Save abstractart/1f28d7c09bc84dd7779f3d0b4743ca9e to your computer and use it in GitHub Desktop.
Constructor with block in Ruby
class A
attr_accessor :attribute
def initialize
yield self
end
end
a = A.new {|a| a.attribute = "block" }.tap {|a| puts a.attribute }
a.attribute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment