Forum: Ruby Module methods

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.
953d32e245f7eb0e670bde0577f1eb43?d=identicon&s=25 Pito Salas (pitosalas)
on 2009-06-03 15:16
I am trying to do this:

==== File: good.rb ====

module Good
   def init_module
      @var = "good"
   end

   def life_is_good
      puts "life is #{@var}"
   end

   [... more methods ...]
end

include X
init_module


======== and in a different file ========

include Good

life_is_good

- - - - - - - -

That works but messes up when I try to use ruby-debug with it. So I
changed it to try to call init_module in a different way: instead of
include X, init_module, I tried both of these, neither of which works:

X::init_module

X.init_module

Each of those calls lead to an 'undefined method 'init_module' for
X:Module.

I am getting myself confused with Module methods etc etc.

Any help would be gratefully accepted!!

Pito
134ea397777886d6f0aa992672a50eaa?d=identicon&s=25 Mark Thomas (Guest)
on 2009-06-03 15:32
(Received via mailing list)
It looks like what you really want is a class. Is there a specific
reason you don't want to do this?

class Good
  def initialize
    @var = "good"
  end

  def life
    puts @var
  end
end

g = Good.new
g.life
953d32e245f7eb0e670bde0577f1eb43?d=identicon&s=25 Pito Salas (pitosalas)
on 2009-06-03 15:53
Mark Thomas wrote:
> It looks like what you really want is a class. Is there a specific
> reason you don't want to do this?
>
> class Good
>   def initialize
>     @var = "good"
>   end
>
>   def life
>     puts @var
>   end
> end
>
> g = Good.new
> g.life

Yeah, I am trying to make as transparent as possible "Domain Specific
Language" so that the second file looks like a program in the DSL with
as little extra stuff as possible. So the second file ends up looking
like:

include Good

life
bicycle :first
telephone :last
life


(FOR EXAMPLE :)
48aef6c74ff21bfe894b201c7936fcdd?d=identicon&s=25 Nation, Carey (Guest)
on 2009-06-03 17:22
(Received via mailing list)
Pretty sure that you need to prefix the module methods with the module
name.  Yeah, I know.

Try:
module Good
   def Good.init_module
      @var = "good"
   end
....

And the in your "different file"

require 'good';
Good.init_module

Which isn't exactly what you want.

<talking out of ear now>
I think that you have to prefix the method with the module name since
using it this way gets things added to main rather than to your own
class/instance.  It smells like include should make it so that you can
omit the Good. on the call, but it doesn't work for me.  Again, I think
it may be something to do with it being in main rather than in your own
class.  You're basically trying to make a mixin for main and I
suspect/know that the rules are somewhat different.
</talking out of ear now>

Hope that this helps some...
134ea397777886d6f0aa992672a50eaa?d=identicon&s=25 Mark Thomas (Guest)
on 2009-06-03 21:01
(Received via mailing list)
On Jun 3, 9:54 am, Pito Salas <r...@salas.com> wrote:
> life
I think part 4 of this article is worth reading:
http://deadprogrammersociety.blogspot.com/2006/11/...
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2009-06-03 22:54
(Received via mailing list)
On Wed, Jun 3, 2009 at 3:00 PM, Mark Thomas <mark@thomaszone.com> wrote:

> I think part 4 of this article is worth reading:
> 
http://deadprogrammersociety.blogspot.com/2006/11/...

Also, the sample chapter of my book has a relevant section about this,
under "Building Flexible Interfaces":

http://oreilly.com/catalog/9780596156749/index.html
953d32e245f7eb0e670bde0577f1eb43?d=identicon&s=25 Pito Salas (pitosalas)
on 2009-06-09 00:34
Gregory Brown wrote:
> On Wed, Jun 3, 2009 at 3:00 PM, Mark Thomas <mark@thomaszone.com> wrote:
>
>> I think part 4 of this article is worth reading:
>> 
http://deadprogrammersociety.blogspot.com/2006/11/...
>
> Also, the sample chapter of my book has a relevant section about this,
> under "Building Flexible Interfaces":
>
> http://oreilly.com/catalog/9780596156749/index.html

Greg, I read the chapter. Excellent stuff. When's the book coming out?
Or is it?

- Pito
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2009-06-09 14:55
(Received via mailing list)
On Mon, Jun 8, 2009 at 6:34 PM, Pito Salas<rps@salas.com> wrote:
>
> Greg, I read the chapter. Excellent stuff. When's the book coming out?
> Or is it?

It's already off to the printers so if you pre-order it now you should
have it within the next couple weeks.

-greg
953d32e245f7eb0e670bde0577f1eb43?d=identicon&s=25 Pito Salas (pitosalas)
on 2009-06-09 16:57
Gregory Brown wrote:
> On Mon, Jun 8, 2009 at 6:34 PM, Pito Salas<rps@salas.com> wrote:
>>
>> Greg, I read the chapter. Excellent stuff. When's the book coming out?
>> Or is it?
>
> It's already off to the printers so if you pre-order it now you should
> have it within the next couple weeks.
>
> -greg

http://www.salas.com/2009/06/09/geeky-check-out-ru...

:)
This topic is locked and can not be replied to.