TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

How do rubygems work anyway?

1 pointsby starrhorneover 11 years ago

1 comment

jamesbrittover 11 years ago
This doesn&#x27;t explain how rubygems works at all.<p>For example, when you do<p><pre><code> require &quot;some_gem&quot; </code></pre> how does <i>require</i> know where to find <i>some_gem.&lt;ext&gt;</i>?<p>First ruby looks in the current load path (i.e. <i>$:</i>). If it cannot find the file it then looks in your set of installed gems. If it finds a match it loads the file and adds that gem path to $:.<p><a href="http://ruby-doc.org/core-2.1.0/Kernel.html#method-i-require" rel="nofollow">http:&#x2F;&#x2F;ruby-doc.org&#x2F;core-2.1.0&#x2F;Kernel.html#method-i-require</a><p>(though those docs say nothing about checking the gem paths.)<p><i>This makes sense since if your project depends on a certain gem, who knows where you might reference it. Better load it as early as possible so its code can be used anywhere.</i><p>That&#x27;s silly. If you are referencing gem code in a file then have that file require the needed gem. <i>require</i> will only load the file once so you can wait until you need a gem before you load it.<p>(<i>load</i>, OTOH, will reload a file as often as you call it).