Does an instance know his own name?

G

Geoff Barnes

If a class has built checks and raises an exception if it finds
something wrong, how can I tell which instance had the error? I know
there's an object_id, but can I know the name of the objects own
instance variable?

huey = Nephew.new
duey = Nephew.new
luey = Nephew.new

duey.raise_error_on_purpose_just_for_fun #-> "Nephew 'duey' raised an
error"

Also, can an instance know his 'path' from the top-level?

class Donald

def initialize
@huey = Nephew.new
@duey = Nephew.new
@luey = Nephew.new
end

end

Donald1 = Donald.new
Donald2 = Donald.new

Donald1.huey.raise_error_on_purpose_just_for_fun
#-> "Nephew Donald1.huey raised an error"

Thanks,
 
J

Jeremy Tregunna

If a class has built checks and raises an exception if it finds
something wrong, how can I tell which instance had the error? I know
there's an object_id, but can I know the name of the objects own
instance variable?

Instances don't have names.
huey = Nephew.new
duey = Nephew.new
luey = Nephew.new

These are examples of binding an instance to a particular key (think
of it in terms of a dictionary/hashtable), and not naming the
instances of Nephew: huey, duey and luey.
 
T

Timothy Goddard

You can identify an object by its object_id. Variables are just
references to an object with a convenient name for your use. If you
want to give an object an actual name, you have to pass it in as a
parameter on construction.
 
L

Logan Capaldo

You can identify an object by its object_id. Variables are just
references to an object with a convenient name for your use. If you
want to give an object an actual name, you have to pass it in as a
parameter on construction.

It is sort of possible to find out someone's name. The following of
course, is easily breakable.
% cat names.rb
class Nephew
def name(&block)
names_objects = eval("local_variables.inject({}){ |h, k| h[k] =
eval(k); h}", block)

names_objects.find { |k, v| v.object_id == object_id }.first
end
end

huey = Nephew.new
duey = Nephew.new
louie = Nephew.new

puts huey.name{}


% ruby names.rb
huey
 
P

Phrogz

Geoff said:
huey = Nephew.new
duey = Nephew.new
luey = Nephew.new

duey.raise_error_on_purpose_just_for_fun #-> "Nephew 'duey' raised an
error"

And what would happen if you did:
huey = Nephew.new
duey = huey
luey = duey
some_array = [ huey ]
huey = nil

duey.raise_error #=> "Nephew...uhm...duey, luey, some_array[ 0
]?...raised an error."

A variable doesn't hold an object, it holds a reference to it. Many
variables (local, global, instance, and class) can all refer to the
same object.

If you have a symbolic name that is unique to an instance, pass that
name in when creating the instance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,211
Messages
2,571,092
Members
47,694
Latest member
digiadvancemarketing

Latest Threads

Top