Frederick Cheung wrote in post #975681: > On Jan 18, 12:16pm, Rushen Aly <[email protected]> wrote: >> Ok then, as an example what does the code mean below >> >> def encrypt_password >> self.encrypted_password = encrypt(self.password) >> end > > Well it's calling encrypted_password= with the result of encrypting > self.password. In this particular pattern password is normally a > virtual attribute, and the encrypted version of it is stored in the > encrypted_password column
I think it's also important to understand the distinction between "calling" and "messaging." In a procedural language such as C, functions are called directly and contain a list of arguments that are passed into the function. In Ruby (as an object-oriented programming language) "encryped_password=" represents a message that takes one argument. A "message" in OOP contains three basic parts. 1. A receiver, 2. The message, and 3. The argument list. Messages are generally back by "methods" which are very similar to functions, but are scoped (encapsulated) within the context of the class containing the method. Methods are not called directly, as in the case of functions, but rather are "called" indirectly by the runtime virtual machine, based on the combination of receiver and message. In the case above there are actually three separate messages, where all three are sent to "self" as the receiver. The three messages are "encrypted_password=", "encrypt", and "password". In the case of the "encrypt" message "self" is assumed, since a receiver is not specified. The first message sent to "self" is "password". The result of that message is then passed as an argument to the message "encrypt". Finally the result of the "encrypt" message is passed as the argument to "encrypted_password=". I hope that doesn't confuse you further, but I do think it is important to understand this basic OOP principal. Methods don't behave exactly like functions. No matter how many classes may implement a given method (like "add" for example) there is only one "add" message. The same "add" message can be sent to any object that implements an "add" method. This is called "polymorphism" in OOP terminology, because each receiver of "add" can provide different behavior to this same message. -- Posted via http://www.ruby-forum.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 [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

