On Thu, May 08, 2003 at 04:24:02AM +0900, meinrad.recheis wrote:
> is it possible to access the variable-name of an object?
> 
> like (pseudo ruby code):
> 
> @instance_var = []
> @instance_var.name = '@instance_var'
          ( is this '==' ? )


I don't really fully understand what you mean, but what I get is that
you'd like to manipulate variables themselves (not the objects they
point to). Take into account that Ruby isn't statically typed, so
variables don't have properties per se (types), only the objects 
they point to. Moreover several variables can refer to the same
object...

The closest to what you ask would be using Object#instance_variables to
get a list of the defined instance variables for a given object.

But as I told before, I don't really get what you intend to do, please
clarify :)

Assuming that you meant that 
 @instance_var.name == '@instance_var'
you can do something like the following (but have no idea what it could
be used for, and do see it as quite evil in fact).

batsman@tux-chan:/tmp$ expand -t2 ab.rb
module InstanceNameInfo
  def attr_with_info(*ids)
    ids.each do |x|
      class_eval <<-EOF
        def #{x}
          obj = @#{x}.dup
          class << obj
            def name
              "@#{x}"
            end
          end
          obj
        end
      EOF
    end
  end

end

class Foo
  extend InstanceNameInfo
  def initialize
    @foo = "asdas"
    @bar = "aaa"
  end

  attr_with_info :foo, :bar
end


a = Foo.new
puts "#{a.foo} => #{a.foo.name}"
puts "#{a.bar} => #{a.bar.name}"
batsman@tux-chan:/tmp$ ruby ab.rb
asdas => @foo
aaa => @bar

You might want to add a singleton method directly to the
objects, etc...

Sorry if I missed the point completely.

-- 
 _           _                             
| |__   __ _| |_ ___ _ __ ___   __ _ _ __  
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ 
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
	Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

To kick or not to kick...
	-- Somewhere on IRC, inspired by Shakespeare