Sha256: 5365e449f4819c9b1fb28ed87440d6a61080e0a4d83feae273d912ba9898c986

Contents?: true

Size: 1.23 KB

Versions: 11

Compression:

Stored size: 1.23 KB

Contents

class String
  def to_numbers(replace='#')
    gsub(/#{replace}/){ Kernel.rand(10) }
  end

  # Ripped right out of rails
  if !defined?(RAILS_ROOT)
    def camelize(first_letter = :upper)
      case first_letter
      when :upper
        to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
      when :lower
        first.downcase + camelize(self)[1..-1]
      end
    end

    def underscore
      to_s.gsub(/::/, '/').
        gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
        gsub(/([a-z\d])([A-Z])/,'\1_\2').
        tr("-", "_").
        downcase
    end

    if Module.method(:const_get).arity == 1
      def constantize
        names = self.split('::')
        names.shift if names.empty? || names.first.empty?

        constant = Object
        names.each do |name|
          constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
        end
        constant
      end
    else
      def constantize
        names = self.split('::')
        names.shift if names.empty? || names.first.empty?

        constant = Object
        names.each do |name|
          constant = constant.const_get(name, false) || constant.const_missing(name)
        end
        constant
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
forgery-0.3.11 lib/forgery/extensions/string.rb
forgery-0.3.10 lib/forgery/extensions/string.rb
forgery-0.3.9 lib/forgery/extensions/string.rb
forgery-0.3.8 lib/forgery/extensions/string.rb
forgery-0.3.7 lib/forgery/extensions/string.rb
forgery-0.3.6 lib/forgery/extensions/string.rb
forgery-0.3.5 lib/forgery/extensions/string.rb
forgery-0.3.4 lib/forgery/extensions/string.rb
forgery-0.3.3 lib/forgery/extensions/string.rb
forgery-0.3.2 lib/forgery/extensions/string.rb
forgery-0.3.1 lib/forgery/extensions/string.rb