From: David Masover Date: 2010-12-07T08:07:46+09:00 Subject: Re: Data structures from C to ruby On Monday, December 06, 2010 03:15:55 pm Costas C.j wrote: > Hi. I am new to ruby and i know c and c++. i wonder if thre is something > similar to c pointers for ruby. "Similar", maybe. It depends what you want to do with them. > i think the hash maybe is an option but i want something for general > purpose, example for n foos object or foo to have more than 2 children > and access from one to another. I'm not really sure what you're asking. Are you just trying to build a generic tree? All Ruby variables are handled by reference. A tree might be a bit complex -- let me see if I can clear it up with a linked list. In C, you'd do this, right? struct Node { Node *prev; Node *next; void *data; }; Then you could do things like this: Node *head = malloc(sizeof(Node)); head->prev = head->next = head->data = NULL; Node *tail = head; void push(Node *list, void *data) { Node *n = malloc(sizeof(Node)); n->next = NULL; n->data = data; n->prev = tail; tail = n; } ...and so on. Right? So in Ruby, you could do it with: class Node attr_accessor :prev, :next, :data end They're just references. You can basically do exactly what you'd expect, except you can assume stuff is nulled out by default. So, assuming one dummy node as before, you could do stuff like: class LinkedList def initialize @head = Node.new @tail = head; end def push(data) n = Node.new n.data = data n.prev = @tail @tail = n end ... end Exactly the same. The only difference is you don't have to free stuff. Does that help? If not, could you please post an example of something you can do in C, but you don't know how to do it in Ruby yet?