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

**URL:** <https://www.ruby-forum.com/t/the-role-of-self-and-within-a-method-of-a-model/181818>\
**Category:** Rails\
**Created:** [January 18, 2010, 9:42pm UTC](https://www.ruby-forum.com/t/the-role-of-self-and-within-a-method-of-a-model/181818 "2010-01-18T21:42:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 18, 2010, 9:42pm UTC](https://www.ruby-forum.com/t/the-role-of-self-and-within-a-method-of-a-model/181818/1 "2010-01-18T21:42:26Z")

</div>

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](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](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 18, 2010, 11:14pm UTC](https://www.ruby-forum.com/t/the-role-of-self-and-within-a-method-of-a-model/181818/2 "2010-01-18T23:14:22Z")

</div>

On Jan 18, 8:40Â pm, JohnMerlino [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 19, 2010, 6:38pm UTC](https://www.ruby-forum.com/t/the-role-of-self-and-within-a-method-of-a-model/181818/3 "2010-01-19T18:38:39Z")

</div>

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-](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](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](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 19, 2010, 6:43pm UTC](https://www.ruby-forum.com/t/the-role-of-self-and-within-a-method-of-a-model/181818/4 "2010-01-19T18:43:21Z")

</div>

On Jan 19, 5:35Â pm, JohnMerlino [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
