Created
August 21, 2014 10:38
-
-
Save phuibonhoa/243f956bdc76786f7db7 to your computer and use it in GitHub Desktop.
Don't use self, unless it is required.
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
# 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