Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.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 类定义上的Struct.new_Ruby - Fatal编程技术网

Ruby 类定义上的Struct.new

Ruby 类定义上的Struct.new,ruby,Ruby,我今天遇到了 class meetingmemberjob

我今天遇到了

class meetingmemberjob

的目的是什么?

Struct是一个ruby类,它创建了一个包含属性和访问器的类对象,您不需要显式定义类。在api中,您可以找到更多详细信息:


在您的例子中,它创建了一个包含两个名为“user”和“meeting”的属性的类,然后类meetingmemberjob继承了它。

Struct是一个ruby类,它创建了一个包含属性和访问器的类对象,您不需要显式定义类。在api中,您可以找到更多详细信息:


在您的例子中,它创建了一个包含两个属性的类,分别名为“user”和“meeting”,然后类meetingmemberjob继承了它。

下面是另一个示例:

class Animal
  def greet
    puts "Hi.  I'm an animal"
  end
end

def get_class
  return Animal
end

class Dog < get_class
  def warn
    puts "Woof."
  end
end

Dog.new.greet
Dog.new.warn

--output:--
Hi.  I'm an animal
Woof.
类动物
def问候
写上“嗨,我是动物”
结束
结束
def get_类
返回动物
结束
类狗
还有一个:

class Dog < Class.new { def greet; puts "Hi"; end }
  def warn
    puts "Woof."
  end
end

Dog.new.greet
Dog.new.warn


--output:--
Hi
Woof.
class-Dog
下面是另一个例子:

class Animal
  def greet
    puts "Hi.  I'm an animal"
  end
end

def get_class
  return Animal
end

class Dog < get_class
  def warn
    puts "Woof."
  end
end

Dog.new.greet
Dog.new.warn

--output:--
Hi.  I'm an animal
Woof.
类动物
def问候
写上“嗨,我是动物”
结束
结束
def get_类
返回动物
结束
类狗
还有一个:

class Dog < Class.new { def greet; puts "Hi"; end }
  def warn
    puts "Woof."
  end
end

Dog.new.greet
Dog.new.warn


--output:--
Hi
Woof.
class-Dog
太棒了,多么棒的语言!太棒了,多么好的语言!