Skip to content

Instantly share code, notes, and snippets.

@nocash
Last active February 18, 2016 20:55
Show Gist options
  • Select an option

  • Save nocash/4d5d3d596476d4c84992 to your computer and use it in GitHub Desktop.

Select an option

Save nocash/4d5d3d596476d4c84992 to your computer and use it in GitHub Desktop.
class Basket
def initialize
# use private setter to initialize variable
set_contents(:empty)
end
def fill_basket
set_contents(:fruit)
end
private
def set_contents(contents)
@contents = contents
end
end
class Basket
def initialize
# initialize variable directly
@contents = :empty
end
def fill_basket
set_contents(:fruit)
end
private
def set_contents(contents)
@contents = contents
end
end
# don't bother with setter for simple variables
class Basket
def initialize
@contents = :empty
end
def fill_basket
@contents = :fruit
end
end
@smdern
Copy link
Copy Markdown

smdern commented Feb 18, 2016

class Basket
  def initialize
    contents = :empty
  end

  def fill_basket
    contents = :fruit
  end

  private
  attr_accessor :contents
end

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