Skip to content

Instantly share code, notes, and snippets.

@kunalbhatt
Created October 12, 2012 00:23
Show Gist options
  • Save kunalbhatt/3876601 to your computer and use it in GitHub Desktop.
Save kunalbhatt/3876601 to your computer and use it in GitHub Desktop.
Object Privacy
# Def initialize
class BankAccount
attr_reader :customer_name, :type, :acct_number
def initialize(customer_name, type, acct_number)
@customer_name = customer_name
@type = type
@acct_number = acct_number
end
end
# Define getter methods
class BankAccount
def initialize(customer_name, type, acct_number)
@customer_name = customer_name
@type = type
@acct_number = acct_number
end
def customer_name
@customer_name
end
def type
@type
end
def acct_number
@acct_number.gsub(/\d{3}-\d{2}/, "XXX-XX")
end
end
# Define setter methods
class BankAccount
attr_reader :customer_name, :type
def initialize(customer_name, type, acct_number)
@customer_name = customer_name
@type = type
@acct_number = acct_number
end
def customer_name=(customer_name)
@customer_name = customer_name
end
def type=(type)
@type = type
end
def acct_number
@acct_number.gsub(/\d{3}-\d{2}/, "XXX-XX")
end
end
# Hide the account
class BankAccount
attr_reader :customer_name, :type
def initialize(customer_name, type, acct_number)
@customer_name = customer_name
@type = type
@acct_number = acct_number
end
def customer_name=(customer_name)
@customer_name = customer_name
end
def type=(type)
@type = type
end
def acct_number
@acct_number.gsub(/\d{3}-\d{2}/, "XXX-XX")
end
end
# Redefine to_s
class BankAccount
attr_reader :customer_name, :type
def initialize(customer_name, type, acct_number)
@customer_name = customer_name
@type = type
@acct_number = acct_number
end
def customer_name=(customer_name)
@customer_name = customer_name
end
def type=(type)
@type = type
end
def acct_number
@acct_number.gsub!(/\d{3}-\d{2}/, "XXX-XX")
end
def to_s
puts "My account information is: #{@customer_name}, #{@type}, #{acct_number}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment