Forum: Ruby Accesing the name of an instance from within an method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
47e2d5513caa1db234f3481d2d528838?d=identicon&s=25 Gerald Ebberink (Guest)
on 2007-01-30 08:14
(Received via mailing list)
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%2CEnsche...
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
4d5b5dd4e263d780a5dfe7ac8b8ac98c?d=identicon&s=25 Tim Pease (Guest)
on 2007-01-30 18:15
(Received via mailing list)
On 1/30/07, Gerald Ebberink <g.h.p.ebberink@nclr.nl> wrote:
> end
> Hi I am fun
>
>
> Kind regards,
>
> Gerald Ebberink
>

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
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 unknown (Guest)
on 2007-01-30 18:15
(Received via mailing list)
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
4d5b5dd4e263d780a5dfe7ac8b8ac98c?d=identicon&s=25 Tim Pease (Guest)
on 2007-01-30 18:16
(Received via mailing list)
On 1/30/07, Gerald Ebberink <g.h.p.ebberink@nclr.nl> wrote:
>             puts "Hi I am" + self.name
> end
>
> end
>
> fun = Someclass.new
> fun.say_hi
>

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
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-01-30 18:30
(Received via mailing list)
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 :/
[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[x].say_hi
#=> Hi, I am c[x]

c[x].send( :say_hi )
#=> Hi, I am (unknown)
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-01-30 18:36
(Received via mailing list)
On Jan 30, 10:29 am, "Phrogz" <g...@refinery.com> wrote:
> Let's make it a little shorter and more robust:
[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.
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-01-30 18:48
(Received via mailing list)
On 1/30/07, Gerald Ebberink <g.h.p.ebber...@nclr.nl> wrote:
> fun.say_hi
>
> give the output
>
> Hi I am fun

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?
47e2d5513caa1db234f3481d2d528838?d=identicon&s=25 Gerald Ebberink (Guest)
on 2007-01-31 11:36
(Received via mailing list)
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
47772d54a14ea4150ab47ee81243dcdb?d=identicon&s=25 unknown (Guest)
on 2007-01-31 12:51
(Received via mailing list)
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.
47e2d5513caa1db234f3481d2d528838?d=identicon&s=25 Gerald Ebberink (Guest)
on 2007-01-31 13:25
(Received via mailing list)
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?
4d5b5dd4e263d780a5dfe7ac8b8ac98c?d=identicon&s=25 Tim Pease (Guest)
on 2007-01-31 22:32
(Received via mailing list)
On 1/31/07, Gerald Ebberink <g.h.p.ebberink@nclr.nl> wrote:
> 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.
>

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 :)

Blessings,
TwP
This topic is locked and can not be replied to.