Variable names

  • Thread starter David King Landrith
  • Start date
G

Gavin Sinclair

So, say we have a method print_line that takes two parameters (1) an
integer line number and (2) a string which is the line.
def print_line(lineNo, line)
lineNo.should_be(Integer, :lineNo)
line.should_be(String, :line)
# by now, we would have thrown a TypeError if lineNo
# and line are not an Integer and a String respectively
[...]


Since what you want doesn't (to me) intuitively exist, I suggest
rewriting it as:

def print_line(lineNo, line)
should_be(Integer, :lineNo)
should_be(String, :line)
...


def evaluate_local_variable(var)
if local_variables.include? var
eval var
else
raise ArgumentError, "You didn't give me a local variable: #{var}"
end
end


You can probably improve on that, and write it in C, but you get the
idea.

Cheers,
Gavin
 
T

ts

D> From what I understand, since 12 is a FixNum, a and b end up being
D> separate FixNum objects with their own separate values of 12. But this
D> strikes me as beside the point.

You have not understood my example. Suppose that you want to define a
method #name, give me the result for this

def tt(x, y)
p x.name, y.name
end

a = []
tt(a, a)


my example has nothing to do with Fixnum

D> In the C api, each function gets passed a pointer to the struct which
D> represents self.

well, I think that I know a little the C API ...


Guy Decoux
 
F

Florian Gross

David said:
But (as usual -- and this is the reason I rarely use this
list) people seem more interested in pedantry than genuine assistance.
Don't get me wrong, the answers are informative and often fill me with
awe at vast expanses of knowledge possessed by those that offer them.
They just provide both too much and not enough information at the same
time, which typically indicates that you're dealing with either an
obscurantist (e.g., a politician) or a pedant.

Or that somebody just isn't interested in talking about what you asked,
either because they don't know or just don't want to talk about this.

That might sound rude, but think about the nature of this mailing list:
You're not talking to one single person, but many persons. If somebody
replies to your thread another person might reply to *that* person
instead of you.

This will of course create responses which don't interest the original
poster at all, but it will create responses which might interest other
persons in the group.

So all this is just natural -- don't be amazed or offended by it,
because obviously not every posting in your thread is directed at you.

Regards,
Florian Gross
 
D

Dan Doel

I wasn't trying to be pedantic.

I was trying to explain _why_ you couldn't do what you want to do,
as are other posts here (including the examples of Mr. Thomas
and Mr. Decoux, who each know far more about Ruby than both of
us put together I'm sure :)).

If all you want is a yes/no answer, I'm sure people here can
accomodate you, but I personally would rather know why I can't
do something rather than simply knowing I can't.

- Dan
 
C

Charles Miller

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?

Everything that has been said thus far leads me to believe that the
answer is no, but nobody has hit the point square on. I'm anxious to
clarify my question or provide more information if my question remains
unclear. But (as usual -- and this is the reason I rarely use this
list) people seem more interested in pedantry than genuine assistance.
Don't get me wrong, the answers are informative and often fill me
with awe at vast expanses of knowledge possessed by those that offer
them. They just provide both too much and not enough information at
the same time, which typically indicates that you're dealing with
either an obscurantist (e.g., a politician) or a pedant.

"Where can I find this fish's bicycle?"

"Fish don't have bicycles."

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

"You guys are so unhelpful. I still don't know where I can find the
bicycles."

Charles Miller
 
D

David King Landrith

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

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
 
J

Jason Voegele

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Too clever by half, Charles:

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

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"
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQFAcCGwrFeZdK3QLzERAiVRAKCYTuYImOz3XNU53mC9iTPUHhTd6wCfYcP1
OZy1aRuoxVLycEivXXN5J5o=
=+q0M
-----END PGP SIGNATURE-----
 
J

Jamis Buck

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

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. :-D

- Jamis

Too clever by half, Charles:

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





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.



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



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
(e-mail address removed)
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"'
 
D

Dan Doel

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
 
D

Dave Thomas

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.

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
 
H

Hal Fulton

David said:
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
 
D

David King Landrith

Does that help any?

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

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.


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top