1

我在 irb 工作并试图清理我下载的一些代码。

我正在运行这个:

require '/Users/alexgenadinik/projects/cmply/cmply-app/lib/app/social/linkedin/linkedin.rb'

它工作正常。该文件包含以下内容:

require File.join(File.expand_path("../",__FILE__),"init")
require 'oauth'

module LinkedIn
  puts "helllllooooo"

  class << self

    #logger.debug "....teeest"
    attr_accessor :token, :secret, :default_profile_fields

    # config/initializers/linkedin.rb (for instance)
    #
    # LinkedIn.configure do |config|
    #   config.token = 'consumer_token'
    #   config.secret = 'consumer_secret'
    #   config.default_profile_fields = ['education', 'positions']
    # end
    #
    # elsewhere
    #
    # client = LinkedIn::Client.new
    def configure
      yield self
      true
    end
  end

  #root_path = File.expand_path("../../../../../",__FILE__)

  autoload :Api,     File.join(LINKED_IN_LOAD_PATH,"linked_in/api.rb") #"linked_in/api"
  autoload :Client,  File.join(LINKED_IN_LOAD_PATH,"linked_in/client.rb") #"linked_in/client"
  autoload :Mash,    File.join(LINKED_IN_LOAD_PATH,"linked_in/mash.rb") #"linked_in/mash"
  autoload :Errors,  File.join(LINKED_IN_LOAD_PATH,"linked_in/errors.rb") #"linked_in/errors"
  autoload :Helpers, File.join(LINKED_IN_LOAD_PATH,"linked_in/helpers.rb") #"linked_in/helpers"
  autoload :Search,  File.join(LINKED_IN_LOAD_PATH,"linked_in/search.rb") #"linked_in/search"

end

但是当我尝试运行这样的命令时:

client = LinkedIn::Client.new('key', 'key')

我收到此错误:

LoadError: no such file to load -- linked_in/helpers/authorization
    from /Users/alexgenadinik/projects/cmply/cmply-app/lib/app/social/linkedin/linked_in/helpers/authorization.rb:4
    from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
    from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `require'
    from /Users/alexgenadinik/projects/cmply/cmply-app/lib/app/social/linkedin/linked_in/client.rb:2
    from (irb):2

所以它指向 client.rb 的第 2 行,开头是这样的:

require 'cgi'
require File.join(LINKED_IN_LOAD_PATH, "linked_in","helpers/authorization")

和authorization.rb的第4行,开头是这样的:

module LinkedIn
  module Helpers

    module Authorization

顺便说一句,我应该从顶部阅读错误消息,还是应该从底部开始阅读以查看错误首先出现的位置?

非常感谢帮助。我不确定为什么它会给出错误。

4

1 回答 1

1

您的文件所在的目录是“linkedin”,但您需要的目录是“linked_in”,您应该将实际目录的名称更改为 linked_in,因为这符合命名约定。

除此之外,我很确定 Rails 会将 app 下的所有目录添加到加载路径中。所以你应该可以说require 'linked_in/linked_in'(假设你同时改变了目录和文件名)然后你可以对所有的自动加载做同样的事情,并摆脱这些File.expand_path ...东西。

于 2012-05-14T19:15:28.923 回答