Skip to content

Instantly share code, notes, and snippets.

@phuibonhoa
Created August 21, 2014 10:38
Show Gist options
  • Save phuibonhoa/243f956bdc76786f7db7 to your computer and use it in GitHub Desktop.
Save phuibonhoa/243f956bdc76786f7db7 to your computer and use it in GitHub Desktop.
Don't use self, unless it is required.
# Only use self when it is necessary.
# reader methods do not require self unless a local variable is declared with the same name (which is a bad idea anyway)
# setter methods do require self, otherwise you are creating a local variable
# Minimal use of self (only writes use 'self')
class Person
attr_accessor :name, :date_of_birth
def initialize(name:, date_of_birth:)
self.name = name
self.date_of_birth = date_of_birth
end
def greet
puts "Hi, my name is #{name}"
end
def age
Time.now.year - date_of_birth.year
end
end
# Unnessary use of self (read and writes both are prefaced with 'self')
class Person
attr_accessor :name, :date_of_birth
def initialize(name:, date_of_birth:)
self.name = name
self.date_of_birth = date_of_birth
end
def greet
puts "Hi, my name is #{self.name}"
end
def age
Time.now.year - self.date_of_birth.year
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment