The ruby gem BCrypt has an example of how to use one of it's modules. Taken from the docs:
def password
@password ||= Password.new(self.password_hash)
end
def password=(new_password)
@password = Password.create(new_password)
self.password_hash = @password
end
Why would we use the instance variable @password at all when we have the self.password_hash attribute? I feel like I am missing something, probably due to my inexperience with ruby. Personally, I would have accomplished what I believe to be the same with:
def password
self.password_hash ||= Password.new(self.password_hash)
end
def password=(new_password)
self.password_hash = Password.create(new_password)
end
@passwordand#password_hash. I would think your method would work, but there may be other side effects that require these two to be allowed to differ, sometimes. The only thing I can tell for sure is that@passwordis always a Password instance, but it appears#password_hashcan be, but does not appear as if it always would be. Is there an alterate way to call#password_hash=?