Modules and methods

Explaine this to me please:

module MyMod

    def mymethod
       puts "hello"
       priv_method
    end

    def priv_method
       puts "world"
    end

    private :priv_method

end

MyMod.mymethod

undefined method `priv_method' for MyMod:Module (NoMethodError)

Javier Valencia wrote:

Explaine this to me please:

module MyMod

   def mymethod
      puts "hello"
      priv_method
   end

   def priv_method
      puts "world"
   end

   private :priv_method

end

MyMod.mymethod

undefined method `priv_method' for MyMod:Module (NoMethodError)

Sorry, mymethod definition is -> def MyMod.mymethod.

The result is the same

How about:
--8<---
#!/usr/bin/ruby -wd

module MyMod

  def MyMod.mymethod
    puts "hello"
    priv_method
  end

  private
  def MyMod.priv_method
    puts "world"
  end

end

MyMod.mymethod
--8<---

regards,

Brian

···

On Wed, 2 Mar 2005 20:48:35 +0900, Javier Valencia <jvalencia@log01.org> wrote:

Javier Valencia wrote:

> Explaine this to me please:
>
> module MyMod
>
> def mymethod
> puts "hello"
> priv_method
> end
>
> def priv_method
> puts "world"
> end
>
> private :priv_method
>
> end
>
> MyMod.mymethod
>
> undefined method `priv_method' for MyMod:Module (NoMethodError)
>
>
Sorry, mymethod definition is -> def MyMod.mymethod.

The result is the same

--
Brian Schröder
http://ruby.brian-schroeder.de/

Brian Schröder wrote:

···

On Wed, 2 Mar 2005 20:48:35 +0900, Javier Valencia <jvalencia@log01.org> wrote:

Javier Valencia wrote:

Explaine this to me please:

module MyMod

  def mymethod
     puts "hello"
     priv_method
  end

  def priv_method
     puts "world"
  end

  private :priv_method

end

MyMod.mymethod

undefined method `priv_method' for MyMod:Module (NoMethodError)

Sorry, mymethod definition is -> def MyMod.mymethod.

The result is the same

How about:
--8<---
#!/usr/bin/ruby -wd

module MyMod

def MyMod.mymethod
  puts "hello"
  priv_method
end

private
def MyMod.priv_method
  puts "world"
end

end

MyMod.mymethod
--8<---

regards,

Brian

Yeah, i know that way, but why i have to use MyMod name into MyMod itself, it looks ugly

Javier Valencia wrote:

Brian Schröder wrote:

Javier Valencia wrote:

Explaine this to me please:

module MyMod

  def mymethod
     puts "hello"
     priv_method
  end

  def priv_method
     puts "world"
  end

  private :priv_method

end

MyMod.mymethod

undefined method `priv_method' for MyMod:Module (NoMethodError)

Sorry, mymethod definition is -> def MyMod.mymethod.

The result is the same

How about:
--8<---
#!/usr/bin/ruby -wd

module MyMod

    def MyMod.mymethod
        puts "hello"
        priv_method
    end

    private
    def MyMod.priv_method puts "world"
    end

end

MyMod.mymethod
--8<---

regards,

Brian

Yeah, i know that way, but why i have to use MyMod name into MyMod itself, it looks ugly

well, the correct question is why can't i use the method without MyMod precedence?

···

On Wed, 2 Mar 2005 20:48:35 +0900, Javier Valencia >> <jvalencia@log01.org> wrote:

Just another example:

···

-------------------
module MyMod

    def MyMod.mymethod
        puts "hello"
        priv_method
    end
       private
    def MyMod.priv_method
        puts "world"
    end
   
MyMod.priv_method

outputs: "world"
-----------------------

this works, but it's under private. what's happening? i don't understand anything

    private
    def MyMod.priv_method
        puts "world"
    end

You have written something like this

       private
       class << self
          def priv_method
             puts "world"
          end
       end

i.e. private was the default for the module but not for the singleton
class.

Guy Decoux

aha, that means:

#!/usr/bin/ruby -wd

module MyMod

  def MyMod.mymethod
    puts "hello"
    priv_method
  end

  class << self
    private
    def priv_method
      puts "world"
    end
  end
    
end

MyMod.mymethod => "Hello World"

MyMod.priv_method => Error, private method

would be the correct way to define it?

regards,

Brian

···

On Wed, 2 Mar 2005 21:55:02 +0900, ts <decoux@moulon.inra.fr> wrote:

> private
> def MyMod.priv_method
> puts "world"
> end

You have written something like this

       private
       class << self
          def priv_method
             puts "world"
          end
       end

i.e. private was the default for the module but not for the singleton
class.

--
Brian Schröder
http://ruby.brian-schroeder.de/

would be the correct way to define it?

yes, you can do it like this. This is the same than when you want to define
an accessor (attr_...) for the "singleton" class

Guy Decoux

ts wrote:

"B" == =?ISO-8859-1?Q?Brian Schr=F6der?= <ISO-8859-1> writes:
           
would be the correct way to define it?

yes, you can do it like this. This is the same than when you want to define
an accessor (attr_...) for the "singleton" class

Guy Decoux

Never seen such way in documentation!
There should be an api change to allow a more "user friendly" way to do things?

Never seen such way in documentation!

documentation is just for anglois :slight_smile:

Guy Decoux

Hi,

···

In message "Re: Modules and methods" on Wed, 2 Mar 2005 22:26:36 +0900, Javier Valencia <jvalencia@log01.org> writes:

There should be an api change to allow a more "user friendly" way to do
things?

We need to define "user friendly" before entering serious discussion.
But I recommend you to learn the language first. In general, trying
to change your learning language is not a good idea.

              matz.

I see that with instance methods happens the same:

module MyMod

    def mymethod
        puts "hello"
        priv_method
    end
       private
    def priv_method
        puts "world"
    end

end

include MyMod

priv_method

priv_method

In ruby, a private method is a method that you must call *without* a
receiver (like in your example)

In your case, try

    self.priv_method

and you'll see that ruby will give an error.

Guy Decoux

If I understand it correctly, then including MyMod into Object, makes
priv_method a private method of Object. So when you call priv_method
inside of object it can access it.

If you try to do

include MyMod

self.priv_method

it will throw an error, because priv_method is private. Maybe it all
can become clearer if you include MyMod into another class than Object
because you are doing things implicitly here.

HTH,

Brian

···

On Wed, 2 Mar 2005 22:32:14 +0900, Javier Valencia <jvalencia@log01.org> wrote:

I see that with instance methods happens the same:

module MyMod

    def mymethod
        puts "hello"
        priv_method
    end

    private
    def priv_method
        puts "world"
    end

end

include MyMod

priv_method

--
Brian Schröder
http://ruby.brian-schroeder.de/

ts wrote:

priv_method

In ruby, a private method is a method that you must call *without* a
receiver (like in your example)

In your case, try

   self.priv_method

and you'll see that ruby will give an error.

Guy Decoux

So it works only for heritance if i can understand.
Shouldn't it work for all cases, not only heritance?

So it works only for heritance if i can understand.
Shouldn't it work for all cases, not only heritance?

I have not understood, sorry.

Can you give what is the definition of private for you ?

Guy Decoux

ts wrote:

So it works only for heritance if i can understand.
Shouldn't it work for all cases, not only heritance?

I have not understood, sorry.

Can you give what is the definition of private for you ?

Guy Decoux

I mean that you can't use that method anywere at anytime, only within the module or class.
This is the c++ way

This is the c++ way

this is what I wanted to hear : perhaps now it's time to you to learn the
ruby way :-)))

Guy Decoux

I mean that you can't use that method anywere at anytime, only within
the module or class.
This is the c++ way

You have included the module, so you are inside of the module and may use it.

Regards,

Brian

···

--
Brian Schröder
http://ruby.brian-schroeder.de/