2011-01-23 4 views
2

내 환경으로 인해 실패한 루비 스크립트가 있습니다. irb에서 이상한 동작을 일으킨 것으로 생각됩니다 (rvm을 사용하고 있지만 문제가 아닌 것 같습니다)MacOSX에서 Ruby/IRB 환경 문제

>> ruby -v 
ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin9.8.0] 
>> irb 
>> FileUtils.mkdir_p('tmp') 
    NameError: uninitialized constant FileUtils 
     from (irb):1 
>> help 
    => nil 
>> FileUtils.mkdir_p('tmp') 
    => "tmp" 

처음에는 FileUtils 명령이 실패하지만 도움말 (또한 실패 함)을 입력하면 작동하는 것 같습니다.

'rubygems'가 필요하며 'FileUtils'가 필요합니다. 문제를 해결하지만 여기에서 무슨 일이 일어나고 있는지 알고 싶습니다.

+1

필자는'help'가'require 'fileutils'' some을 호출하는 것이 꽤 분명하다고 생각합니다. 당신은 이것에 대해 뭔가를 찾을 수 있습니다 : https://github.com/ruby/ruby/tree/trunk/lib/irb – zengr

답변

1

"help"명령이 있다는 것을 알지 못했지만 FileUtils에 대한 의존성이있어 아마도 도움말 파일을로드했을 것입니다. "도움말"은 IRB 세션에 요구 사항을로드하고 있습니다.

>> before = ObjectSpace.each_object.map { |i| i.class }.uniq 
=> [Regexp, String, Array, Class, Hash, Module, Proc, MatchData, File, Binding, NoMemoryError, Float, SystemStackError, fatal, Bignum, Object, IO, Thread, ThreadGroup, IRB::Locale, IRB::Notifier::LeveledNotifier, IRB::Notifier::CompositeNotifier, IRB::StdioOutputMethod, IRB::Notifier::NoMsgNotifier, Enumerable::Enumerator, RubyToken::TkNL, RubyToken::TkEND, RubyToken::TkBITOR, RubyToken::TkIDENTIFIER, RubyToken::TkDOT, RubyToken::TkRBRACE, RubyToken::TkSPACE, RubyToken::TkfLBRACE, RubyToken::TkCONSTANT, RubyToken::TkASSIGN, IRB::SLex::Node, IRB::SLex, RubyLex, IRB::ReadlineInputMethod, IRB::WorkSpace, IRB::Context, IRB::Irb] 
>> help 
=> nil 
>> after = ObjectSpace.each_object.map { |i| i.class }.uniq 
=> [Regexp, String, MatchData, Array, Class, RI::ClassEntry, RI::MethodEntry, Hash, Module, Dir, Proc, File, Binding, NoMemoryError, Float, SystemStackError, fatal, Bignum, Object, IO, Thread, ThreadGroup, IRB::Locale, Range, IRB::Notifier::LeveledNotifier, IRB::Notifier::CompositeNotifier, IRB::StdioOutputMethod, IRB::Notifier::NoMsgNotifier, YAML::Syck::Resolver, Gem::ConfigFile, RubyToken::TkNL, RubyToken::TkIDENTIFIER, IRB::SLex::Node, IRB::SLex, RubyLex, IRB::ReadlineInputMethod, IRB::WorkSpace, IRB::Context, IRB::Irb, RI::TopLevelEntry, RI::RiReader, GetoptLong, RI::RiCache, RI::Options, RiDriver, Rational, Date::Infinity, Enumerable::Enumerator, RubyToken::TkRBRACE, DefaultDisplay, RI::TextFormatter] 
>> after == before 
=> false 
>> after - before 
=> [RI::ClassEntry, RI::MethodEntry, Dir, Range, YAML::Syck::Resolver, Gem::ConfigFile, RI::TopLevelEntry, RI::RiReader, GetoptLong, RI::RiCache, RI::Options, RiDriver, Rational, Date::Infinity, DefaultDisplay, RI::TextFormatter] 

after - before에 클래스를로드합니다. FileUtils는 어디에 있습니까? 나는 그것의 모듈이 Dir의 일부라고 생각하지만, 나는 그것의 100 %가 아니다.

+0

Tin Man이 말했듯이, OP는 'fileutils'를 요구해야합니다. –

+0

@ Ryan Bigg, 문제는 "왜 그 문제를 해결하는 데 도움이되는지"였다. 그는 그 기사의 마지막 줄에 그 해결책이 있다고 말했다. – EnabrenTane

1

당신은 require 'fileutils' 필요 : 그것은 IRB 그것을 미리로드하지 않는 이유는 기본적으로 인터프리터에 의해 포함되지

require 'fileutils' 
FileUtils.pwd # => "/" 

. IRB는 대화식이기 때문에 인터프리터가로드 도움말 파일과 같이 수행하지 않는 작업을 즉석에서 수행해야합니다. 귀하의 요청에 대한 응답으로 그렇게하는 것은 저에게 예상치 못한 일이 아니며, 단지 그렇게하도록 프로그램 된 것입니다. 코드를 보면 쉽게 추적 할 수있을 것입니다.

기본적으로 IRB는 구문 오류에 올바르게 응답하고 "help"명령에 대한 응답으로 수행 한 작업을 수행합니다. 당신이 절대적으로 그것이 무엇을하고 있는지 알고있는 경우

, 당신은 처리를 추적 IRB를 요청하여 알아낼 수 있습니다

echo help | irb -f --trace > irb.out 

는 "도움"을 입력하면 IRB가 수행하는 작업의 추적을 생성합니다. 파일을 검색하여 보여줍니다

#0:/Users/greg/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/rdoc/ri/store.rb:2::-: require 'fileutils' 

은 IRB로드 '리'로 store.rb이 요구된다.

FileUtils는 Ruby 표준 라이브러리의 일부이므로 인터프리터와 함께 번들로 제공되지만 인터프리터 시작시 Dir 및 File과 같이 자동으로 포함되지 않습니다. 그것은 완전히 독립적이며 Dir의 일부는 아닙니다.

+0

질문은'왜 그 문제를 해결하는 데 도움이됩니까? '마지막 줄에 그는 해결책이 있지만 궁금한 이유는 무엇입니까? – EnabrenTane

관련 문제