# Intra-module method calls

**URL:** <https://www.ruby-forum.com/t/intra-module-method-calls/59103>\
**Category:** Ruby\
**Created:** [April 28, 2006, 5:30pm UTC](https://www.ruby-forum.com/t/intra-module-method-calls/59103 "2006-04-28T17:30:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 28, 2006, 5:30pm UTC](https://www.ruby-forum.com/t/intra-module-method-calls/59103/1 "2006-04-28T17:30:32Z")

</div>

I have a module named X

module X

end

I would like to call one method from another in my module.

If I do

public  
def method1  
…  
method2  
end

private  
def method2  
…  
end

why doesn’t this work? Is it because this isn’t a class and the  
“private” keyword isn’t meaningful?

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 28, 2006, 5:31pm UTC](https://www.ruby-forum.com/t/intra-module-method-calls/59103/2 "2006-04-28T17:31:36Z")

</div>

Sorry, I wasn’t clear enough - my question is why does the call to  
method2 from method1 fail with a

undefined method `method2’ for X:Module

error?

Thanks,  
WG

Wes G. wrote:

> I have a module named X
> 
> module X
> 
> end
> 
> I would like to call one method from another in my module.
> 
> If I do
> 
> public  
> def method1  
> …  
> method2  
> end
> 
> private  
> def method2  
> …  
> end
> 
> why doesn’t this work? Is it because this isn’t a class and the  
> “private” keyword isn’t meaningful?
> 
> Thanks,  
> Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 28, 2006, 6:24pm UTC](https://www.ruby-forum.com/t/intra-module-method-calls/59103/3 "2006-04-28T18:24:57Z")

</div>

Wes G. wrote:

> Sorry, I wasn’t clear enough - my question is why does the call to  
> method2 from method1 fail with a
> 
> undefined method `method2’ for X:Module

Is the following what you are doing ?

irb(main):007:0\> module X  
irb(main):008:1\> public  
irb(main):009:1\> def method1  
irb(main):010:2\> “method1” + method2  
irb(main):011:2\> end  
irb(main):012:1\> private  
irb(main):013:1\> def method2  
irb(main):014:2\> “method2”  
irb(main):015:2\> end  
irb(main):016:1\> end  
=\> nil  
irb(main):017:0\> class A  
irb(main):018:1\> include X  
irb(main):019:1\> end  
=\> A  
irb(main):020:0\> A.new.method1  
---------------------^^^^^^^^^^---------------  
irb(main):021:0\> A.new.method1  
=\> “method1method2”

Because the above should work just fine. If the above is not what you  
are seeing, please post the code so we can better help.

Zach

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 28, 2006, 6:29pm UTC](https://www.ruby-forum.com/t/intra-module-method-calls/59103/4 "2006-04-28T18:29:36Z")

</div>

I just want to have some utility methods for use by my app.  
The code below works.  
Notice the calls to add\_default\_path in the other two methods.

My question is: why do I have to qualify the name of add\_default\_path  
with ESimplyUtil since no external code is calling that method? Why  
can’t I just have

def add\_default\_path  
end

?

Thanks,  
WG

===================

Here’s my module:

module ESimplyUtil  
public  
def ESimplyUtil.validate\_and\_format\_url(in\_url)  
begin  
url = URI.parse(in\_url)  
rescue  
raise(“This is not a valid URL”)  
end

```
#If the URL is valid HTTP URL, default the path if necessary
#If the URL is generic make it into an HTTP URL
# Get the host - match the first set of non-/ characters
# Get the path - match all of the characters including the first 

```

slash  
#If the URL is a valid non-HTTP URL, then it’s an error.  
if (url.instance\_of?(URI::HTTP))  
url = add\_default\_path(url)  
elsif (url.instance\_of?(URI::Generic))  
if (((url.scheme != nil) && (url.scheme != “http”)) ||  
(url.path.nil?) || (url.path == ‘’))  
raise(“This is not a valid URL”)  
else  
port = 80  
host = url.path.match(/[^/]+/)[0]  
path\_match = url.path.match(//(.\*)/)  
if (path\_match.nil?)  
path = ‘/’  
else  
path = path\_match[0]  
end

```
    url = URI::HTTP.new('http', nil, host, 80, nil, path, nil, nil, 

```

nil)  
end  
elsif (! url.instance\_of?(URI::HTTP))  
raise(“This is not a valid URL”)  
end

```
url

```

end

public  
def ESimplyUtil.get\_URL(myURL)  
url = URI.parse(myURL)  
url = add\_default\_path(url)  
req = Net::HTTP::Get.new(url.path)  
res = Net::HTTP.start(url.host, url.port) {|http|  
http.request(req)  
}

```
res

```

end

private  
def ESimplyUtil.add\_default\_path(url)  
if ((url.path == ‘’) || (url.path.nil?))  
url.path = ‘/’  
end

```
url

```

end  
end

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 28, 2006, 7:35pm UTC](https://www.ruby-forum.com/t/intra-module-method-calls/59103/5 "2006-04-28T19:35:04Z")

</div>

> > > > > “W” == Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

W\> module ESimplyUtil

[…]

W\> private  
W\> def ESimplyUtil.add\_default\_path(url)

Be carefull, ruby will not do what you think

moulon% cat b.rb  
#!/usr/bin/ruby  
module A  
private  
def self.a  
end

class \<\< self  
private  
def b  
end  
end  
end

[“a”, “b”].each do |m|  
puts “#{m}() private ? #{A.private\_methods.include?(m)}”  
puts “#{m}() public ? #{A.public\_methods.include?(m)}”  
end

moulon%

moulon% ./b.rb  
a() private ? false  
a() public ? true  
b() private ? true  
b() public ? false  
moulon%

Guy Decoux

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 28, 2006, 7:42pm UTC](https://www.ruby-forum.com/t/intra-module-method-calls/59103/6 "2006-04-28T19:42:55Z")

</div>

So, in normal language, does that mean that “private” is not meaningful  
within the context of a module?

Thanks,  
Wes

ts wrote:

> > > > > > “W” == Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> W\> module ESimplyUtil
> 
> […]
> 
> W\> private  
> W\> def ESimplyUtil.add\_default\_path(url)
> 
> Be carefull, ruby will not do what you think
> 
> moulon% cat b.rb  
> #!/usr/bin/ruby  
> module A  
> private  
> def self.a  
> end
> 
> class \<\< self  
> private  
> def b  
> end  
> end  
> end
> 
> [“a”, “b”].each do |m|  
> puts “#{m}() private ? #{A.private\_methods.include?(m)}”  
> puts “#{m}() public ? #{A.public\_methods.include?(m)}”  
> end
> 
> moulon%
> 
> moulon% ./b.rb  
> a() private ? false  
> a() public ? true  
> b() private ? true  
> b() public ? false  
> moulon%
> 
> Guy Decoux
