【问题标题】:Declaring a class within a module在模块中声明一个类
【发布时间】:2012-06-18 13:08:00
【问题描述】:

测量工作代码(在一个名为 Surveyor 的模块中,不少于),试图理解它。我遇到了这个部分,它包含一个模块中的一个类。这与包含模块相同吗?如果不是,这样做有什么好处?谢谢。 (奖励积分:为什么我们将 self 附加到类,这不是已经暗示了吗?)

module Surveyor
  class Common
    RAND_CHARS = [('a'..'z'), ('A'..'Z'), (0..9)].map{|r| r.to_a}.flatten.join
    OPERATORS = %w(== != < > <= >= =~)

    class << self
      def make_tiny_code(len = 10)
        if RUBY_VERSION < "1.8.7"
          (1..len).to_a.map{|i| RAND_CHARS[rand(RAND_CHARS.size), 1] }.join
        else
          len.times.map{|i| RAND_CHARS[rand(RAND_CHARS.size), 1] }.join
        end
      end

      def to_normalized_string(text)
        words_to_omit = %w(a be but has have in is it of on or the to when)
        col_text = text.to_s.gsub(/(<[^>]*>)|\n|\t/su, ' ') # Remove html tags
        col_text.downcase!                            # Remove capitalization
        col_text.gsub!(/\"|\'/u, '')                   # Remove potential problem characters
        col_text.gsub!(/\(.*?\)/u,'')                  # Remove text inside parens
        col_text.gsub!(/\W/u, ' ')                     # Remove all other non-word characters      
        cols = (col_text.split(' ') - words_to_omit)
        (cols.size > 5 ? cols[-5..-1] : cols).join("_")
      end

      def equal_json_excluding_wildcards(a,b)
        return false if a.nil? or b.nil?
        a = a.is_a?(String) ? JSON.load(a) : JSON.load(a.to_json)
        b = b.is_a?(String) ? JSON.load(b) : JSON.load(b.to_json)
        deep_compare_excluding_wildcards(a,b)
      end
      def deep_compare_excluding_wildcards(a,b)
        return false if a.class != b.class
        if a.is_a?(Hash)
          return false if a.size != b.size
          a.each do |k,v|
            return false if deep_compare_excluding_wildcards(v,b[k]) == false
          end
        elsif a.is_a?(Array)
          return false if a.size != b.size
          a.each_with_index{|e,i| return false if deep_compare_excluding_wildcards(e,b[i]) == false }
        else
          return (a == "*") || (b == "*") || (a == b)
        end
        true
      end

      alias :normalize :to_normalized_string

      def generate_api_id
        UUIDTools::UUID.random_create.to_s
      end
    end
  end
end

【问题讨论】:

    标签: ruby-on-rails ruby oop class


    【解决方案1】:

    这样做有什么好处?

    它用作namespace,所以同名的类不会冲突(所以它与mixins无关)。这是标准的。

    为什么我们将 self 附加到类,这不是已经暗示了吗?

    这只是defining class-methods的一种方式(另一种是def self.method_name)。

    【讨论】:

      【解决方案2】:

      这和包含模块一样吗?

      没有。当你有module Foo; end 然后做

      class Bar
        include Foo
      end
      

      您最终会得到一个类Bar,其中包含模块Foo 的所有方法。但是当我们这样做时

      module Foo
        class Bar
        end
      end
      

      我们最终得到一个 Foo::Bar 类,其中不包含 Foo 中不在 Bar 中的任何方法

      这样做有什么好处?

      如果需要,它允许您组织代码。

      为什么我们将 self 附加到类,这不是已经暗示了吗?

      不,它还没有“暗示”。这样做相当于用self. 定义该块中的每个方法,例如def self.mymethod; end。见class << self idiom in Ruby

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-21
        • 2021-11-28
        • 1970-01-01
        • 1970-01-01
        • 2020-02-08
        • 2021-08-29
        • 1970-01-01
        • 2016-11-28
        相关资源
        最近更新 更多