0

I'm trying to convert this code:

#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) {}
};

into Ruby, but I can't figure it out. I've tried using a module, but found out you can't really have constructors in a Module. I don't really understand the ruby struct class either. If somebody could either explain these or help me convert it, it would be much appreciated.

snowe
  • 1,312
  • 1
  • 20
  • 41

1 Answers1

3

I think this may be worth a look:

C++ - struct vs. class

Your struct is what most languages (including Ruby) would call a class (not a C style struct):

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

You need the attr_accessor to make the members public (this being the default behavior of structs in C++).

Community
  • 1
  • 1
badgerious
  • 98
  • 3
  • ok that is what I thought, but it just didn't seem right to be using a class as a struct. I didn't realize that they didn't compare the same way. I also don't understand why I got downvoted, maybe it's because my question was too specific? – snowe Apr 09 '13 at 00:41