Accesing the name of an instance from within an method

Hi all,

I am wondering if it is possible to access the name of an instance from
within a method.

What I would like to do is something similar to this

class Someclass

def say_hi

            puts "Hi I am" + self.name

end

end

fun = Someclass.new

fun.say_hi

give the output

Hi I am fun

Kind regards,

Gerald Ebberink

Gerald Ebberink
Laser Technician

<http://www.nclr.nl> NCLR B.V.

<http://maps.google.com/maps?q=PO+box+2662%2CEnschede+7500CR%2CThe+Netherlan
ds&hl=en> PO box 2662
Enschede 7500CR
The Netherlands

Work: 31 53 4891110
Direct: 31 53 4893961
Fax: 31 53 4891102
Email: <mailto:g.h.p.ebberink@nclr.nl> g.h.p.ebberink@nclr.nl

http://www.linkedin.com/in/geraldebberink

My gut reaction is to say "no, you can't do that." The say_hi method
does not know about the context of the caller and the local variables
available there.

However, take a look at Ryan Davis' ParseTree and Ruby2Ruby gems.
These allow a program to analyze its own source code. That might be
one direction to look into.

So, a reserved "maybe" is probably the best answer :wink:

Ryan or Eric, any thoughts on this one?

Blessings,
TwP

···

On 1/30/07, Gerald Ebberink <g.h.p.ebberink@nclr.nl> wrote:

Hi all,

I am wondering if it is possible to access the name of an instance from within a method.
What I would like to do is something similar to this

class Someclass

def say_hi
  puts "Hi I am" + self.name
end

end

fun = Someclass.new

fun.say_hi

give the output

Hi I am fun

Kind regards,

Gerald Ebberink

I'm going to invoke the "What are you trying to do?" rule.

If you give this group the bigger picture of what you are trying to accomplish
I'm sure we'll give you any number of options other than "you can't do that in Ruby".

Gary Wright

···

On Jan 30, 2007, at 2:13 AM, Gerald Ebberink wrote:

I am wondering if it is possible to access the name of an instance from within a method.

This is an utter kludge and I would not recommend using it in any
production code, but here it is ... oh, and it will only work for this
very specific case :confused:

$ cat tmp.rb

class A
  def say_hi
    m = %r/([^:]+):(\d+)/.match caller.first
    return if m.nil?

    fn = m[1]
    num = Integer(m[2])
    line = nil

    File.open(fn,'r') do |fd|
      cnt = 0
      until fd.eof?
        cnt += 1
        l = fd.readline
        if cnt >= num
          line = l
          break
        end
      end
    end

    m = %r/(\w+)\.say_hi/.match line
    puts "Hi, I am #{m[1]}" unless m.nil?
  end
end

a = A.new
a.say_hi

b = A.new
b.say_hi

$ ruby tmp.rb
Hi, I am a
Hi, I am b

Blessings,
TwP

···

On 1/30/07, Gerald Ebberink <g.h.p.ebberink@nclr.nl> wrote:

Hi all,

I am wondering if it is possible to access the name of an instance from within a method.
What I would like to do is something similar to this

class Someclass

def say_hi
            puts "Hi I am" + self.name
end

end

fun = Someclass.new
fun.say_hi

Instances do not have names; what you have above is a variable (named
'fun') that references your instance. For example, consider this:

my_array = [ Someclass.new ]
wow = my_array[ 0 ]
my_hash = { :foo=>my_array[0] }

wow.say_hi
my_array[ 0 ].say_hi
hy_hash[ :foo ].say_hi

What would you expect that method to output?

···

On 1/30/07, Gerald Ebberink <g.h.p.ebber...@nclr.nl> wrote:

I am wondering if it is possible to access the name of an instance
from within a method.
What I would like to do is something similar to this
class Someclass
def say_hi
  puts "Hi I am" + self.name
end
end

fun = Someclass.new
fun.say_hi

give the output

Hi I am fun

[snip]

Very tricky!

Let's make it a little shorter and more robust:
class A
  def say_hi
    m = %r/([^:]+):(\d+)/.match caller.first
    return if m.nil?

    line = IO.readlines( m[1] )[ m[2].to_i - 1 ]
    return if line.nil?

    puts "Hi, I am #{line[/\S+(?=.say_hi)/] || '(unknown)'}"
  end
end

a = A.new
b = a
c = [ a ]
d = { :foo=>c[0] }

a.say_hi
#=> Hi, I am a

b.say_hi
#=> Hi, I am b

c[0].say_hi
#=> Hi, I am c[0]

d[:foo].say_hi
#=> Hi, I am d[:foo]

x=0
y = c.say_hi
#=> Hi, I am c

c.send( :say_hi )
#=> Hi, I am (unknown)

···

On Jan 30, 10:15 am, "Tim Pease" <tim.pe...@gmail.com> wrote:

This is an utter kludge and I would not recommend using it in any
production code, but here it is ... oh, and it will only work for this
very specific case :confused:

Well here comes the big picture. I have a class contains some data and
amongst other things can generate graphs. What I would like to do is to
place these graphs in a bunch of files which have unique names (otherwise I
end up with always the last graph). Since only the variable (and the data)
are different amongst the instances I wonder how I could give them meaning
full names like "20070131 var1 XY.jpg" "20070131 var1 Both.jpg " etc.

Now I did think about giving the var as an option while creating the
instance, but that doesn't help since it is perfectly possible in the code
that variable from which the instance is referenced is changed.

Now I come to think of it I could try to overload the = operater to let some
internal variable be changed when the reference is changed but then I still
need the name of the variable.

Kind regards
Gerald Ebberink

···

-----Original Message-----
From: gwtmp01@mac.com [mailto:gwtmp01@mac.com]
Sent: dinsdag 30 januari 2007 17:51
To: ruby-talk ML
Subject: Re: Accesing the name of an instance from within an method

On Jan 30, 2007, at 2:13 AM, Gerald Ebberink wrote:

I am wondering if it is possible to access the name of an instance
from within a method.

I'm going to invoke the "What are you trying to do?" rule.

If you give this group the bigger picture of what you are trying to
accomplish
I'm sure we'll give you any number of options other than "you can't
do that in Ruby".

Gary Wright

[snip]

Oops, but my version has this result:

d[ :foo ].say_hi
#=> Hi, I am ]

Of course, the whole thing is reasonably meaningless, so it's not
surprising that this would break.

···

On Jan 30, 10:29 am, "Phrogz" <g...@refinery.com> wrote:

Let's make it a little shorter and more robust:

You can't overload the assignment operator. It's not a method. See
page 324 of the pickaxe.

···

On Jan 31, 11:35 am, "Gerald Ebberink" <g.h.p.ebber...@nclr.nl> wrote:

Now I come to think of it I could try to overload the = operater to let some
internal variable be changed when the reference is changed but then I still
need the name of the variable.

When you create an instance of this class that contains all the data,
give it a base filename to use when creating and saving off graphs.
Use an incrementing counter to make sure all the filenames are unique.

If you want to get fancy, you can look at the directory where the file
is to be created ...

File.dirname('path/to/data/filename.graph') #=> 'path/to/data'

and see if there are already files named 'filename.1.graph' etc. That
will give you the number to start using to make sure you don't
overwrite older graphs from previous runs.

Just my thoughts on that one :slight_smile:

Blessings,
TwP

···

On 1/31/07, Gerald Ebberink <g.h.p.ebberink@nclr.nl> wrote:

Well here comes the big picture. I have a class contains some data and
amongst other things can generate graphs. What I would like to do is to
place these graphs in a bunch of files which have unique names (otherwise I
end up with always the last graph). Since only the variable (and the data)
are different amongst the instances I wonder how I could give them meaning
full names like "20070131 var1 XY.jpg" "20070131 var1 Both.jpg " etc.

Now I did think about giving the var as an option while creating the
instance, but that doesn't help since it is perfectly possible in the code
that variable from which the instance is referenced is changed.

Now I come to think of it I could try to overload the = operater to let some
internal variable be changed when the reference is changed but then I still
need the name of the variable.

Sorry I'm getting my langanges mixed up.

Gerald Ebberink
Laser Technician

NCLR B.V.
PO box 2662
Enschede 7500CR
The Netherlands
  Work: 31 53 4891110
Direct: 31 53 4893961
Fax: 31 53 4891102
Email: g.h.p.ebberink@nclr.nl

http://www.linkedin.com/in/geraldebberink

    Want a signature like this?

···

-----Original Message-----
From: burn.redmond.burn@gmail.com [mailto:burn.redmond.burn@gmail.com]
Sent: woensdag 31 januari 2007 12:50
To: ruby-talk ML
Subject: Re: Accesing the name of an instance from within an method

On Jan 31, 11:35 am, "Gerald Ebberink" <g.h.p.ebber...@nclr.nl> wrote:

Now I come to think of it I could try to overload the = operater to let

some

internal variable be changed when the reference is changed but then I

still

need the name of the variable.

You can't overload the assignment operator. It's not a method. See
page 324 of the pickaxe.