A question about pointing from one object to many other objects

Hello all,

I’m completely new to Ruby and considering programming my next project
using Ruby. It’s been a while since I’ve programmed, and I’m rusty on
simple concepts as well.

My project will basically have objects that are english words, and these
objects will be related to other objects (other english words).

For example, suppose I have an object called “tree”.
Then a tree can be tall, green, leafy and have even more
characteristics.

So I want the object “tree” to have pointers to the objects “tall”,
“green” and “leafy”. Please assume that the objects “tall”, “green” and
“leafy” already exist.

Also, I don’t know how many characteristics that “tree” may have, and I
want to able to create more pointers to other objects while the program
is running e.g. point to “woody”, “oak”, “pine” etc.

I use the word pointers loosely.
This could be a link, a reference, etc.

How could I accomplish something like this?

thank you

You’d have to create the objects and hold the references within the
object. I’d suggest an Array or Hash to hold the groups.
For example:

object_tree is an instance of class Word which holds @attributes, which
is an Array containing the Words
object_tall, object_green, and object_leafy, each with their own
attributes.

Learn a bit more about object oriented programming, which is a paradigm
and will help you to create systems in any language.

Brajesh KS wrote in post #1160133:

Hello all,
ple concepts as well.

My project will basically have objects that are english words, and these
objects will be related to other objects (other english words).

How could I accomplish something like this?

thank you

class Stuff
attr_reader :id
def intialize(id)
@links= Hash.new {|h,type| h[type]={} }
self.id=id
end
def add(role,object)
@links[role.to_symb][object.id]=object
end
end

a=Stuff.new(“a”) ;b=Stuff.new(“b”) ;c=Stuff.new(“c”)
d=Stuff.new(“d”)

a.add(:parent,b)
b.add(:child,a)
c.add(:parent,a)
d.add(:maried,a)

Regis d’Aubarede wrote in post #1160392:

class Stuff
attr_reader :id
def intialize(id)
@links= Hash.new {|h,type| h[type]={} }
self.id=id
end
def add(role,object)
@links[role.to_symb][object.id]=object
end
end

a=Stuff.new(“a”) ;b=Stuff.new(“b”) ;c=Stuff.new(“c”)
d=Stuff.new(“d”)

a.add(:parent,b)
b.add(:child,a)
c.add(:parent,a)
d.add(:maried,a)

Hi Regis,
Please could you explain this a bit further,
Thank you

Brajesh KS wrote in post #1160408:

every object of my model will inherit of class Stuff

class Stuff

end

a=Stuff.new(…)

Each object will have a list of associations ( “links” ),
each association will have a name. this dictionary of associations will
be build on constructor :

class Stuff
def intialize(id)
@links= Hash.new
@id=id
end

end

I suppose that each entity will have a unique identity( ‘id’ ) ,
so i declare a accessor for reading access of the id of one entity,
and give this id at construction

class Stuff
attr_reader :id
def intialize(id)

self.id=id
end

end

a= Stuff.new(900984324)
a.id
-> 900984324

Then each each association will contain a list of object which is
‘linked’ to my entity. I choose to use a Hash for this list. so at
construction, each association must point to a empty hash when the
association will
be created:

class Stuff
attr_reader :id
def intialize(id)
@links= Hash.new {|h,role| h[role]={} }
self.id=id
end

end

Now we can add a method for associate a object to my stuff.
I give the name of the association, and the object to be linked :

class Stuff

def add(role,object)
@links[role][object.id]=object
end
end

a=Stuff.new(3234324)
b=Stuff.new(5243334)

a.add(:parent,b)

So in object a, @links[:parent] while contain a reference of b object
with his id : @links[:parent]={ 5243334 => b }

Then you can append some methods in Stuff for doing some
algorithm.
Suppose your model represent a family structure,
let create a sub-class of Stuff, with some algorithm specific to
family model :

class Person < Stuff
def children() @links[:child].values end
def parent() @links[:parent].values end
def check_family()
self.parent.each { |par|
raise “Incoherence” unless par.children.keys?(self.id)
}
self.children.values.each { |child| child.check_family }
end
end

a=Person.new(“John”)
b=Person.new(“Mary”)
b.add(:parent,a)
a.add(:child,b)
a.check_family

NOTA : by using Hash for association, the check is more efficient
( Hash member search) than if i had use a Array ( link.find ),
and i can have the array of object with link.values()

… pointer …

in Ruby, a variable are a reference (handle) of an object.
you can’t have direct access of data structure if the object.

So a variable is always a ‘pointer’ to an object :

  • Stuff is a const variable which reference a class object
  • in c=22, c reference a Integer variable which is immutable
    (22 is always 22, it can’t be 23 !)
    so c=22
    c=c+1 ; c referene a new Integer instance, which value is
    the value of 22 object and the value of 1 object.
    22 is not refeeced, it will be garbage collected…

Sorry for my english :slight_smile: