The level of Ruby programmers vs PHP's

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.

And this is what happen instead in the PHP newsgroup:
http://groups.google.com/group/comp.lang.php/browse_thread/thread/e02037a5c14e54c9/3288eeef436b64f7#3288eeef436b64f7

do you mean something like this

def print_name_variable(var)
print 'var = ’
print var
end

cheers

Shuaib Z. wrote:

do you mean something like this

def print_name_variable(var)
print 'var = ’
print var
end

oh i mean something like

print_debug($foo)

will print

$foo is 3

so if i have 20 expressions,

like

print_debug(23)
print_debug(8
(1.0/3))
print_debug(a + 2.038)
[…]

it will print out the expression as well as the value at the same time,
as a convenience for testing and for debugging.

SpringFlowers AutumnMoon wrote:

print_debug(23)
print_debug(8
(1.0/3))
print_debug(a + 2.038)
[…]

it will print out the expression as well as the value at the same time,
as a convenience for testing and for debugging.

so it is more like, printing out literally the expression of interest as
a string, and then the evaluated value.

I know this method (inspect) it does output the values in a human
readable way but does not output the name of the variable.

example

irb(main):008:0> arr = [10, “text”, 10.78]
=> [10, “text”, 10.78]
irb(main):009:0> puts arr.inspect
[10, “text”, 10.78]
=> nil
irb(main):010:0>

hopefully this helps. Sorry I am not php expert and ruby neither.

From: Shuaib Z. [mailto:[email protected]]

hopefully this helps. Sorry I am not php expert

and ruby neither.

no problem, i’m not an expert, ever…

how about,

arr = [10, “text”, 10.78]
=> [10, “text”, 10.78]

pd=proc{|d| “#{d} is #{eval(d).inspect}”}
=> #Proc:[email protected]:24(irb)

pd.call(“arr”)
=> “arr is [10, “text”, 10.78]”

pd.call(“arr<<999”)
=> “arr<<999 is [10, “text”, 10.78, 999]”

but the main question is: “there can be many arr arrays, which one?” I
think a debugger is better suited.

kind regards -botp

On Oct 19, 2007, at 04:32, SpringFlowers AutumnMoon wrote:

print_debug(23)
print_debug(8
(1.0/3))
print_debug(a + 2.038)
[…]

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

Ben G. wrote:

On Oct 19, 2007, at 04:32, SpringFlowers AutumnMoon wrote:

print_debug(23)
print_debug(8
(1.0/3))
print_debug(a + 2.038)
[…]

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

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.

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.

And this is what happen instead in the PHP newsgroup:
http://groups.google.com/group/comp.lang.php/browse_thread/thread/e02037a5c14e54c9/3288eeef436b64f7#3288eeef436b64f7

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?

mortee

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…

On 10/19/07, Phlip [email protected] 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.

Just default-arg it to the top binding.

def debug_eval(code, binding = TOP_LEVEL_BINDING)
puts “#{code}\n => #{eval(code, binding)}”
end

Jason

On 19/10/2007, mortee [email protected] wrote:

on the PHP newsgroup?

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 Oct 19, 2007, at 4:32 AM, SpringFlowers AutumnMoon wrote:

oh i mean something like

print_debug($foo)

will print

$foo is 3

OOOH! I just learned this is CS class!!!

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 19/10/2007, Daniel DeLorme [email protected] 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…

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

And this is what happen instead in the PHP newsgroup:
http://groups.google.com/group/comp.lang.php/browse_thread/thread/e02037a5c14e54c9/3288eeef436b64f7#3288eeef436b64f7

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, 19 Oct 2007 17:34:29 +0900, SpringFlowers AutumnMoon
[email protected] wrote:

so it is more like, printing out literally the expression of interest as
a string, and then the evaluated value.

I think the best you can do in Ruby without resorting to
ParseTree/Ruby2Ruby is something like this:

print_debug {“23"}
print_debug {"8
(1.0/3)”}
print_debug {“a + 2.038”}

…where print_debug is implemented like this:

def print_debug(&expr_block)
expr = expr_block.call
value = eval(expr, expr_block.binding)
puts “#{expr} = #{value.inspect}”
end

Does that help?

-mental

Mental G. wrote:

print_debug {“23"}
print_debug {"8
(1.0/3)”}
print_debug {“a + 2.038”}

…where print_debug is implemented like this:

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.
[email protected]

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.
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs