SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Strange OOP explanation at the beggining of the book...

    I have finished the book already but re-lokking at it i noticed that when the author explains at the beginning of the book the concepts of OOP he says that in OOP objects can interact with each other for example when an object (called the sender) sends a message (calls a method on) to another object. I don't really grasp this concept and it semms to me very strange as there isn't such a thing in OOP or rubu or rails i think. When we invoke methos we use the notation: "object.instance_method" directly in irb or in a ruby source file. Where and how we see an object that call another object's method?

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Message receiver is an object whose method we're calling and message sender is an object whose method performs the call.

    Code Ruby:
    class Bob
    	def send_message_to(other)
    		other.say_hello
    	end
    end
     
    class Alice
    	def say_hello
    		puts "hello"
    	end
    end
     
     
     
    bob = Bob.new
    alice = Alice.new
     
    bob.send_message_to(alice)
    Here bob is a sender and alice is a receiver of the message "say_hello".
    Hope this helps.

  3. #3
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog View Post
    Message receiver is an object whose method we're calling and message sender is an object whose method performs the call.

    Code Ruby:
    class Bob
    	def send_message_to(other)
    		other.say_hello
    	end
    end
     
    class Alice
    	def say_hello
    		puts "hello"
    	end
    end
     
     
     
    bob = Bob.new
    alice = Alice.new
     
    bob.send_message_to(alice)
    Here bob is a sender and alice is a receiver of the message "say_hello".
    Hope this helps.
    The example is ok but i think that design is less used in rails. In the book it explains it and then it never reuses it as a pratical example in the rails book application. Everytime we call a method on a object we do it directly.

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I feel my example was a bit confusing... let's try another one (from rails scaffolding code):


    Code ruby:
    class AdminController < ApplicationController
      def show
        @post = Post.find(params[:id])
      end
    end
    in "show": AdminController is a sender, Post is a receiver and "find" is a message.

  5. #5
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog View Post
    Ok, I feel my example was a bit confusing... let's try another one (from rails scaffolding code):


    Code ruby:
    class AdminController < ApplicationController
      def show
        @post = Post.find(params[:id])
      end
    end
    in "show": AdminController is a sender, Post is a receiver and "find" is a message.
    Can you explain in deatl that? Aren't both of they classes instead of objects?
    You mean that when a page request is made by the browser rails instantiate the class AdminController and then call on that instance its instance method "show" as: "admin_controller_object.show"?

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In pure OOP languages like ruby the boundary between classes and objects is rather blurred. 'Class' is just a special kind of 'object'.

    When dispatching the request, rails instantiates a new object of class 'AdminController' and sends the message 'show' to it (= calls the method 'show' declared in the class 'AdminController'). This object (of class 'AdminController') sends the message 'find' to the class 'Post'. That message is sent directly to the class, not to any instance of it.

  7. #7
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This:

    Code Ruby:
    other.say_hello

    is the same thing as this:

    Code Ruby:
    other.send(:say_hello)

    So when you do some_object.some_method() you are sending the some_method message to some_object.

    Code Ruby:
    class Example
      def test
        3 + 4 # 3.+(4) == 3.send(:+, 4)
      end
    end
     
    e  = Example.new  # Example.send(:new)
     
    # the e object sends the + message to the 3 object:
    e.test # e.send(:test)

  8. #8
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    This:

    Code Ruby:
    other.say_hello

    is the same thing as this:

    Code Ruby:
    other.send(:say_hello)

    So when you do some_object.some_method() you are sending the some_method message to some_object.

    Code Ruby:
    class Example
      def test
        3 + 4 # 3.+(4) == 3.send(:+, 4)
      end
    end
     
    e  = Example.new  # Example.send(:new)
     
    # the e object sends the + message to the 3 object:
    e.test # e.send(:test)
    So in your last example first i send directly the message "test" to the "e" object variable, and then this object send the message "+" to the 3 object inside the instance method is it right the concept?
    So in the end when we simply have "Object.object_method" WE are sending directly the message to the object in the source code or in irb right?

  9. #9
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, yes, but we are objects too ;-)

    Code Ruby:
    Array.new

    Here you send the "new" message to Array. Array is a class object (i.e. the class of Array is Class). The new method isn't a keyword like in other languages. This new method first allocates memory for the object is creates, and then it calls the_new_object.initialize().

  10. #10
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so at the end there are many (even though a little bit hard to grasp at first) cases in which an object is the responsible to the call of another object's method and the rest is when WE are calling (send a message) directly an object's method right?

  11. #11
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes.

  12. #12
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    one last doubt. "self" refer to the current class or object? For example in the class method definition "self.some_class_method_name" "self" refers to the current class but in a call to a method inside an action in a controller for example where we omit the receiver we are calling that method on the implicit self object right?

  13. #13
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes. Self always is the object before the dot:

    object.method()

    Object is self inside method().

    So in YourController.class_method() => YourController is the self.

    YourController.new().instance_method() => YourController is self in new(), and the return-value of new is self in instance_method().

  14. #14
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    Yes. Self always is the object before the dot:

    object.method()

    Object is self inside method().

    So in YourController.class_method() => YourController is the self.

    YourController.new().instance_method() => YourController is self in new(), and the return-value of new is self in instance_method().
    I forgot the fact that classes are also objects (of class Class)
    Perfectly clear thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •