Class: String

Inherits:
Object show all
Defined in:
lib/core_ext/string.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym) ⇒ Object

this is truly a magnificent core extension it let’s you do stuff like “foo”.foo? # => true TODO: fuzzy matching for underscores/spaces

Raises:

  • (NoMethodError)


14
15
16
17
18
19
20
# File 'lib/core_ext/string.rb', line 14

def method_missing sym
  raise NoMethodError.new("Undefined method #{sym} for String") unless sym.to_s.end_with? "?"
  this = self.downcase.gsub("_"," ")
  that = sym.to_s.clip # remove ?
  that = that.downcase.gsub("_"," ")
  this == that
end

Instance Method Details

#clip(n = 1) ⇒ Object

remove last (or more) chars from string



3
4
5
# File 'lib/core_ext/string.rb', line 3

def clip n=1
  self[0..-n-1]
end

#clip!(n = 1) ⇒ Object



7
8
9
# File 'lib/core_ext/string.rb', line 7

def clip! n=1
  self.replace(clip n)
end

#to_procObject

this handy little helper evals code the OO way :)



23
24
25
# File 'lib/core_ext/string.rb', line 23

def to_proc
  eval "Proc.new do\n#{self}\nend"
end