Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring a class within a module

Surveying work code (in a module called Surveyor, no less), trying to understand it. I ran across this section that contains a class within a module. Is this the same as including the module? If not, what is the advantage of doing it this way? Thanks. (BONUS POINTS: Why are we appending self to class, is that not already implied?)

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
like image 678
steve_gallagher Avatar asked Jun 15 '12 12:06

steve_gallagher


People also ask

Can we write class inside a module?

After importing the package containing this class, the code is compiling and able to use the class variable inside the module.

Can we create class inside module in Ruby?

The objects may be constants, methods, classes, or other modules. Modules have two uses. You can use a module as a convenient way to bundle objects together, or you can incorporate its contents into a class with Ruby's include statement.

Can we define a class inside a class in Ruby?

So, in ruby privates classes can be defined inside a class as a sub-class and declaring them into privates constants, here this private class can be only accessed through the outer-class.

What is the difference between a class and a module?

What is the difference between a class and a module? Modules are collections of methods and constants. They cannot generate instances. Classes may generate instances (objects), and have per-instance state (instance variables).


1 Answers

what is the advantage of doing it this way?

It serves as namespace so classes with the same name do not clash (so it has nothing to do with mixins). It's standard.

Why are we appending self to class, is that not already implied?

That's just one of the ways of defining class-methods (the other being def self.method_name).

like image 56
tokland Avatar answered Oct 17 '22 21:10

tokland