Tuesday, January 19, 2010

[Rails] Re: The role of self and :: within a method of a model

On Jan 19, 5:35 pm, JohnMerlino <stoici...@aol.com> 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
>
>
>
>
> Frederick Cheung-2 wrote:
>
> > On Jan 18, 8:40 pm, JohnMerlino <stoici...@aol.com> 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
>
> >>     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-...
> >> Sent from the RubyOnRails Users mailing list archive at Nabble.com.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Ruby on Rails: Talk" group.
> > To post to this group, send email to rubyonrails-talk@googlegroups.com.
> > To unsubscribe from this group, send email to
> > rubyonrails-talk+unsubscribe@googlegroups.com.
> > For more options, visit this group at
> >http://groups.google.com/group/rubyonrails-talk?hl=en.
>
> --
> View this message in context:http://old.nabble.com/The-role-of-self-and-%3A%3A-within-a-method-of-...
> Sent from the RubyOnRails Users mailing list archive at Nabble.com.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate