Created
October 12, 2012 00:23
-
-
Save kunalbhatt/3876601 to your computer and use it in GitHub Desktop.
Object Privacy
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
# 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