Module: TextTube::CoreExtensions

Included in:
Hash, String
Defined in:
lib/ext/to_constant.rb,
lib/ext/blank.rb

Overview

Core lib extensions reside here.

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


6
7
8
9
10
# File 'lib/ext/blank.rb', line 6

def blank?
  respond_to?(:empty?) ? 
    empty? :
    !self
end

#to_constantObject

to_constant tries to find a declared constant with the name specified in the string. It raises a NameError when the name is not in CamelCase or is not initialized.

Examples

"Module".to_constant #=> Module
"Class".to_constant #=> Class


12
13
14
15
16
17
18
# File 'lib/ext/to_constant.rb', line 12

def to_constant
  unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ self
    fail NameError, "#{self.inspect} is not a valid constant name!"
  end
  
  Object.module_eval("::#{$1}", __FILE__, __LINE__)
end