[Ruby] Processing with multiple colons. A class that utilizes the module namespace.

This is a personal memo.

Step by step to understand what a code with multiple colons :: indicates, as shown below.

Aaa :: Bbb :: Ccc :: Ddd.new (argument :)

table of contents

  1. Ddd.new
  2. Ddd.new (argument)
  3. Ccc::Ddd.new
  4. Advantages of defining a class in a module
  5. Aaa::Bbb::Ccc::Ddd.new
  6. Aaa :: Bbb :: Ccc :: Ddd.new (argument)
  7. Pass by specifying the argument name
  8. (Supplement) Define module and class name using ::

Ddd.new First, a simple understanding of `Ddd.new`. This is creating a new instance of the class (Ddd).

This is the process required to call the instance method defined in the class Ddd.

python


#Class definition
class Ddd
  def hello
    p "hello!!!"
  end
end


Ddd.new.hello
#output
"hello!!!"

c = Ddd.new
c.hello
#output
"hello!!!"

You can either execute the method directly on the instance or store the instance in a variable and execute the method.

-Class name.new. Method name


** ▼ If there is no argument **

If no argument is set for the method in the class, it is not necessary to pass the argument with new.

An error will occur if you pass unnecessary arguments.

Error example


Ddd.new.hello("aaa")
ArgumentError (wrong number of arguments (given 1, expected 0))

## Ddd.new (argument) Then if the new method has an argument. The argument of new is passed to the specified class.

It is used when the method defined in the class requires an argument.

python


class Ddd
  def hello2(name)
    p "hello #{name}"
  end
end

Ddd.new.hello2("aaa")
#output
"hello aaa"

Ccc::Ddd.new Then if there is one `::`. The two colons before the class name represent the module name to which the class belongs.

Module name :: class name.new

python


module Ccc
  class Ddd
    def hello3
      p "Ccc::Ddd hello3"
    end
  end
end

Ccc::Ddd.new.hello3
#output
"Ccc::Ddd hello3

Benefits of defining classes within modules

The advantage of writing a module outside the class is that the module functions as a namespace, so you can use the same method name in different namespaces.

** ▼ Example ** If module is used as a namespace, multiple processes with the same class name and the same method name can be described.

python


#####moduleCcc
module Ccc
  class Ddd
    def hello3
      p "Ccc::Ddd hello3"
    end
  end
end

#####moduleZzz
module Zzz
  class Ddd
    def hello3
      p "Zzz::Ddd hello3"
    end
  end
end


##call
Ccc::Ddd.new.hello3
=> "Ccc::Ddd hello3"

Zzz::Ddd.new.hello3
=> "Zzz::Ddd hello3"

If you want to call the class (Ddd) in module Ccc and the process, use Ccc :: Ddd.new. Method name.

Similarly, if you want to call the class (Ddd) and process in module Zzz, use Zzz :: Ddd.new. Method name.


Aaa::Bbb::Ccc::Ddd.new If you embed a class in multiple modules, connect multiple `module names ::` at the time of calling.

python


module Aaa
  module Bbb
    module Ccc
      class Ddd
        def hello
          p "Aaa::Bbb::Ccc::Ddd's hello method"
        end
      end
    end
  end
end

##Method call
Aaa::Bbb::Ccc::Ddd.new.hello
=> "Aaa::Bbb::Ccc.Ddd's hello method"

## Aaa :: Bbb :: Ccc :: Ddd.new (argument) If there is an argument, it is sufficient if the argument is required for the processing in the class.

python


module Aaa
  module Bbb
    module Ccc
      class Ddd
        def hello(name)
          p "hello #{name}Mr. Aaa::Bbb::Ccc::Ddd method"
        end
      end
    end
  end
end

##Method call
Aaa::Bbb::Ccc::Ddd.new.hello("aaa")
=> "hello aaa. Aaa::Bbb::Ccc::Ddd method"

## Pass by specifying the argument name You can use the key argument `key name:` ​​to specify where and what value to pass.

Aaa :: Bbb :: Ccc :: Ddd.new (argument :)

python


module Aaa
  module Bbb
    module Ccc
      class Ddd
        def hello(name1:, name2:)
          p "hello #{name1}With#{name2}Mr."
        end
      end
    end
  end
end

Aaa::Bbb::Ccc::Ddd.new.hello(name2: "zzz", name1: "yyy")
=> "hello yyy and zzz"

If you specify it with a key argument, you can pass the arguments in the same order.


## (Supplement) Define module and class name using :: You can also use `::` when defining module and class names.

For class


class Aaa::Bbb
  def hello(name)
    p "hello #{name} !!!"
  end
end

Aaa::Bbb.new.hello("aaa")
=> "hello aaa !!!"

For module


module Aaa::Yyy
  class Zzz
    def hello(name)
      p "hello #{name} !!!"
    end
  end
end

Xxx::Yyy::Zzz.new.hello("bbb")
=> "hello bbb !!!"

Recommended Posts

[Ruby] Processing with multiple colons. A class that utilizes the module namespace.
Determine that the value is a multiple of 〇 in Ruby
[Ruby / Rails] Set a unique (unique) value in the class
[Ruby] Misunderstanding that I was using the module [Beginner]
A memo that handles a class created independently with ArrayList
What is a Ruby module?
[Ruby] Relationship between parent class and child class. The relationship between a class and an instance.
Create a static file that expands variables using the ERB class
A story that struggled with the introduction of Web Apple Pay
[Illustration] Finding the sum of coins with a recursive function [Ruby]