Sha256: 7227679b15a9a9b711ba726429d001714188cc93af0727274e09624d37a28356

Contents?: true

Size: 1.35 KB

Versions: 7

Compression:

Stored size: 1.35 KB

Contents

module RightSupport::Ruby
  module ObjectExtensions
    # Attempt to require one or more source files.
    #
    # This method is useful to conditionally define code depending on the availability
    # of gems or standard-library source files.
    #
    # === Parameters
    # Forwards all parameters transparently through to Kernel#require.
    #
    # === Return
    # Returns true or false
    def require_succeeds?(*args)
      require(*args)
      return true
    rescue LoadError => e
      return false
    end

    # Attempt to require one or more source files; if the require succeeds (or
    # if the files have already been successfully required), yield to the block.
    #
    # This method is useful to conditionally define code depending on the availability
    # of gems or standard-library source files. 
    #
    # === Parameters
    # Forwards all parameters transparently through to Kernel#require.
    #
    # === Block
    # The block will be called if the require succeeds (if it does not raise LoadError).
    #
    # === Return
    # Preserves the return value of Kernel#require (generally either true or false).
    def if_require_succeeds(*args)
      result = require(*args)
      yield if block_given?
      return result
    rescue LoadError => e
      return false
    end
  end
end

class Object
  include RightSupport::Ruby::ObjectExtensions
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
right_support-1.4.1 lib/right_support/ruby/object_extensions.rb
right_support-1.4.0 lib/right_support/ruby/object_extensions.rb
right_support-1.3.3 lib/right_support/ruby/object_extensions.rb
right_support-1.3.2 lib/right_support/ruby/object_extensions.rb
right_support-1.3.1 lib/right_support/ruby/object_extensions.rb
right_support-1.3.0 lib/right_support/ruby/object_extensions.rb
right_support-1.2.0 lib/right_support/ruby/object_extensions.rb