Modules and methods

  • Thread starter Javier Valencia
  • Start date
J

Javier Valencia

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)
 
R

Robert Klemme

Javier Valencia said:
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)

You're defining instance methods, i.e. if you create an instance of a
class that includes MyMod it will have these methods.

You probably want singleton methods of the module:

module MyMod
class <<self
def mymethod
puts "hello"
priv_method
end

def priv_method
puts "world"
end

private :priv_method
end
end
hello
world
=> nil

Kind regards

robert
 
J

Javier Valencia

Javier said:
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
 
B

Brian Schröder

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
 
J

Javier Valencia

Brian said:
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
 
J

Javier Valencia

Javier said:
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?
 
J

Javier Valencia

Just another example:

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

def MyMod.mymethod
puts "hello"
priv_method
end

private
def MyMod.priv_method
puts "world"
end


end

MyMod.priv_method

outputs: "world"
 
T

ts

J> private
J> def MyMod.priv_method
J> puts "world"
J> 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
 
B

Brian Schröder

J> private
J> def MyMod.priv_method
J> puts "world"
J> 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.

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
 
T

ts

"B" == =?ISO-8859-1?Q?Brian Schr=F6der?= <ISO-8859-1> writes:

B> 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
 
J

Javier Valencia

ts said:
B> 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?
 
J

Javier Valencia

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
 
T

ts

J> 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
 
J

Javier Valencia

ts said:
J> 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?
 
T

ts

J> So it works only for heritance if i can understand.
J> 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
 
M

Malte Milatz

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

Well, the purposes of a module are mainly

- being a name space for classes
- gathering together methods, which then can be mixed into classes.

Extensive use of module methods (def MyModule.my_method) are often an
indication for a functional (or at least: unusual) programming style, that
is, not what Ruby is really for. So it's quite understandable that it's a
little involved to use them extensively.

That's my opinion, anyway.

Malte
 
B

Brian Schröder

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

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
 
J

Javier Valencia

ts said:
J> So it works only for heritance if i can understand.
J> 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
 
T

ts

J> 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top