From: Simon Strandgaard Date: 2004-04-03T07:24:19+09:00 Subject: Re: Variable names On Sat, 03 Apr 2004 02:20:24 +0900, David King Landrith wrote: [snip] > Is there any way to access the name of a given variable instance from > within it? In other words, for any object x, is there C function to > which I can pass self to determine that the program calls it 'x'? > > Thanks, > You want something like this ? server> ruby a.rb a.rb:4:in `should_be': expected String, but got Fixnum (TypeError) from a.rb:12:in `test' from a.rb:16 server> expand -t2 a.rb class Object def should_be(klass) unless self.kind_of?(klass) raise TypeError, <<-MSG.gsub(/^\s*/, '') expected #{klass}, but got #{self.class} MSG end end end def test(str) str.should_be(String) end test("hello") test(666) # boom.. as we expect server> -- Simon Strandgaard