Binstubs
Have you ever griped about constantly having to type bundle exec before running executables within a Ruby repo? For instance, bundle exec rubocop or bundle exec rails s ? We need the bundle exec to ensure we're getting the right version of whatever gem or executable we're running. But typing bundle exec continually can be cumbersome. And there could be unexpected version inconsistencies if we forget it. Binstubs to the rescue! Many Ruby apps we use will already have a bin directory containing binstubs. Rails applications, for instance, are initialized with a few binstubs, including bin/rails . Binstubs wrap around executables to ensure we're running the right version of a gem. This means that, for instance, instead of running bundle exec rails server , we can run bin/rails server , and get the same result. Okay, so Rails will generate some binstubs for us, but what about gems which don't have autogenerated binstubs, or if we're not in a Rails repository? We can always run bundle binstubs <gem_name> and bundler will generate the binstub for us. For instance bundle binstubs rubocop will generate bin/rubocop , and then instead of running bundle exec rubocop , we can simply run bin/rubocop . You might be reading this and thinking "bin/rubocop " is still cumbersome, why can't we just run rubocop ? Turns out, we can set ourselves up so we only need to run rubocop . More on this next week! |