5 Replies - 888 Views - Last Post: 08 March 2014 - 10:59 AM Rate Topic: -----

#1 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

self

Posted 06 March 2014 - 11:08 PM

Here is an example that implements a duck type. I am having trouble understanding the meaning of the self arg on line 6. I believe that self is refering to each object that is iterated through but I don't have a practical understanding of what is going on here. All I know is that each object has unique behavior that is provoked by the duck type method call.

class Trip
  attr_reader :bicycles, :customers, :vehicle
  
  def preparer(preparers) #an array of objects that respond to the prepare_trip method
    preparers.each { |preparer|
      preparer.prepare_trip(self) }
    end
  end


This post has been edited by heaphyg: 06 March 2014 - 11:12 PM


Is This A Good Question/Topic? 0
  • +

Replies To: self

#2 Sharparam  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 3
  • Joined: 16-June 13

Re: self

Posted 07 March 2014 - 03:20 AM

Correct me if I'm wrong but I believe the self here refers to the instance of the Trip class in which the preparer method is running. (That seems like an odd name for that method?)
Was This Post Helpful? 4
  • +
  • -

#3 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2088
  • View blog
  • Posts: 3,177
  • Joined: 21-June 11

Re: self

Posted 07 March 2014 - 06:35 AM

View Postheaphyg, on 07 March 2014 - 07:08 AM, said:

I believe that self is refering to each object that is iterated through


No, preparer is referring to each object you're iterating over (because that's the name between the pipes in the block).

View PostSharparam, on 07 March 2014 - 11:20 AM, said:

Correct me if I'm wrong but I believe the self here refers to the instance of the Trip class in which the preparer method is running.


You're right.

Quote

(That seems like an odd name for that method?)


Indeed, method names should usually be verbs (except in the case of getter methods). It's especially confusing that here a local variable inside the method has the same name as the method.
Was This Post Helpful? 3
  • +
  • -

#4 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: self

Posted 07 March 2014 - 09:59 AM

This example is from Practical Object Oriented Design in Ruby - I didn't make it. The reasoning behind the preparer method name is that it is a special method for objects that play the "preparer" role (objects that can respond to the prepare_trip method). the method is an abstraction.

So self always refers to the current scope...bit what does it mean to use self as an argument. I'm trying to understand what is being achieved with this argument. In general I don't know what it means to ever plug self in as an argument in any context.

This post has been edited by heaphyg: 07 March 2014 - 10:06 AM

Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2088
  • View blog
  • Posts: 3,177
  • Joined: 21-June 11

Re: self

Posted 07 March 2014 - 12:09 PM

It means the same thing as if you pass any other object. To illustrate this, take the following example:

trip = Trip.new
# ...
preparers = [...]
trip.prepare(preparers)



This is the same as:

trip = Trip.new
# ...
preparers = [...]

preparers.each { |preparer|
  preparer.prepare_trip(trip)
}



As you see I simply took the body of the method and replaced the word self with trip since, in this case, trip was the object on which we called the method.

PS: I just googled the book and it seems the method is actually called prepare (which makes much more sense since that's a verb and does not clash with the name of any local variables) and not preparer.
Was This Post Helpful? 1
  • +
  • -

#6 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: self

Posted 08 March 2014 - 10:59 AM

That cleared things up for me. Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1