The following is an example for the LSP:
require 'date'
class User
attr_accessor :settings, :email
def initialize(email:)
@email = email
end
end
class AdminUser < User
end
user = User.new(email: "[email protected]")
user.settings = {
level: "Low Security",
status: "Live",
signed_in: Date.today
}
admin = AdminUser.new(email: "[email protected]")
admin.settings = ["Editor", "VIP", Date.today]
puts user.settings
puts admin.settings
For our code walk-through, I created a basic User class in Ruby. Additionally, I built an AdminUser class that inherits from the parent User class. The classes have attributes for settings and an email.
In our case study, we decided to make an interesting decision to use the hash data type for our settings in the User class. However, we're using the array data type for our AdminUser class settings. This seems like an innocuous issue because when we run this program, both classes seem to be working fine:.
{:level=>"Low Security", :status=>"Live", :signed_in=>#<Date: 2016-09-23 ((2457655j,0s,0n),+0s,2299161j)>}
Editor
VIP
2016-09-23