developer tip

gem을 설치했지만 'gemname'이 필요하지 않습니다.

optionbox 2020. 12. 1. 07:55
반응형

gem을 설치했지만 'gemname'이 필요하지 않습니다. 왜?


내가 정말로 묻는 질문은 왜 require가 보석의 이름을 취하지 않는지입니다. 또한 그렇지 않은 경우, 망할 물건을 요구하는 비밀 주문을 찾는 가장 쉬운 방법은 무엇입니까!?

예를 들어 memcache-client설치 한 경우 다음을 사용하여 요구해야합니다.

require 'rubygems'
require 'memcache'

포함해야하는 파일이 무엇인지에 대한 표준은 없습니다. 그러나 try를 따라 사용할 수있는 일반적으로 따르는 몇 가지 규칙이 있습니다.

  • 종종 파일은 gem과 같은 이름으로 불립니다. 그래서 require mygem작동합니다.
  • 종종 파일 당신이 보석의 이름을 얻을 수 있도록하는 경우, 보석의 lib 하위 디렉토리에있는 유일한 .rb 파일입니다 (아마도 당신 itterating이 공급 업체를 통해입니다 / 2.1 레일 프로젝트 사전에 보석), 당신은 검사 할 수 있습니다 #{gemname}/lib합니다. rb 파일이 있고, 하나만 있다면 필요한 것입니다.

이 모든 것이 작동한다면 gem의 디렉토리를 살펴 보는 것뿐입니다 ( gem environment | grep INSTALLATION | awk '{print $4}'lib 디렉토리 를 실행 하고 살펴보면 찾을 수 있습니다 . 파일을 읽어야 할 것이고해야 할 일을 설명하는 주석이 있기를 바랍니다)


내 시스템도 RubyGems의 존재에 대해 알지 못하는 것 같습니다. 'require'명령은 RubyGems에 의해 덮어 쓰여져 gem을로드 할 수 있지만 RubyGems가 이미 필요하지 않은 한 어떻게해야할지 모릅니다. 따라서 직접 작성하는 경우 다음을 수행 할 수 있습니다.

require 'rubygems'
require 'gem-name-here'

다른 사람의 코드를 실행하는 경우 다음을 사용하여 명령 줄에서 수행 할 수 있습니다.

ruby -r rubygems script.rb

또한 Ruby가 시작시로드 할 항목을 결정하는 데 사용하는 환경 변수가 있습니다.

export RUBYOPT=rubygems

( http://www.rubygems.org/read/chapter/3에서 . 환경 변수는 Orion Edwards 가 저에게 지적했습니다 )

( "require 'rubygems'가 당신에게 효과가 없다면,이 조언은 제한적인 도움이 될 것입니다. :)


요구 사항은 루비 경로에있는 파일에 매핑해야합니다. 'gem 환경'을 실행하여 gem이 설치된 위치를 확인할 수 있습니다 (설치 디렉토리 찾기).

kburton@hypothesisf:~$ gem environment
RubyGems Environment:
  - RUBYGEMS VERSION: 1.2.0
  - RUBY VERSION: 1.8.7 (2008-08-08 patchlevel 71) [i686-linux]
  - INSTALLATION DIRECTORY: /usr/local/ruby/lib/ruby/gems/1.8
  - RUBY EXECUTABLE: /usr/local/ruby/bin/ruby
  - EXECUTABLE DIRECTORY: /usr/local/ruby/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86-linux
  - GEM PATHS:
     - /usr/local/ruby/lib/ruby/gems/1.8
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - http://gems.rubyforge.org/
kburton@editconf:~$ 

그런 다음 요구하려는 특정 .rb 파일을 찾을 수 있습니다. 또한, $ :의 내용을 irb에서 인쇄하여 ruby가 모듈을 검색 할 경로 목록을 볼 수 있습니다.

kburton@hypothesis:~$ irb
irb(main):001:0> $:
=> ["/usr/local/ruby/lib/ruby/site_ruby/1.8", "/usr/local/ruby/lib/ruby/site_ruby/1.8/i686-linux", "/usr/local/ruby/lib/ruby/site_ruby", "/usr/local/ruby/lib/ruby/vendor_ruby/1.8", "/usr/local/ruby/lib/ruby/vendor_ruby/1.8/i686-linux", "/usr/local/ruby/lib/ruby/vendor_ruby", "/usr/local/ruby/lib/ruby/1.8", "/usr/local/ruby/lib/ruby/1.8/i686-linux", "."]
irb(main):002:0>

또한 rails 사람들은 gem을 설치 한 후 rails 서버다시 시작하는 것을 기억해야합니다.


You need to include "rubygems" only if you installed the gem using gem . Otherwise , the secret incantation would be to fire up irb and try different combinations . Also , you can pass the -I option to the ruby interpreter so that you include the instalation directory of the gem , in the LOAD_PATH . Note that $LOAD_PATH is an array , which means you can add directories to it from within your script.


The question I'm really asking is why require does not take the name of the gem.

Installing a gem gets the files onto your system. It doesn't make any claims as to what those files will be called.
As laurie points out there are several conventions for how they are named, but there's nothing to enforce that, and many gem authors unfortunately don't stick to them.

Also, In the case that it doesn't, what's the easiest way to find the secret incantation to require the damn thing!?

Read the docs for your gem?
I find googling for rdoc gemname will usually find the official rdocs for your gem, which usually show you how to use it.

Memcache is perhaps not the best example, as they assume you'll be using it from rails, and the 'require' will have already been done for you, but most other ones I've seen have examples which show the correct 'require' incantations


An issue I just ran into was that the actual built gem was not including all the files that it should have.

The issue with files was that there was a syntax mistake in the in the gemspec, but no errors were thrown during the build.

Just adding this here in case anybody else runs into the same issue.


I too had this problem since installing OS X Lion, and found that even if I ran the following code I would still get the warning message. require 'rubygems' require 'nokogiri'

I tried loads of solutions posted here and on the web, but in the end my work around solution was to simply follow the instructions at http://martinisoftware.com/2009/07/31/nokogiri-on-leopard.html to reinstall LibXML & LibXSLT from source, but ensuring the version of LibXML I installed matched the one that was expected by Nokogiri.

Once I had done that, the warnings went away.


I had this problem because I use rvm and was trying to use the wrong version of ruby. The gem in question needed 1.9.2 and I had set 2.0.0 as my default! Maybe a dumb error but one that someone else arriving on this page will probably have made.


Watch source of gem and check lib directory. If there is no rb file then you must point to gem main rb file in subdirectory:

require 'dir/subdir/file'

for /lib/dir/subdir/file.rb.

참고URL : https://stackoverflow.com/questions/132867/i-have-a-gem-installed-but-require-gemname-does-not-work-why

반응형