Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Struct with optional attributes

struct Person
  property name : String
  property email : String?

  def initialize(@name)
  end

  def initialize(@name, @email)
  end
end

prs = Person.new("Foo")
p! prs
p! prs.name
p! prs.email

prs.email = "foo@bar.com"
p! prs
p! prs.email
prs # => Person(@name="Foo", @email=nil)
prs.name # => "Foo"
prs.email # => nil
prs # => Person(@name="Foo", @email="foo@bar.com")
prs.email # => "foo@bar.com"