Forum: Ruby Intra-module method calls

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.
Wes G. (Guest)
on 2006-04-28 19:30
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
Wes G. (Guest)
on 2006-04-28 19:31
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
zdennis (Guest)
on 2006-04-28 20:24
(Received via mailing list)
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
Wes G. (Guest)
on 2006-04-28 20:29
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
ts (Guest)
on 2006-04-28 21:35
(Received via mailing list)
>>>>> "W" == Wes G. <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
Wes G. (Guest)
on 2006-04-28 21:42
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> 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
This topic is locked and can not be replied to.