So this question came up while learning rails but its more of a general
ruby question I think.
The tutorial I am working through defines a class:
class User < ActiveRecord::Base
.
.
private
def encrypt_password
self.encrypted_password = encrypt(password)
end
.
.
end
and said it used self.encrypted_password so that the function wouldn’t
create a local variable. That makes since to me what doesn’t is that it
goes on to say the it could have written encrypt(self.password) but it
wasn’t required. How does
ruby know where to find the latter but not the first?
Another question this chapter brought up is what is the difference and
when do I use self.somevar and @somevar. In regular ruby I always just
used @var to set declare and use instance variables and never really
needed self but rails seems to use it a lot. I tried to play around with
it in the console and variables I made with @ couldn’t be seen by self.
Thank you