将c++中的结构体转换为Ruby

Converting struct in C++ to Ruby

本文关键字:转换 Ruby 结构体 c++      更新时间:2023-10-16

我正在尝试转换此代码:

#pragma once
#include "thread.h"
#include <vector>
struct Process {
  enum Type {
    SYSTEM,
    USER
  };
  // process ID
  int pid;
  // process type
  Type type;
  // threads belonging to this process
  std::vector<Thread*> threads;
  // constructor
  Process(int pid, Type type) : pid(pid), type(type) {}
};

转换成Ruby,但我不明白。我尝试过使用模块,但发现你不能在模块中真正拥有构造函数。我也不太懂ruby结构类。如果有人能解释一下或者帮我转换一下,我将不胜感激。

我认为这可能值得一看:

c++ -结构与类

你的结构是大多数语言(包括Ruby)会调用的类(不是C风格的结构):

class Process
  def initialize(pid, type)
    @type = type
    @pid = pid
    @threads = []
  end
  attr_accessor :type, :pid, :threads
end

您需要attr_accessor使成员公开(这是c++中结构体的默认行为)