Sha256: b26f853e853da1c3d83ada2087fd82bb43ee9a44cf7d9aee7b3a87de4552d295

Contents?: true

Size: 541 Bytes

Versions: 3

Compression:

Stored size: 541 Bytes

Contents

# frozen_string_literal: true

# Add simple safe_constantize method to String
module Runger::Ext::StringConstantize
  refine String do
    def safe_constantize
      names = split('::')

      return nil if names.empty?

      # Remove the first blank element in case of '::ClassName' notation.
      names.shift if names.size > 1 && names.first.empty?

      names.inject(Object) do |constant, name|
        break if constant.nil?

        constant.const_get(name, false) if constant.const_defined?(name, false)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
runger_config-5.2.0 lib/runger/ext/string_constantize.rb
runger_config-5.1.0 lib/runger/ext/string_constantize.rb
runger_config-5.0.0 lib/runger/ext/string_constantize.rb