⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 core_ext.rb

📁 敏捷开发 ruby on rails 实现的一个购物车系统
💻 RB
字号:
require 'will_paginate'require 'set'unless Hash.instance_methods.include? 'except'  Hash.class_eval do    # Returns a new hash without the given keys.    def except(*keys)      rejected = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys)      reject { |key,| rejected.include?(key) }    end     # Replaces the hash without only the given keys.    def except!(*keys)      replace(except(*keys))    end  endendunless Hash.instance_methods.include? 'slice'  Hash.class_eval do    # Returns a new hash with only the given keys.    def slice(*keys)      allowed = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys)      reject { |key,| !allowed.include?(key) }    end    # Replaces the hash with only the given keys.    def slice!(*keys)      replace(slice(*keys))    end  endendunless Hash.instance_methods.include? 'rec_merge!'  Hash.class_eval do    # Same as Hash#merge!, but recursively merges sub-hashes    # (stolen from Haml)    def rec_merge!(other)      other.each do |key, other_value|        value = self[key]        if value.is_a?(Hash) and other_value.is_a?(Hash)          value.rec_merge! other_value        else          self[key] = other_value        end      end      self    end  endendrequire 'will_paginate/collection'unless Array.instance_methods.include? 'paginate'  # http://www.desimcadam.com/archives/8  Array.class_eval do    def paginate(options_or_page = {}, per_page = nil)      if options_or_page.nil? or Fixnum === options_or_page        if defined? WillPaginate::Deprecation          WillPaginate::Deprecation.warn <<-DEPR            Array#paginate now conforms to the main, ActiveRecord::Base#paginate API.  You should \            call it with a parameters hash (:page, :per_page).  The old API (numbers as arguments) \            has been deprecated and is going to be unsupported in future versions of will_paginate.          DEPR        end        page = options_or_page        options = {}      else        options = options_or_page        page = options[:page]        raise ArgumentError, "wrong number of arguments (1 hash or 2 Fixnums expected)" if per_page        per_page = options[:per_page]      end      WillPaginate::Collection.create(page || 1, per_page || 30, options[:total_entries] || size) do |pager|        pager.replace self[pager.offset, pager.per_page].to_a      end    end  endend

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -