Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在模块中声明类_Ruby On Rails_Ruby_Oop_Class - Fatal编程技术网

Ruby on rails 在模块中声明类

Ruby on rails 在模块中声明类,ruby-on-rails,ruby,oop,class,Ruby On Rails,Ruby,Oop,Class,测量工作代码(在称为测量员的模块中,不少于),试图理解它。我浏览了这个部分,其中包含一个模块中的类。这与包含模块相同吗?如果没有,这样做的好处是什么?谢谢(加分:为什么我们要在课堂上附加自我,这还没有暗示?) 模块测量器 普通班 RAND|CHARS=[('a'..'z'),('a'..'z'),(0..9)].map{r|r.to_a}.flatten.join 运算符=%w(=!==~) 类)|\n |\t/su',)#删除html标记 col_text.downcase!#删除资本化 co

测量工作代码(在称为测量员的模块中,不少于),试图理解它。我浏览了这个部分,其中包含一个模块中的类。这与包含模块相同吗?如果没有,这样做的好处是什么?谢谢(加分:为什么我们要在课堂上附加自我,这还没有暗示?)

模块测量器
普通班
RAND|CHARS=[('a'..'z'),('a'..'z'),(0..9)].map{r|r.to_a}.flatten.join
运算符=%w(=!=<>=~)
类)|\n |\t/su',)#删除html标记
col_text.downcase!#删除资本化
col_text.gsub!(/\“\'”/u',)\删除潜在的问题字符
col_text.gsub!(/\(.*?\)/u',)#删除列中的文本
col_text.gsub!(/\W/u,,)#删除所有其他非单词字符
cols=(col_text.split(“”)-单词到省略)
(cols.size>5?cols[-5..-1]:cols.join(“”)
结束
def equal_json_不包括通配符(a,b)
如果a.nil?或b.nil,则返回false?
a=a.is_a?(字符串)?JSON.load(a):JSON.load(a.to_JSON)
b=b.is_a?(字符串)?JSON.load(b):JSON.load(b.to_JSON)
深度比较不包括通配符(a,b)
结束
def deep_compare_不包括通配符(a、b)
如果a.class!=b.class,则返回false
如果a.is_a?(散列)
如果a.size!=b.size,则返回false
a、 每个都有| k,v|
如果不包括通配符(v,b[k])==false,则返回false
结束
elsif a.is_a?(数组)
如果a.size!=b.size,则返回false
a、 如果不包括通配符(e,b[i])==false},则带有{u索引{e,i}的每个{u返回false}
其他的
返回(a==“*”)| |(b==“*”)| |(a==b)
结束
真的
结束
别名:normalize:to_normalized_字符串
def生成api id
UUIDTools::UUID.random\u create.to\s
结束
结束
结束
结束
这样做有什么好处

它的作用是使同名的类不会冲突(因此它与mixin无关),这是标准的

为什么我们要将自我附加到类中,这还没有暗示吗

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
这只是方法之一(另一种是
def self.method\u name

这与包含模块相同吗

否。当您有
模块Foo;结束
然后执行

class Bar
  include Foo
end
您将得到一个包含模块
Foo
的所有方法的类
Bar

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

这样做有什么好处

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

为什么我们要将自我附加到类中,这还没有暗示吗

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
不,它还不是“隐含的”。这样做相当于用
self.
定义该块中的每个方法,就像
def self.mymethod;end
一样。参见类