Last Updated: February 25, 2016
·
2.407K
· monde

execute ruby scripts directly without bundler exec

You can execute ruby scripts directly in a directory with a bundler Gemfile without having to invoke them with the bundler exec command by requiring budler/setup in your script.

In this example assume that the Gemfile includes active_support such as in a Rails app. example.rb:

#!/usr/bin/env ruby
require 'rubygems'
require 'bundler/setup'
require 'active_support/all'

# overkill example, blank? is defined in active support
puts "  nil is blank? #{nil.blank?}"
puts "   '' is blank? #{''.blank?}"
puts "'foo' is blank? #{'foo'.blank?}"

Ensure the script is executable and then call it directly.

% chmod +x example.rb 
% ./example.rb 
  nil is blank? true
   '' is blank? true
'foo' is blank? false