Forum: Ruby ruby modules and classes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D449d54c3b0f8c9930c11c7d7d3e6cdd?d=identicon&s=25 Surendra Singhi (Guest)
on 2006-03-01 15:58
(Received via mailing list)
Hello,

When I try the code below I get an error. Why is it so? Am I
misunderstanding
how modules behave? How should the module be written so that `who_am_i'
is
added as a method to the class Phonograph.

module Debug
  def self.who_am_i?
    "#{self.class.name} (\##{self.id}): #{self.to_s}"
  end
end
class Phonograph
  include Debug
end
Phonograph.who_am_i?

Thanks.

--
Surendra Singhi
http://ssinghi.kreeti.com, http://www.kreeti.com
Read the latest news at: http://news.kreeti.com
,----
| "O thou my friend! The prosperity of Crime is like unto the lightning,
| whose traitorous brilliancies embellish the atmosphere but for an
| instant, in order to hurl into death's very depths the luckless one
| they have dazzled." -- Marquis de Sade
`----
A9b6a93b860020caf9d2d1d58c32478f?d=identicon&s=25 Ross Bamford (Guest)
on 2006-03-01 16:12
(Received via mailing list)
On Wed, 2006-03-01 at 23:57 +0900, Surendra Singhi wrote:
> end
> class Phonograph
>   include Debug
> end
> Phonograph.who_am_i?

Depends what you want, but how about:

	module Debug
	  def who_am_i?
	    "#{self.class.name} (\##{self.object_id}): #{self.to_s}"
	  end
	end

	class Phonograph
	  class << self
	    include Debug
	  end
	end

	p Phonograph.who_am_i?

Or, if you want it as both class and instance methods, I like this
trick:

	module Debug
	  def who_am_i?
	    "#{self.class.name} (\##{self.object_id}): #{self.to_s}"
	  end

	  def self.included(othermod)
	    othermod.extend(self)
	  end
	end

	class Phonograph
	  include Debug
	end

	p Phonograph.who_am_i?
	p Phonograph.new.who_am_i?

(P.s. do you know about Object#inspect?)
D449d54c3b0f8c9930c11c7d7d3e6cdd?d=identicon&s=25 Surendra Singhi (Guest)
on 2006-03-02 14:16
(Received via mailing list)
Hello Ross,

Ross Bamford <rossrt@roscopeco.co.uk> writes:

>>   end
>> end
>> class Phonograph
>>   include Debug
>> end
>> Phonograph.who_am_i?
>
> Depends what you want, but how about:
>
Thanks for your reply.

> 	end
>
> 	p Phonograph.who_am_i?
>

In the meantime I found another way to do this:

class Phonograph
  extend Debug
end

are the two methods equivalent, or is there any difference?

> 	  end
> 	end
>
> 	class Phonograph
> 	  include Debug
> 	end
>
> 	p Phonograph.who_am_i?
> 	p Phonograph.new.who_am_i?

>
> (P.s. do you know about Object#inspect?)
Now I do.

Thanks once again, I appreciate your help.

--
Surendra Singhi
http://ssinghi.kreeti.com, http://www.kreeti.com
Read the latest news at: http://news.kreeti.com
,----
| Great wits are sure to madness near allied,
| And thin partitions do their bounds divide.
|
|     (John Dryden, Absalom and Achitophel, 1681)
`----
This topic is locked and can not be replied to.