i asked the same question on Ruby: how do you write some code to print
out the variable's name automatically as well as its content. In here, I
got some helpful discussions and solutions.
i asked the same question on Ruby: how do you write some code to print
out the variable's name automatically as well as its content. In here, I
got some helpful discussions and solutions.
Are you currently looking for an answer, or you have already discussed
this, and are just pointing out the difference in answers got here and
on the PHP newsgroup?
i asked the same question on Ruby: how do you write some code to print
out the variable's name automatically as well as its content. In here, I
got some helpful discussions and solutions.
For the record, what was the solution? I can see how you could do it with eval, both in ruby and php, but beyond that...
In my experience, the best (and most helpful) PHP programmers are usually
not part of the PHP community -- but they tend to be active members of
communities for other languages. Unfortunately, you have to be pretty
clever to make a question about how to do something in PHP "on-topic" for
another language's community, such as in ruby-talk.
I guess, if I wanted to know how to do something in PHP, I'd be more
likely to ask for a general solution that would work for both Ruby and
PHP in the Ruby community than to specifically ask for a PHP solution in
the PHP community, since I'd be more likely to get useful responses (and
they'd be especially useful, generally speaking, because they'd be
portable across languages).
···
On Fri, Oct 19, 2007 at 03:00:01PM +0900, SpringFlowers AutumnMoon wrote:
i asked the same question on Ruby: how do you write some code to print
out the variable's name automatically as well as its content. In here, I
got some helpful discussions and solutions.
--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
John Kenneth Galbraith: "If all else fails, immortality can always be
assured through spectacular error."
They actually gave you a PHP solution that works for variables (with a
bit of tweaking). You probably cannot get arbitrary expressions.
However, I am not surprised. PHP stands for something like Personal
Homepage Preprocessor. Something akin to what awk is for shell
scripts, just for the web. People tend to forget about this and try to
use it for other stuff.
In ruby you can always eval the expression if you do not mind that you
either have to pass around a binding or limit yourself to non-local
variables. And it's slow.
If you want macros then you are out of luck with most scripting
languages I guess.
Thanks
Michal
···
On 19/10/2007, mortee <mortee.lists@kavemalna.hu> wrote:
Are you currently looking for an answer, or you have already discussed
this, and are just pointing out the difference in answers got here and
on the PHP newsgroup?
There is a function in PHP that returns name-indexed array of
currently accessible variables or something like that (which is not
unlike operating on Ruby bindings for this purpose). This allows to
print values of variables by name if you pass in the array and the
variable name.
Thanks
Michal
···
On 19/10/2007, Daniel DeLorme <dan-ml@dan42.com> wrote:
SpringFlowers AutumnMoon wrote:
> i asked the same question on Ruby: how do you write some code to print
> out the variable's name automatically as well as its content. In here, I
> got some helpful discussions and solutions.
For the record, what was the solution? I can see how you could do it
with eval, both in ruby and php, but beyond that...
This is one of the few reasons I wish Ruby had macros. Having a macro like this can be very useful, but it isn't possible with a function/method because the argument gets evaluated before being passed to the context of the function/method. In the above examples print_debug would be passed 3, 8, 2.0 and some float value.
I don't know if it would be possible to have some special macroish functionality in Ruby that would allow "print_debug()" but not open the can of worms that is macros.
Ben
···
On Oct 19, 2007, at 04:32, SpringFlowers AutumnMoon wrote:
Ok, it cannot print $foo, because $foo is a reference. All that is actually passed to the method is the value of $foo. Thus, the method does not actually know who gave it the address of the value - only the value itself.
Although I think forth might let you...... Since you can do liike (I think):
a = 5
method(a)
a #=> 6
HTH
aRi
-------------------------------------------|
Nietzsche is my copilot
···
On Oct 19, 2007, at 4:32 AM, SpringFlowers AutumnMoon wrote:
it will print out the expression as well as the value at the same
time,
as a convenience for testing and for debugging.
This is one of the few reasons I wish Ruby had macros. Having a
macro like this can be very useful, but it isn't possible with a
function/method because the argument gets evaluated before being
passed to the context of the function/method. In the above examples
print_debug would be passed 3, 8, 2.0 and some float value.
I don't know if it would be possible to have some special macroish
functionality in Ruby that would allow "print_debug()" but not open
the can of worms that is macros.
Ben
Ugly solution:
def debug_eval(code, binding)
puts "#{code}\n=> #{eval(code, binding)}"
end
a = 3
b = 5
debug_eval(%{a*b}, binding)
Though IMHO all you need when you print the debug output of some
statement is a label, such as
p [:value_of_foo, foo_expression]
That's usually sufficient here. I mean, when you use such a statement
you *know* what expression you're inspecting. At least I seriously hope
that such statements are only used for short time inspection and for
that timeframe, your memory should be sufficient, no?
Regards
Stefan
···
On Oct 19, 2007, at 04:32, SpringFlowers AutumnMoon wrote:
Ugly solution:
def debug_eval(code, binding)
puts "#{code}\n=> #{eval(code, binding)}"
end
I think there's some way to get the binding without passing it in.
Though IMHO all you need when you print the debug output of some
statement is a label, such as
p [:value_of_foo, foo_expression]
That's usually sufficient here. I mean, when you use such a statement
you *know* what expression you're inspecting. At least I seriously hope
that such statements are only used for short time inspection and for
that timeframe, your memory should be sufficient, no?
Except that assert() should use the exact same reflection, to
self-document, and should live forever.
Ideally, we should not need assert_equal, assert_match, assert_near,
assert_far, etc. We should only have assert, and it should generate a
useful diagnostic based entirely on reflection. Our current suite of
hacks exist to customize the diagnostic at fault time.
def print_debug(&expr_block)
expr = expr_block.call
value = eval(expr, expr_block.binding)
puts "#{expr} = #{value.inspect}"
end
Does that help?
Thanks for all the help.
This guy Jerry Stuckle is just outright rude on the PHP newsgroup:
Jerry Stuckle wrote:
This is neither C nor Ruby. Don't try to compare them (and, BTW, it's
not part of the C language - it's the debug libraries you're using which
allow it).
If you want the variable's name, just say it!
echo '$foo=' . $foo;
···
--
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
No, if you can't code a simple echo statement than you should be in the
food or hospitality industry.
We told you the answer. If you don't want to accept it, then the food
or hospitality industry sounds like a much better fit for you.
--
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net