Sha256: 6b9cde911635381c235de97aacd6f68ffecc01076a36a002369ebafb6953d0b8

Contents?: true

Size: 1.13 KB

Versions: 32

Compression:

Stored size: 1.13 KB

Contents

#
# Additions to core Ruby objects
#

class String

  unless String.method_defined?(:camel_case)
    #
    # Turn an underscored string into camel case, ie this_becomes -> ThisBecomes
    #
    def camel_case
      self.split("_").inject("") { |str, piece| str + piece.capitalize }
    end
  end

  unless String.method_defined?(:to_class)
    #
    # Treat a string as a class name and return the class. Optionally provide a
    # module to look up the class in.
    #
    def to_class(mod = nil)
      res = "#{mod}::#{self}".sub(/^::/, "").split("::").inject(Object) do |mod, obj|
        raise "No such class/module" unless mod.const_defined?(obj)
        mod = mod.const_get(obj)
      end
    end
  end

  unless String.method_defined?(:classify)
    #
    # Turn a string in either underscore or camel case form into a class directly
    #
    def classify(mod = nil)
      self.camel_case.to_class(mod)
    end
  end

  unless String.method_defined?(:underscore)
    #
    # Turn a camelcase string into underscore string
    #
    def underscore
      self.split(/([A-Z][^A-Z]*)/).find_all { |str| str != "" }.join("_").downcase
    end
  end
end

Version data entries

32 entries across 32 versions & 3 rubygems

Version Path
juicer-1.2.0 lib/juicer/ext/string.rb
juicer-1.1.2 lib/juicer/ext/string.rb
juicer-1.1.1 lib/juicer/ext/string.rb
juicer-1.1.0 lib/juicer/ext/string.rb
juicer-1.0.22 lib/juicer/ext/string.rb
juicer-1.0.21 lib/juicer/ext/string.rb
juicer-1.0.20 lib/juicer/ext/string.rb
juicer-1.0.19 lib/juicer/ext/string.rb
juicer-1.0.18 lib/juicer/ext/string.rb
juicer-1.0.17 lib/juicer/ext/string.rb
juicer-1.0.16 lib/juicer/ext/string.rb
juicer-1.0.15 lib/juicer/ext/string.rb
juicer-1.0.14 lib/juicer/ext/string.rb
juicer-1.0.13 lib/juicer/ext/string.rb
psyho_juicer-1.0.11 lib/juicer/ext/string.rb
juicer-1.0.12 lib/juicer/ext/string.rb
juicer-1.0.11 lib/juicer/ext/string.rb
juicer-1.0.10 lib/juicer/ext/string.rb
psyho_juicer-1.0.9 lib/juicer/ext/string.rb
juicer-1.0.9 lib/juicer/ext/string.rb