Variable names

David King Landrith said:

Let me restate the question:

Whenever a method is called on an object, one and only one symbol is
used to represent the object.

That statement is incorrect.

Fair enough. Does this work?

Whenever a method is called on an object, one and only one expression
or symbol has been resolved to
used to represent the object.

And so:

Is there any way to determine which symbol was representing the
expression or symbol at the time that the method was invoked?

You’ll forgive me if I feel like I’m playing twenty questions here.
Perhaps I should be asking: There seems to be some “thing” that the
interpreter determines refers to an object, and there can only be one
of these per method that is invoked. How would we describe this
“thing”?

Simple answer: NO

I can see that this is also the answer to the restated question I ask
above.

Just out of curiosity: At what point does the interpreter discard the
expression that it been evaluated to represent the object?

···

On Apr 2, 2004, at 5:22 PM, Jim Weirich wrote:

Longer answer: Throw an exception and immediately catch it. Ask the
exception for its backtrace and parse the file and line number from the
backtrace (see ri backtrace for details). Then read the file in
question
and find the line number of the caller function. Then parse the line
of
code and guess at the variable name (if there is one).

If you always call your type checking method in the form

var.should_be(ClassName) # or whatever format you use

then parsing the line of code is not a difficult problem. However, in
the
general case it is quite difficult.


David King Landrith
(w) 617.227.4469x213
(h) 617.696.7133

Life is tough. It’s even tougher if you’re
an idiot. --John Wayne

public key available upon request

Too clever by half, Charles:

Just the same, you’ve hit upon why I seldom find this list to be very
useful.

“Where can I find this fish’s bicycle?”

“Fish don’t have bicycles.”

The question appears to be enquiring about modes of travel. Thus, a
reasonable answer might point the user in the right direction to
understand modes of fish travel. Even if you don’t understand what the
questioner is trying to get at, if you still desire to offer some
answer, then you should at least request clarification.

The answer you’ve proposed above offers as much illumination as the
following exchange:

Q: “Why do fish hav fines?”

···

On Apr 4, 2004, at 10:41 AM, Charles Miller wrote:

A: “Fish don’t ‘hav’ fines.”

Q: “OK. Why do ‘fishe’ have fins?”

A: “‘Fishe’ don’t have fins’.”

etc.

“But they must have something like it. Can I find their cars?”

“No, they don’t have cars.”

“But they have to get to work somehow!”

“They do?”

If you really think that this is a reasonable type of conversation,
then you may want to find and read some book on how to engage in
fruitful conversation. I understand that there are several good ones
out there. Books that have received good reviews on Amazon include
Conversationally Speaking : Tested New Ways to Increase Your Personal
and Social Effectiveness
by Alan Garner, or How to Start a
Conversation and Make Friends : Revised and Updated
by Don Gabor. I’m
not offering this to be insulting or glib, if you think that this type
of exchange is appropriate then you really may find them to be at least
somewhat helpful. (For the record: I have no relationship to these
books at all, and I have nothing to gain by mentioning them.)

“You guys are so unhelpful. I still don’t know where I can find the
bicycles.”

Actually it’s more like the following:

After 2 days and several emails, I still have no idea how the
characters are getting evaluated and resolved by the interpreter and at
what point these resolved or evaluated characters are disposed of in
favor of the objects that they represent. Moreover, I still have no
idea if or why this question may reflect a category mistake.

I have no experience in writing compilers/interpreters, and I don’t
believe that I should have to in order to learn about such topics.

Best regards,

Dave


David King Landrith
(w) 617.227.4469x213
(h) 617.696.7133

One useless man is a disgrace, two
are called a law firm, and three or more
become a congress – John Adams

public key available upon request

David King Landrith wrote:

Remarks like this make such persons unwelcome to this list.
Gennady.

···

Replies like this make this list next to useless.

On Apr 2, 2004, at 4:46 PM, Dave Thomas wrote:

On Apr 2, 2004, at 15:35, David King Landrith wrote:

Let me restate the question:

Whenever a method is called on an object, one and only one symbol is
used to represent the object. Is there any way to determine which
symbol was representing the object at the time that the method was
invoked?

Not always:

(1 + 2).abs

Cheers

Dave


David King Landrith
(w) 617.227.4469x213
(h) 617.696.7133

Life is tough. It’s even tougher if you’re
an idiot. --John Wayne

public key available upon request

I’m sorry you felt like that: that certainly wasn’t my intent. I was
simply trying to point out what others have said: the receiver of a
message is an object, not a variable, and it isn’t always possible to
determine an appropriate variable name to use from within a receiver.

Have you considered changing your approach slightly? Rather than making
it assertion-based, you could implement it more like cast-style
checking:

def delete_file(str)
name = cast(:str, String)
# …

The ‘cast’ method could then verify its its first parameter was a
kind_of second parameter, and would have the name lying around if it
needed to report an error.

The interpreter takes this kind of approach when ensuring that
parameters have the right behavior. In fact, it goes one better,
supplying the name of a conversion method to use if the object isn’t of
the required type:

name = cast(:str, String, :to_str)

You could also do this, making the third parameter optional.

However, I still question whether this is all worthwhile… :slight_smile:

Cheers

Dave

···

On Apr 2, 2004, at 16:01, David King Landrith wrote:

Replies like this make this list next to useless.

David King Landrith wrote:

David King Landrith said:

Let me restate the question:

Whenever a method is called on an object, one and only one symbol is
used to represent the object.

That statement is incorrect.

Fair enough. Does this work?

Whenever a method is called on an object, one and only one expression
or symbol has been resolved to
used to represent the object.

And so:

Is there any way to determine which symbol was representing the
expression or symbol at the time that the method was invoked?

You’ll forgive me if I feel like I’m playing twenty questions here.
Perhaps I should be asking: There seems to be some “thing” that the
interpreter determines refers to an object, and there can only be one
of these per method that is invoked. How would we describe this “thing”?

This thing is a reference to the receiving object. you can use self#id
to find out this reference but you can’t go from that to the variable or
expression that was origionaly used.

Simple answer: NO

I can see that this is also the answer to the restated question I ask
above.

Just out of curiosity: At what point does the interpreter discard the
expression that it been evaluated to represent the object?

Presumably as soon as it has been evaluated.

you want to define a method so that this works

class X
def meth()
p self.magic
end
end

x = X.new
x.meth #=> x

the problem is this is the moral equivelent of

def meth(self)
p self.magic
end

x = x.new
meth(x) #=> x

within meth it just knows there is an object called self, it has no
knowledge of the fact that elsewhere in the program this object is
reffered to as x.

···

On Apr 2, 2004, at 5:22 PM, Jim Weirich wrote:


Mark Sparshatt

Have you considered that perhaps the reason that this list is seldom helpful
to you is that you continuously degrade the people that are trying to help
you?

I’ve stopped reading your posts, not because they don’t have any interesting
content, but because you’re quite arrogant and rude. Step down from your
high horse. Goodbye.


Jason Voegele
One of the most striking differences between a cat and a lie is that a cat has
only nine lives.
– Mark Twain, “Pudd’nhead Wilson’s Calendar”

···

On Sunday 04 April 2004 11:45 am, David King Landrith wrote:

Too clever by half, Charles:

Just the same, you’ve hit upon why I seldom find this list to be very
useful.

David,

I think I’ll have to side with you on this. Experts, in my experience,
have a tendency to answer the literal question that was asked, without
trying to understand and clarify the issues underneath. I know I’ve
fallen into that trap before. However, don’t assume it to be a
conversational mistake: I’ve noticed that it tends to happen only in
electronic media; the same person would not necessarily have the same
conversational style person-to-person.

I have to admit that I have not followed this thread. However, Looking
back in the archive I think I’ll take a stab and answering your question.

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’?

I have a strong desire to say “no”, here. That’s the easy answer. :slight_smile:

The more difficult answer requires some understanding of Ruby’s
execution environment. First of all, there is no guarantee that the
object that is recieving the message is bound to a variable at all.
Consider:

puts Object.new.to_s

The object recieving the “to_s” message is not bound to a variable.
Another scenario is that an object may be bound to multiple variables:

a = b = Object.new
puts b.to_s

Your question, if I understood it, is whether or not the “to_s” method
in the example above would be able to determine that it was invoked via
the binding of ‘self’ to ‘b’.

The answer to that is “no”. It may be possible to identify that it
could have been bound to either ‘a’ or ‘b’ (it would require some
introspection of the local execution environment of the caller, which
may or may not be possible), but you cannot state for certain one way or
another in Ruby.

I hope that helped to answer your question. What is more, I hope I
didn’t duplicate another post in this thread that I failed to read. :smiley:

  • Jamis

David King Landrith wrote:

···

Too clever by half, Charles:

Just the same, you’ve hit upon why I seldom find this list to be very
useful.

On Apr 4, 2004, at 10:41 AM, Charles Miller wrote:

“Where can I find this fish’s bicycle?”

“Fish don’t have bicycles.”

The question appears to be enquiring about modes of travel. Thus, a
reasonable answer might point the user in the right direction to
understand modes of fish travel. Even if you don’t understand what the
questioner is trying to get at, if you still desire to offer some
answer, then you should at least request clarification.

The answer you’ve proposed above offers as much illumination as the
following exchange:

Q: “Why do fish hav fines?”

A: “Fish don’t ‘hav’ fines.”

Q: “OK. Why do ‘fishe’ have fins?”

A: “‘Fishe’ don’t have fins’.”

etc.

“But they must have something like it. Can I find their cars?”

“No, they don’t have cars.”

“But they have to get to work somehow!”

“They do?”

If you really think that this is a reasonable type of conversation, then
you may want to find and read some book on how to engage in fruitful
conversation. I understand that there are several good ones out there.
Books that have received good reviews on Amazon include
Conversationally Speaking : Tested New Ways to Increase Your Personal
and Social Effectiveness
by Alan Garner, or How to Start a
Conversation and Make Friends : Revised and Updated
by Don Gabor. I’m
not offering this to be insulting or glib, if you think that this type
of exchange is appropriate then you really may find them to be at least
somewhat helpful. (For the record: I have no relationship to these
books at all, and I have nothing to gain by mentioning them.)

“You guys are so unhelpful. I still don’t know where I can find the
bicycles.”

Actually it’s more like the following:

After 2 days and several emails, I still have no idea how the characters
are getting evaluated and resolved by the interpreter and at what point
these resolved or evaluated characters are disposed of in favor of the
objects that they represent. Moreover, I still have no idea if or why
this question may reflect a category mistake.

I have no experience in writing compilers/interpreters, and I don’t
believe that I should have to in order to learn about such topics.

Best regards,

Dave


David King Landrith
(w) 617.227.4469x213
(h) 617.696.7133

One useless man is a disgrace, two
are called a law firm, and three or more
become a congress – John Adams

public key available upon request

.


Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck.org/jamis

ruby -h | ruby -e
‘a=;readlines.join.scan(/-(.)[e|Kk(\S*)|le.l(…)e|#!(\S*)/) {|r| a <<
r.compact.first };puts “\n>#{a.join(%q/ /)}<\n\n”’

Let me see if I can do better this time.

You read the expression:

a.foo

And you see #foo being called on the object called ‘a’.

The interpreter does something like this:

  1. I see that I need to find an object called ‘a’
  2. I look in my symbol table and find the appropriate ‘a’ entry and follow
    it to the object it points to.
  3. I call the #foo method of the object I have.

Note that in 3 there is no reference to ‘a’. In fact, the object you have
could be the result of some more complicated expression as in the case

(1 + 2).foo

In that case, the process is more like:

  1. Get the object that’s the result of evaluating (1 + 2)
  2. Call #foo on that object

Now, you’re inside #foo, so you have the object that ‘a’ pointed to, and
you want to retrieve :a from it. However, in this process, once you have
the object, you throw away the variable name or expression that it came
from because you don’t need it anymore.

If you had access to the symbol table, you could look through it and find
all symbols that lead to the object you have. But it’s possible that it has
more than one name in the symbol table, so although you could find a
name for that object, you wouldn’t necessarily be able to find the name
you want. Also, in the (1 + 2) case, the object doesn’t necessarily have
a name in the symbol table, because (1 + 2) wasn’t assigned to any
variable, and the symbol table only stores variable names. It doesn’t matter
anyway since Ruby doesn’t give you access to the symbol table.

What you want (more or less) is the expression that most recently
corresponded to the object, but the interpreter doesn’t store that information
because it’s useless 99% of the time.

That is a technical explanation of why you can’t do it.

If you want a more abstract description try this:

A variable name is not part of an object. An object is a chunk of memory,
and a variable name is a memory address. The chunk of memory doesn’t
know what its address is, though; it’s just a sequence of 1’s and 0’s.

Analogy:

I am me. Other people call me by some name (Dan, Daniel, Dolio, etc.).
I don’t call myself by any of those names, and I don’t know all the names
that people use to think about me. I’d have to ask them all to find that
information out. In some sense, my name isn’t intrinsically part of me, it’s
just a label people use to refer to me. It’s external, so I don’t know about
or use it.

Was any of this helpful? If not I can try again, but I don’t know how I can
clarify much better, either in concrete or abstract terms.

  • Dan

I think tis might well be the underlying issue. To understand why
people are saying that your question on the surface doesn’t make sense
to them, so have to invest some time meeting them in the middle when
they explain the issues.

They are honestly trying to be helpful, but the underlying issue is
more complex than you’d like it to be. This isn’t a language issue,
it’s a conceptual one. The problem would be the same in most languages,
and with most technologies.

An object may be referenced from many variables at the same time, and
each variable may reference many objects over its life. Given that,
it’s hard to think of a possible API that would let you know the name
of a variable that was used to reference an object. Consider.

def so_something_with(v1, v2)
   # your checks go here
end

a = cat
do_something_with(a, a)

Within the context of the method, how would you (from the object “cat”)
go about resolving the name of a variable used to reference you?

Variables don’t really exist in their own right: they are not objects,
and have no behavior other than containment.

Cheers

Dave

···

On Apr 4, 2004, at 10:45, David King Landrith wrote:

After 2 days and several emails, I still have no idea how the
characters are getting evaluated and resolved by the interpreter and
at what point these resolved or evaluated characters are disposed of
in favor of the objects that they represent. Moreover, I still have
no idea if or why this question may reflect a category mistake.

I have no experience in writing compilers/interpreters, and I don’t
believe that I should have to in order to learn about such topics.

David King Landrith wrote:

After 2 days and several emails, I still have no idea how the characters
are getting evaluated and resolved by the interpreter and at what point
these resolved or evaluated characters are disposed of in favor of the
objects that they represent. Moreover, I still have no idea if or why
this question may reflect a category mistake.

OK, let me take a stab at this.

If I understand you correctly, you want to retrieve the original
expression on which a method was called.

In the current Ruby implementation, this is absolutely impossible.
Ruby does not at this time expose such a deep and detailed knowledge of
the state of the interpreter’s internals.

It is true that the expression is represented as a string of characters,
and that this eventually corresponds to an object on which you may call
a method.

But the resolution or evaluation as you call it happens so early, so
deep in the bowels of the interpreter, that the running code cannot get
access to the original text.

Ruby is a very introspective language. But no language (in my
repertoire) has such a deep, deep introspection – not Java, not Ruby,
not anything else (that I know).

Lisp is probably an exception – in fact, I would think it would be.
And there are likewise probably others.

The Ruby interpreter is a black box. Exposing its internals to the
running program requires a great deal of work on the part of the
language implementor. What’s more, it makes sense to hide everything
that doesn’t NEED to be exposed, for the sake of modularity.

So there are only certain things that are exposed. eval(), for instance,
is like a little window into the guts of the interpreter. But it is a
window that took man-months to get right.

I have no experience in writing compilers/interpreters, and I don’t
believe that I should have to in order to learn about such topics.

If you want to solve a problem of such gigantic complexity, and there
has been no provision for this kind of thing in advance, then yes,
you do need knowledge of writing interpreters, because you will have
to write one of your own that does what you want.

In short: The Ruby interpreter doesn’t do what you want.

Your alternatives are:

  1. Use a different language, such as Lisp.
  2. Write your own Ruby interpreter.
  3. Persuade Matz to invest a few man-months in this endeavor.

Does that help any?

Hal

Now that’s helpful. Thx.

···

On Apr 2, 2004, at 5:18 PM, Dave Thomas wrote:

On Apr 2, 2004, at 16:01, David King Landrith wrote:

Replies like this make this list next to useless.

I’m sorry you felt like that: that certainly wasn’t my intent. I was
simply trying to point out what others have said: the receiver of a
message is an object, not a variable, and it isn’t always possible to
determine an appropriate variable name to use from within a receiver.

Have you considered changing your approach slightly? Rather than
making it assertion-based, you could implement it more like cast-style
checking:

def delete_file(str)
name = cast(:str, String)
# …

The ‘cast’ method could then verify its its first parameter was a
kind_of second parameter, and would have the name lying around if it
needed to report an error.

The interpreter takes this kind of approach when ensuring that
parameters have the right behavior. In fact, it goes one better,
supplying the name of a conversion method to use if the object isn’t
of the required type:

name = cast(:str, String, :to_str)

You could also do this, making the third parameter optional.

However, I still question whether this is all worthwhile… :slight_smile:

Cheers

Dave


David King Landrith
(w) 617.227.4469x213
(h) 617.696.7133

Life is tough. It’s even tougher if you’re
an idiot. --John Wayne

public key available upon request

Does that help any?

Immensely. Your post obviously took some time and effort to write.
Thank you.

···

On Apr 4, 2004, at 7:04 PM, Hal Fulton wrote:

David King Landrith wrote:

After 2 days and several emails, I still have no idea how the
characters are getting evaluated and resolved by the interpreter and
at what point these resolved or evaluated characters are disposed of
in favor of the objects that they represent. Moreover, I still have
no idea if or why this question may reflect a category mistake.

OK, let me take a stab at this.

If I understand you correctly, you want to retrieve the original
expression on which a method was called.

In the current Ruby implementation, this is absolutely impossible.
Ruby does not at this time expose such a deep and detailed knowledge of
the state of the interpreter’s internals.

It is true that the expression is represented as a string of
characters,
and that this eventually corresponds to an object on which you may call
a method.

But the resolution or evaluation as you call it happens so early, so
deep in the bowels of the interpreter, that the running code cannot get
access to the original text.

Ruby is a very introspective language. But no language (in my
repertoire) has such a deep, deep introspection – not Java, not Ruby,
not anything else (that I know).

Lisp is probably an exception – in fact, I would think it would be.
And there are likewise probably others.

The Ruby interpreter is a black box. Exposing its internals to the
running program requires a great deal of work on the part of the
language implementor. What’s more, it makes sense to hide everything
that doesn’t NEED to be exposed, for the sake of modularity.

So there are only certain things that are exposed. eval(), for
instance,
is like a little window into the guts of the interpreter. But it is a
window that took man-months to get right.

I have no experience in writing compilers/interpreters, and I don’t
believe that I should have to in order to learn about such topics.

If you want to solve a problem of such gigantic complexity, and there
has been no provision for this kind of thing in advance, then yes,
you do need knowledge of writing interpreters, because you will have
to write one of your own that does what you want.

In short: The Ruby interpreter doesn’t do what you want.

Your alternatives are:

  1. Use a different language, such as Lisp.
  2. Write your own Ruby interpreter.
  3. Persuade Matz to invest a few man-months in this endeavor.

Does that help any?

Hal


David King Landrith
(w) 617.227.4469x213
(h) 617.696.7133

One useless man is a disgrace, two
are called a law firm, and three or more
become a congress – John Adams

public key available upon request

This is a test of the news gateway. Sorry to flood. Dennis and
I are working on the problem and to test it I have to respond to
an existing message…

David

···

On Mon, 5 Apr 2004, Hal Fulton wrote:

stuff


David A. Black
dblack@wobblini.net