Skip to content

Instantly share code, notes, and snippets.

@rgo
Created July 1, 2011 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgo/1058634 to your computer and use it in GitHub Desktop.
Save rgo/1058634 to your computer and use it in GitHub Desktop.
Using &block as constructor
# &block constructors
# From http://www.deploymentzone.com/2011/06/23/block-constructors
# If you need a behavior like this in your class:
Person.new do |p|
p.first_name = "John"
p.last_name = "Brown"
p.dob = Date.parse("5/9/1800")
end
# You can achieve it with this snippet:
class Person
attr_accessor :first_name, :last_name, :dob
def initialize
yield self if block_given?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment