SoFunction
Updated on 2025-04-09

Ruby's simple study notes (II): class inheritance, attributes, class variables

1. How to declare a subclass

Copy the codeThe code is as follows:

class Treasure < Thing

In this way, the attribute name and description in the Thing class are inherited by Treasure

2. What are the parameters of the parent class initialize method in the following three ways?

Copy the codeThe code is as follows:

# This passes a, b, c to the superclass
def initialize( a, b, c, d, e, f )
super( a, b, c )
end
# This passes a, b, c to the superclass

def initialize( a, b, c )
super
end
# This passes no arguments to the superclass
def initialize( a, b, c)
super()
end

The first is to pass a, b, and c in the parameters into the initialize method of the parent class; the second is to pass all parameters, but not brackets; the third is to pass parameters without passing them in

3. Setter/getter of attributes

Someone writes setter/getter like this:

Copy the codeThe code is as follows:

puts( t1.get_description )
t1.set_description( “Some description” )

This seems to be more convenient:
Copy the codeThe code is as follows:

puts( )
= “Some description”

If you want to use the second way of writing, you have to write it like this:

Note: This is correct: def name=(aName)

But this is wrong: def name =(aName)

Have you seen the difference?

According to the previous chapter, you can know that there are two methods defined here: description method and description= method. It turns out that it was implemented by adding "=" to the method name. Ruby is really amazing, so Java cannot write this way.

However, there are actually easier ways to implement setter/getter

Copy the codeThe code is as follows:

attr_reader :description
attr_writer :description

It consists of an attr_reader/attr_writer plus a symbol (:description). In fact, you can set a setter/getter for multiple elements at once.
Copy the codeThe code is as follows:

attr_writer(:name, :description)
attr_accessor(:value, :id, :owner)
attr_accessor

Equivalent to:
Copy the codeThe code is as follows:

attr_reader :value
attr_writer :value

Unlike Java, the super method in Ruby can appear in any method, not just initialize (constructor).

In point 2, the use of the super method is introduced. A separate super passes all parameters to the parent class initialize, while a super with parameters passes the specified parameters to the parent class initialize.

Copy the codeThe code is as follows:

# This passes aName, aDescription to the superclass
def initialize( aName,aDescription )
super( aName, aDescription )
end

# This passes a, b, c to the superclass's aMethod
def aMethod( a, b, c )
super
end

5. Constants & nested classes

Copy the codeThe code is as follows:

class X
 A = 10
 
 class Y
  def xyz
   puts( "goodbye" )
  end
 end
 
 def
  puts("hello")
 end
end

Constant: variables that start with capital letters.

If you want to access constants or internal classes, you need to use ::

Copy the codeThe code is as follows:

puts( X::A )
X::abc       # You can also use :: to call methods
# Of course this is OK

ob = X::

6. Partial Class

In Ruby, existing classes can be modified and affected by the generated objects

Copy the codeThe code is as follows:

class A
  def a
    puts 'a'
  end
end

a =
a.public_methods(false)//Print A class all public methods
# => [:a] //Only a

class A
  def b
    puts 'b'
  end
end

a.public_methods(false)
# => [:a, :b]//There are a and b

What cannot be modified is which class inherits. for example

Copy the codeThe code is as follows:

class A
  def a
    puts 'a'
  end
end

class A < String
  def c
    puts 'c'
  end
end

# TypeError: superclass mismatch for class A
# All classes inherit the Object class by default, and A also inherits the Object, so when you ask A to inherit String, you will report an error