#name of instance 
Author Message
 #name of instance

i'm back at trying to get a form to be automagically update from changes
to an associated object. this is the first time i've really started to
hit dead ends with ruby...

is there any way to get the name of an instance variable?

class A
  def myname
    puts self.name  # ?
  end
end

bobo = A.new
bob.myname

producing:

bobo

--
~transami

  _(")_  dobee dobee do...
   \v/  
   ^ ^



Tue, 04 Jan 2005 15:13:04 GMT  
 #name of instance
opps, slight typo. not bob, but bobo:

class A
  def myname
    puts self.name  # ?
  end
end

bobo = A.new
bobo.myname   # OOPS

producing:

bobo

Quote:

> i'm back at trying to get a form to be automagically update from changes
> to an associated object. this is the first time i've really started to
> hit dead ends with ruby...

> is there any way to get the name of an instance variable?

> class A
>   def myname
>     puts self.name  # ?
>   end
> end

> bobo = A.new
> bob.myname

> producing:

> bobo

> --
> ~transami

>   _(")_  dobee dobee do...
>    \v/  
>    ^ ^

--
~transami

  _(")_  dobee dobee do...
   \v/  
   ^ ^



Tue, 04 Jan 2005 15:28:41 GMT  
 #name of instance

Quote:

> i'm back at trying to get a form to be automagically update from changes
> to an associated object. this is the first time i've really started to
> hit dead ends with ruby...

> is there any way to get the name of an instance variable?

What you appear to be asking for, is the name of the local variable
(bobo) that holds the reference to the object on which the method is
being called. I do not think this is possible, since it could be that
there wasn't a local variable holding a reference when you called it
(such as if you called A.new.myname).

Quote:
> class A
>   def myname
>     puts self.name  # ?
>   end
> end

> bobo = A.new
> bob.myname

Even with the name of a local variable in the outside scope, you'll need
a binding to that scope to access it. A little more code showing what
you want to do with the name of the local variable would probably spawn
more concrete, useful answers than this one.

--
(\[ Kent Dahl ]/)_    _~_    __[ http://www.stud.ntnu.no/~kentda/ ]___/~
 ))\_student_/((  \__d L b__/  NTNU - graduate engineering - 4. year  )
( \__\_?|?_/__/ ) _)Industrial economics and technological management(
 \____/_?_\____/ (____engineering.discipline_=_Computer::Technology___)



Tue, 04 Jan 2005 15:43:12 GMT  
 #name of instance

T> bobo = A.new
T> bob.myname

 If I write this

  baba = bobo = A.new

 What is the "name" of the instance ?

 Same question with

  A.new.myname

Guy Decoux



Tue, 04 Jan 2005 15:44:17 GMT  
 #name of instance

Quote:
----- Original Message -----


Sent: Friday, July 19, 2002 2:23 AM
Subject: Re: #name of instance

> opps, slight typo. not bob, but bobo:

> class A
>   def myname
>     puts self.name  # ?
>   end
> end

> bobo = A.new
> bobo.myname   # OOPS

> producing:

> bobo

Without some tremendous gyrations,
it's just not possible.

Remember that a variable is just a
reference to an object.

First of all, an object has no way
of knowing what variable might
reference it.

Second, there could be any number
of variables referencing the same
object.

  toto = gogo = bobo = A.new
  toto.myname  # Logically, all three
  gogo.myname  # of these must produce
  bobo.myname  # the same result.

Thirdly, there might be no variable
at all referencing the object.

  A.new.myname # What now?

So unless you're prepared to do some
weird things to associate a name with
an object when it's created, there's
just no way. Even if you *do* that,
there are still shortcomings.

Perhaps time to rethink.

If you're not familiar with how Symbols
work in Ruby, go look that up. That
might give you some ideas.

Cheers,
Hal



Tue, 04 Jan 2005 15:44:17 GMT  
 #name of instance
good points, quite right. your notions did make me rethink and...

i think i found a solution. instead of assocciating a widget to the name
of a variable, as i had been thinking, instead i must associate it to
the object itself. thus instead of the name, i will simply use the id.

thanks guys!

~transami

Quote:

> ----- Original Message -----


> Sent: Friday, July 19, 2002 2:23 AM
> Subject: Re: #name of instance

> > opps, slight typo. not bob, but bobo:

> > class A
> >   def myname
> >     puts self.name  # ?
> >   end
> > end

> > bobo = A.new
> > bobo.myname   # OOPS

> > producing:

> > bobo

> Without some tremendous gyrations,
> it's just not possible.

> Remember that a variable is just a
> reference to an object.

> First of all, an object has no way
> of knowing what variable might
> reference it.

> Second, there could be any number
> of variables referencing the same
> object.

>   toto = gogo = bobo = A.new
>   toto.myname  # Logically, all three
>   gogo.myname  # of these must produce
>   bobo.myname  # the same result.

> Thirdly, there might be no variable
> at all referencing the object.

>   A.new.myname # What now?

> So unless you're prepared to do some
> weird things to associate a name with
> an object when it's created, there's
> just no way. Even if you *do* that,
> there are still shortcomings.

> Perhaps time to rethink.

> If you're not familiar with how Symbols
> work in Ruby, go look that up. That
> might give you some ideas.

> Cheers,
> Hal

--
~transami

  _(")_  dobee dobee do...
   \v/  
   ^ ^



Tue, 04 Jan 2005 15:54:41 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. naming DesignWare instances

2. Naming class instances in real time

3. Determining names of instances

4. I'm sorry, name of instance inside its methods

5. instances of classes and instances of instances

6. How to name instance variables

7. Changing Instance Names

8. Singleton methods and instance methods with same name?

9. instance variables by name?

10. verilog instance name length

11. escaped instance names

12. help: escaped instance names

 

 
Powered by phpBB® Forum Software