<p>I'm working on a Rails app and am looking to include some functionality from "<a href="https://stackoverflow.com/questions/42566/getting-the-hostname-or-ip-in-ruby-on-rails">Getting the Hostname or IP in Ruby on Rails</a>" that I asked.</p>

<p>I'm having problems getting it to work. I was under the impression that I should just make a file in the lib directory, so I named it 'get_ip.rb', with the contents:</p>

<pre><code>require 'socket'

module GetIP
  def local_ip
    orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true  # turn off reverse DNS resolution temporarily

    UDPSocket.open do |s|
      s.connect '64.233.187.99', 1
      s.addr.last
    end
  ensure
    Socket.do_not_reverse_lookup = orig
  end
end
</code></pre>

<p>I had also tried defining GetIP as a class but when I do the usual <code>ruby script/console</code>, I'm not able to use the <code>local_ip</code> method at all. Any ideas?</p>

[{'answer_id': 45261, 'author': 'Curt Hagenlocher', 'author_id': 533, 'author_profile': 'https://Stackoverflow.com/users/533', 'pm_score': 3, 'selected': True, 'text': "<p>You haven't described how you're trying to use the method, so I apologize in advance if this is stuff you already know.</p>\n\n<p>The methods on a module never come into use unless the module is included into a class.  Instance methods on a class require there to be an instance of the class.  You probably want a class method instead.  And the file itself should be loaded, generally through the require statement.</p>\n\n<p>If the following code is in the file getip.rb,</p>\n\n<pre><code>require 'socket'\n\nclass GetIP\n  def self.local_ip\n    orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true\n\n    UDPSocket.open do |s|\n      s.connect '64.233.187.99', 1\n      s.addr.last\n    end\n  ensure\n    Socket.do_not_reverse_lookup = orig\n  end\nend\n</code></pre>\n\n<p>Then you should be able to run it by saying,</p>\n\n<pre><code>require 'getip'\nGetIP.local_ip\n</code></pre>\n"}, {'answer_id': 45291, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 3, 'selected': False, 'text': "<p><code>require</code> will load a file. If that file contains any class/module definitions, then your other code will now be able to use them. If the file just contains code which is not in any modules, it will get run as if it were in the same place as your 'require' call (like PHP include)</p>\n\n<p><code>include</code> is to do with modules.</p>\n\n<p>It takes all the methods in the module, and adds them to your class. Like this:</p>\n\n<pre><code>class Orig\nend\n\nOrig.new.first_method # no such method\n\nmodule MyModule\n  def first_method\n  end\nend\n\nclass Orig\n   include MyModule\nend\nOrig.new.first_method # will now run first_method as it's been added.\n</code></pre>\n\n<p>There's also <code>extend</code> which works like include does, but instead of adding the methods as <em>instance</em> methods, adds them as <em>class</em> methods, like this:</p>\n\n<p>Note above, how when I wanted to access first_method, I created a new object of <code>Orig</code> class. That's what I mean by instance method.</p>\n\n<pre><code>class SecondClass\n  extend MyModule\nend\nSecondClass.first_method # will call first_method\n</code></pre>\n\n<p>Note that in this example I'm not making any new objects, just calling the method directly on the class, as if it had been defined as <code>self.first_method</code> all along.</p>\n\n<p>So there you go :-)</p>\n"}, {'answer_id': 45676, 'author': 'TonyLa', 'author_id': 1295, 'author_profile': 'https://Stackoverflow.com/users/1295', 'pm_score': 0, 'selected': False, 'text': '<p><code>require</code> and <code>include</code> are two different things.</p>\n\n<p><code>require</code> is to strictly load a file once from a load path.  The loadpath is a string and this is the key used to determine if the file has already been loaded.</p>\n\n<p><code>include</code> is used to "mix-in" modules into other classes. <code>include</code> is called on a module and the module methods are included as instance methods on the class.</p>\n\n<pre><code>  module MixInMethods\n    def mixed_in_method\n      "I\'m a part of #{self.class}"\n    end\n  end\n\n  class SampleClass\n    include MixInMethods\n  end\n\n  mixin_class = SampleClass.new\n  puts my_class.mixed_in_method # &gt;&gt; I\'m a part of SampleClass\n</code></pre>\n\n<p>But many times the module you want to mix in is not in the same file as the target class.  So you do a <code>require \'module_file_name\'</code> and then inside the class you do an <code>include module</code>.  </p>\n'}]
['https://Stackoverflow.com/questions/45253', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/422/']