Sha256: c1347b8baab989b7ff00331385cbec9348e6ffa6b2a8ff1697c53f58685b7e0c

Contents?: true

Size: 1.17 KB

Versions: 20

Compression:

Stored size: 1.17 KB

Contents

class String
  # Turn a downcased string and capitalize it
  # so that it can be a class
  # doc_river #=> DocRiver
  def camelcase
    gsub(/(^|_|-)(.)/) { $2.upcase }
  end
  
  # "FooBar".snake_case #=> "foo_bar"
  def snake_case
   gsub(/\B[A-Z]+/, '_\&').downcase
  end
 
  # "FooBar".dasherize #=> "foo-bar"
  def dasherize
    gsub(/\B[A-Z]+/, '-\&').downcase
  end
    
  # Turn a string from lowercased with a .
  # to a classified classname
  # rice_and_beans #=> "RiceAndBeans"
  # handles subclassed and namespaced classes as well
  # for instance
  #   rice::and::beans #=> Rice::And::Beans
  def classify
    self.sub(/.*\./, '').split("::").map {|ele| ele.camelcase }.join("::")
  end
  # Constantize 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".constantize #=> Module
  #   "Class".constantize #=> Class
  def constantize(mod=Object)
    camelcased_word = classify
    begin
      mod.module_eval(camelcased_word, __FILE__, __LINE__)
    rescue NameError
      nil
    end
  end
  
  def /(o)
    File.join(self, o.to_s)
  end
  
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
poolparty-1.6.9 lib/core/string.rb
poolparty-1.6.8 lib/core/string.rb
poolparty-1.6.7 lib/core/string.rb
poolparty-1.6.6 lib/core/string.rb
poolparty-1.6.5 lib/core/string.rb
poolparty-1.6.4 lib/core/string.rb
poolparty-1.6.3 lib/core/string.rb
poolparty-1.6.2 lib/core/string.rb
poolparty-1.6.1 lib/core/string.rb
poolparty-1.6.0 lib/core/string.rb
poolparty-1.5.0 lib/core/string.rb
poolparty-1.4.8 lib/core/string.rb
poolparty-1.4.7 lib/core/string.rb
poolparty-1.4.6 lib/core/string.rb
poolparty-1.4.5 lib/core/string.rb
poolparty-1.4.4 lib/core/string.rb
poolparty-1.4.3 lib/core/string.rb
poolparty-1.4.2 lib/core/string.rb
poolparty-1.4.1 lib/core/string.rb
poolparty-1.4.0 lib/core/string.rb