The role of self and :: within a method of a model

Hey all,
I understand that a def self.abc, for example, is a class method, which
allows you to call its contents by just referencing model.abc (rather
than
model.new.abc). However, what is its role when located in a method
inside a
model like in the code below.

Also in the code below, you see :: located in the method. Isn’t that
used
for modules and namespaces? If so, why is it located in the method here.
These two things are preventing me from comprehending the below code:

def hash_new_password
  # First reset the salt to a new random string.  You could choose a
  # longer string here but for a salt, 8 bytes of randomness is 

probably
# fine. Note this uses SecureRandom which will use your
platform’s
secure
# random number generator.
self.salt = ActiveSupport::SecureRandom.base64(8)
# Now calculate the hash of the password, with the salt prepended,
store
# store that in the database
self.hashed_password = Digest::SHA2.hexdigest(self.salt +
@new_password)
end
end

Thanks for any suggestions.

View this message in context:
http://old.nabble.com/The-role-of-self-and-%3A%3A-within-a-method-of-a-model-tp27216408p27216408.html
Sent from the RubyOnRails Users mailing list archive at Nabble.com.

On Jan 18, 8:40Â pm, JohnMerlino [email protected] wrote:

Hey all,
I understand that a def self.abc, for example, is a class method, which
allows you to call its contents by just referencing model.abc (rather than
model.new.abc). However, what is its role when located in a method inside a
model like in the code below.

If you call a method without specifying the receiver (ie if you just
write foo()) then you are implicitly sending that call to the current
object, ie self. Writing self.foo just makes that explicit. Just about
the only time it is actually necessary is when calling a setter
function: if you write

salt = …

ruby thinks you are trying to set a local variable called salt rather
than call the current object’s salt= method

Also in the code below, you see :: located in the method. Isn’t that used
for modules and namespaces? If so, why is it located in the method here.
These two things are preventing me from comprehending the below code:

:: is the scope operator. SecureRandom is nested inside ActiveSupport
so normally you need to write ActiveSupport::SecureRandom (unless your
lexical scope includes ActiveSupport or you’ve included it etc
(constant resolution is a little gnarly in ruby))

Fred

salt is a method? I thought it was an attribute of the model

Also, are you saying that SHA2 is a constant and Diges is a utility
class of
ActiveSupport, and because SHA2 was defined within the class and because
we
are referencing it outside of the class, it must be defined with the
scope
operator ::?

Digest::SHA2

Frederick C.-2 wrote:

model like in the code below.
ruby thinks you are trying to set a local variable called salt rather

  # Now calculate the hash of the password, with the salt prepended,

context:http://old.nabble.com/The-role-of-self-and-%3A%3A-within-a-method-of-


View this message in context:
http://old.nabble.com/The-role-of-self-and-%3A%3A-within-a-method-of-a-model-tp27216408p27229547.html
Sent from the RubyOnRails Users mailing list archive at Nabble.com.

On Jan 19, 5:35Â pm, JohnMerlino [email protected] wrote:

salt is a method? I thought it was an attribute of the model

it’s both - there are a pair of methods (a getter and a setter) for
each of your attributes.

Also, are you saying that SHA2 is a constant and Diges is a utility class of
ActiveSupport, and because SHA2 was defined within the class and because we
are referencing it outside of the class, it must be defined with the scope
operator ::?

Digest::SHA2

There is no relationship between ActiveSupport and Digest - Digest is
a top level constant.
SHA2 is defined inside Digest so yes, normally you need to write
Digest::SHA2. If you first did

include Digest

Then you could just write SHA2, but I wouldn’t bother with that.

Fred