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.
Gerald E. (Guest)
on 2007-01-30 09: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 E.







Gerald E.
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:removed_email_address@domain.invalid> 
removed_email_address@domain.invalid

http://www.linkedin.com/in/geraldebberink
Tim P. (Guest)
on 2007-01-30 19:15
(Received via mailing list)
On 1/30/07, Gerald E. <removed_email_address@domain.invalid> wrote:
> end
> Hi I am fun
>
>
> Kind regards,
>
> Gerald E.
>

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 D.' 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
unknown (Guest)
on 2007-01-30 19:15
(Received via mailing list)
On Jan 30, 2007, at 2:13 AM, Gerald E. 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 W.
Tim P. (Guest)
on 2007-01-30 19:16
(Received via mailing list)
On 1/30/07, Gerald E. <removed_email_address@domain.invalid> 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
Gavin K. (Guest)
on 2007-01-30 19:30
(Received via mailing list)
On Jan 30, 10:15 am, "Tim P." <removed_email_address@domain.invalid> 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)
Gavin K. (Guest)
on 2007-01-30 19:36
(Received via mailing list)
On Jan 30, 10:29 am, "Phrogz" <removed_email_address@domain.invalid> 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.
Gavin K. (Guest)
on 2007-01-30 19:48
(Received via mailing list)
On 1/30/07, Gerald E. <removed_email_address@domain.invalid> 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?
Gerald E. (Guest)
on 2007-01-31 12: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 E.
unknown (Guest)
on 2007-01-31 13:51
(Received via mailing list)
On Jan 31, 11:35 am, "Gerald E." <removed_email_address@domain.invalid> 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.
Gerald E. (Guest)
on 2007-01-31 14:25
(Received via mailing list)
Sorry I'm getting my langanges mixed up.

Gerald E.
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: removed_email_address@domain.invalid

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




    Want a signature like this?
Tim P. (Guest)
on 2007-01-31 23:32
(Received via mailing list)
On 1/31/07, Gerald E. <removed_email_address@domain.invalid> 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.