Robert said:
Robert said:
2009/2/18 Aldric Giacomoni <"aldric[removeme]"@trevoke.net>:
I know I could just use rubytree, which looks quite nice, but I'd like
to see what you guys would do about creating a tree and linking the
nodes together.
In C++ we'd just make pointers, so how would we do the equivalent
in Ruby?
We use object references - as always when referring other objects.
Ruby does not have the multitude of options that C++ has.
Alright, Robert - I don't know how that works in Ruby! Would you provide
me with a simple example, explain it, or point me to something that
explains it, please?
I wasn't aware that you were after _such_ basic information.
Actually, since you mentioned using rubytree I assumed that you are
familiar with the language. The simplest and most basic form of a
relation between two objects is probably:
class Foo
def set(x)
@the_other = x
end
end
f = Foo.new
x = Foo.new
f.set(x)
Now f references x.
I suggest you get your hands on David's new book once it is out and in
the meantime consult those various introductory documents (can be
found via
http://www.ruby-doc.org/).
Cheers
robert
Essentially, the main information you require is that, everything in
Ruby is an object, and every object is actually a reference to an object.
so
class Foo
attr_reader :the_other
def set(x)
@the_other = x
end
end
f = Foo.new
bar = f
bar.the_other => nil
x = Foo.new
f.set(x)
bar.the_other => x
It helps me to think of references and the objects they point to as
separate entities (I'm unsure of the truthfulness of this). It's the
opposite of c++ really. c++ requires you to explicitly state you want
pass-by-reference and Ruby requires you to state pass-by-value (which
ends up in the form of a .dup call all the times I've ever wanted to use
it, which is less often than you might think)
Cheers,
Michael
=======================================================================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.
=======================================================================