Skip to content

Instantly share code, notes, and snippets.

@talbright
Last active August 29, 2015 14:13
Show Gist options
  • Save talbright/e5339467eaacbbbf84fc to your computer and use it in GitHub Desktop.
Save talbright/e5339467eaacbbbf84fc to your computer and use it in GitHub Desktop.
What to do when ruby gems use the same require path and conflict...
# config/initializers/elasticsearch_transition.rb
# Both elasticsearch-0.4.11 and rubberband-0.1.6 use 'elasticsearch'
# in their respective gem lib dirs. This causes the elasticsearch gem
# to incorrectly require same named paths from the wrong gem (in this
# case from rubberband). That's because rubberband is in the require
# path first.
#
# You have to set require: false in your Gemfile for the
# elasticsearch gems.
rubberband = $LOAD_PATH.find { |l| l =~ %r{/rubberband-.*?/lib} }
$LOAD_PATH.delete(rubberband)
require 'elasticsearch'
$LOAD_PATH.unshift(rubberband)
@talbright
Copy link
Author

Conflicts like this happen when gem files use the same paths in the lib directory.

rubberband-0.1.6

$ tree rubberband-0.1.6/lib
rubberband-0.1.6/lib
├── elasticsearch
│   ├── client
│   │   ├── abstract_client.rb
│   │   ├── admin_cluster.rb
│   │   ├── admin_index.rb
│   │   ├── auto_discovering_client.rb
│   │   ├── default_scope.rb
│   │   ├── hits.rb
│   │   ├── index.rb
│   │   └── retrying_client.rb
│   ├── client.rb
│   ├── encoding
│   │   ├── base.rb
│   │   └── json.rb
│   ├── encoding.rb
│   ├── transport
│   │   ├── base.rb
│   │   ├── base_protocol.rb
│   │   ├── http.rb
│   │   ├── memcached.rb
│   │   ├── thrift
│   │   │   ├── elasticsearch_constants.rb
│   │   │   ├── elasticsearch_types.rb
│   │   │   └── rest.rb
│   │   └── thrift.rb
│   ├── transport.rb
│   └── version.rb
├── elasticsearch.rb
└── rubberband.rb

5 directories, 24 files

elasticsearch-transport-0.4.11

$ tree elasticsearch-transport-0.4.11/lib
elasticsearch-transport-0.4.11/lib
├── elasticsearch
│   ├── transport
│   │   ├── client.rb
│   │   ├── transport
│   │   │   ├── base.rb
│   │   │   ├── connections
│   │   │   │   ├── collection.rb
│   │   │   │   ├── connection.rb
│   │   │   │   └── selector.rb
│   │   │   ├── errors.rb
│   │   │   ├── http
│   │   │   │   ├── curb.rb
│   │   │   │   └── faraday.rb
│   │   │   ├── response.rb
│   │   │   ├── serializer
│   │   │   │   └── multi_json.rb
│   │   │   └── sniffer.rb
│   │   └── version.rb
│   └── transport.rb
└── elasticsearch-transport.rb

6 directories, 14 files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment