Ruby Basics

Time:2022-6-21

Create class: class is followed by the class name. The first letter of the class name must be capitalized. When instantiating, use the new method to instantiate. There is a constructor in c# with the same name as the class. In ruby, the constructor is the initialize method. When a class object is instantiated through the new method, the initialize method will be called automatically to pass the parameters in new to the initialize method for initialization. The domain in c\y is called an instance variable in ruby. When defining, it is prefixed with @ to indicate that it is an instance variable.

Copy codeThe codes are as follows:

class Customer
    def initialize(name,age)
      @name,@age=name,age
    end
end
 
c1=Customer.new(“Tom”,20)


Accessor

 

Instance variables can only be accessed through instance methods. If you want to access them, you can provide a method accessor. It is called an attribute in c# and a method is defined to represent an attribute in Java.

Copy codeThe codes are as follows:

class Customer
    def initialize(name,age)
      @name,@age=name,age
    end
    
    def name
      @name
    end
    def age
      @age
    end
end


The above defines two methods X and y, which can read @x and @y instance variables. This defines the read attribute method. If you want to assign values, you should also define the write attribute method:

Copy codeThe codes are as follows:

    def name=(v)
      @name=v
    end
    def age=(v)
      @age=v
    end


Note:

Copy codeThe codes are as follows:

c1=Customer.new(“Tom”,20)
c1.name=”Jhon”


Write attributes can only be used in this way in instances. If you are in a class, you cannot replace @name=value with name=value. But you can use self to do this: self name=value
Ruby provides a simplified method for defining attributes: attr_ Reader and attr_ accessor。 Followed by a symbol, a read-write attribute with the same name will be automatically created.

Copy codeThe codes are as follows:

class Customer
    def initialize(name,age)
      @name,@age=name,age
    end 
 
    attr_reader :name,:age
    attr_accessor:name,:age
end
c1=Customer.new(“Tom”,20)
puts c1.name,c1.age
c1.name=”Jhon”
c1.age=30
puts c1.name,c1.age


Class variable, constant, class method
Class variables start with @ @; Constants are accessed through [class name +:: constant name]; Class method adds the class name before the instance method name. When defining class methods, you can use one method:

Copy codeThe codes are as follows:

class<<self
Def method
       end
end


Then, this method is a class method:

Copy codeThe codes are as follows:

class Customer
    def initialize(name,age)
      @name,@age=name,age
    end
    class <<self
      def showName
        ‘ok’
      end
    end
end
 
puts Customer.showName


Method accessibility
Public: public. By default, the methods in the class are public and can be used anywhere. Constructor initialize is private.
Private: private, used within a class, and can only be called by instance methods of a class or subclass. It can only be called implicitly through self, not through an object display. A private method m can only be called through M, not through O.M or self M call.
Protected: a protected method used within a class or subclass. The difference from private is that in addition to the self implicit call, the call can also be displayed through this kind of object.
You can declare the accessibility of a method by:

Copy codeThe codes are as follows:

#Accessibility private protected public
  private
  def private_method
    puts “private method testing”
  end
  protected
  def protected_method
    puts “protected method testing”
  end
  
  public:protected_method


Factory method
Use the new method private, and then create an instance through the class method

Copy codeThe codes are as follows:

class Fruit
  private_class_method:new
  def Fruit.CreateFruit
    new Fruit
  end
end
f=Fruit.CreateFruit


Module

 

Module is used as a namespace. Calling a class is the same as calling a constant: two colons
Another function is as mixing. Include the instance methods in the module to other classes. The sense function is like the namespace introduction in c\.