Search code examples
ruby-on-railsruby

When do I have to 'require' a Ruby class in my Rake task?


I'm working on a Rake task which fetches a file via an external URL. To do this, I have to require the Net::HTTP Ruby class in my .rake file.

This seems odd to me, because I've not loaded it in as a gem (I've not downloaded it from anywhere in fact), so this Ruby class must be pre-installed in the Rails framework. Yet in every .rake file I have, I have to require it. So it must, for some reason, be inactive until I tell it otherwise(?).

This is not the same question as "How to require classes that I've created in different files". Rather, I want to know

  • How it is that I don't have to download and install the Net::HTTP class into my application?
  • Why do I have to manually require it when I use it, if it's already pre-installed in Rails?

Solution

  • Net::HTTP is part of the Ruby Standard Library, which is distributed with Ruby itself (not Rails). To use any of the standard libraries you must require them first.