Class: Symbol

Inherits:
Object show all
Defined in:
lib/qualitysmith_extensions/symbol/match.rb,
lib/qualitysmith_extensions/symbol/constantize.rb

Constant Summary collapse

HasSymbolSupport =
true

Instance Method Summary collapse

Instance Method Details

#=~(regexp) ⇒ Object

Warning: Due to what I think is a bug in Ruby, $1, Regexp.last_match do not yield accurate results when queried after returning from a method call that does a regexp match! If you need access to that data, you’re better off just doing :symbol.to_s =~ /regexp/ yourself. If all you need is a true/false (matches or doesn’t match) result, then you can use this.

:cat =~ /c.t/


28
29
30
# File 'lib/qualitysmith_extensions/symbol/match.rb', line 28

def =~(regexp)
  to_s =~ regexp
end

#constantizeObject

Tries to find a declared constant with the name specified in self.

:Foo.constantize => Foo

Unlike ActiveSupport, we don’t do this check (because Kernel.module “can handle module hierarchy”):

vendor/rails/activesupport/lib/active_support/inflector.rb
  unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
    raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
  end


23
24
25
# File 'lib/qualitysmith_extensions/symbol/constantize.rb', line 23

def constantize
  Kernel.constant(self)
end

#match(regexp) ⇒ Object



18
19
20
# File 'lib/qualitysmith_extensions/symbol/match.rb', line 18

def match(regexp)
  to_s.match(regexp)
end