Sha256: 0056622bb216bc59b113b2f5604f1a3fa91ef0d8a5981799819f2c1eeafc2332

Contents?: true

Size: 506 Bytes

Versions: 3

Compression:

Stored size: 506 Bytes

Contents

module Kernel
  # Optionally require a gem. If it is not available, <tt>nil</tt>
  # will be returned. Alternatively, a block can be provided with code
  # to run.
  #
  # @example Without a block
  #   require "non_existent" #=> nil
  # @example With a custom block
  #   require "non_existent" do
  #     puts "Something went wrong"
  #   end #=> Outputs "Something went wrong"
  def require_optional(gem, &block)
    require gem
  rescue LoadError
    block&.call
  end

  private :require_optional
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
shenanigans-1.0.16 lib/shenanigans/kernel/require_optional.rb
shenanigans-1.0.15 lib/shenanigans/kernel/require_optional.rb
shenanigans-1.0.14 lib/shenanigans/kernel/require_optional.rb