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
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
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
$ 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
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
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
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".
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 ...
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
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.
-----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.