c++ - 将 C++ 中的结构转换为 Ruby

标签 c++ ruby module struct

我正在尝试转换这段代码:

#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​​ struct 类。如果有人可以解释这些或帮助我转换它,将不胜感激。

最佳答案

我认为这可能值得一看:

C++ - struct vs. class

您的结构是大多数语言(包括 Ruby)所称的类(不是 C 风格的结构):

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

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

关于c++ - 将 C++ 中的结构转换为 Ruby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15885653/

相关文章:

ruby-on-rails - Gem::Ext::BuildError:错误:无法构建 gem native 扩展 Bcrypt-Ruby

c++ - 为什么 WinDbg 无法使用 Service Pack 2 x86 零售符号下载从 Windows Server 2003 加载符号?

c++ - 如何正确编写指针函数声明?

c++ - forward<T> 和 forward<T&&> 一样吗?

javascript - 对象字面量与模块模式

ruby 继承 vs mixins

python - 调用 if __name__ == '__main__' : in one module from a function in another module

c++ - 如何在我的 MEX 文件中使用 OpenMP 而不会导致 MATLAB 崩溃?

ruby - 如何规范退出或中止的方法

ruby - 如何使用 Ruby 提取路由参数