Last active
June 10, 2020 09:44
-
-
Save abstractart/1f28d7c09bc84dd7779f3d0b4743ca9e to your computer and use it in GitHub Desktop.
Constructor with block in Ruby
This file contains 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 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