the level of Ruby programmers vs PHP's

  • Thread starter SpringFlowers AutumnMoon
  • Start date
S

Shuaib Zahda

do you mean something like this

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

cheers
 
S

SpringFlowers AutumnMoon

Shuaib said:
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(2**3)
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.
 
S

SpringFlowers AutumnMoon

SpringFlowers said:
print_debug(2**3)
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.
 
S

Shuaib Zahda

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

Peña, Botp

RnJvbTogU2h1YWliIFphaGRhIFttYWlsdG86c2h1YWliLnphaGRhQGdtYWlsLmNvbV0gDQojIGhv
cGVmdWxseSB0aGlzIGhlbHBzLiBTb3JyeSBJIGFtIG5vdCBwaHAgZXhwZXJ0IA0KIyBhbmQgcnVi
eSBuZWl0aGVyLg0KDQpubyBwcm9ibGVtLCBpJ20gbm90IGFuIGV4cGVydCwgZXZlci4uDQoNCmhv
dyBhYm91dCwNCg0KPiBhcnIgPSBbMTAsICJ0ZXh0IiwgMTAuNzhdDQo9PiBbMTAsICJ0ZXh0Iiwg
MTAuNzhdDQoNCj4gcGQ9cHJvY3t8ZHwgIiN7ZH0gaXMgI3tldmFsKGQpLmluc3BlY3R9In0NCj0+
ICM8UHJvYzoweGI3ZDJhODkwQChpcmIpOjI0Pg0KDQo+IHBkLmNhbGwoImFyciIpDQo9PiAiYXJy
IGlzIFsxMCwgXCJ0ZXh0XCIsIDEwLjc4XSINCg0KPiBwZC5jYWxsKCJhcnI8PDk5OSIpDQo9PiAi
YXJyPDw5OTkgaXMgWzEwLCBcInRleHRcIiwgMTAuNzgsIDk5OV0iDQoNCmJ1dCB0aGUgbWFpbiBx
dWVzdGlvbiBpczogInRoZXJlIGNhbiBiZSBtYW55IGFyciBhcnJheXMsIHdoaWNoIG9uZT8iICBJ
IHRoaW5rIGEgZGVidWdnZXIgaXMgYmV0dGVyIHN1aXRlZC4NCg0Ka2luZCByZWdhcmRzIC1ib3Rw
DQoNCg==
 
M

mortee

SpringFlowers said:
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...7a5c14e54c9/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
 
B

Ben Giddings

print_debug($foo)

will print

$foo is 3

so if i have 20 expressions,

like

print_debug(2**3)
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
 
S

Stefan Rusterholz

Ben said:
print_debug(2**3)
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
 
P

Phlip

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

Daniel DeLorme

SpringFlowers said:
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...
 
M

Michal Suchanek

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?

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
 
M

Michal Suchanek

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
 
A

Ari Brown

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
 
C

Chad Perrin

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...7a5c14e54c9/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).
 
M

MenTaLguY

SpringFlowers said:
print_debug(2**3)
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 think the best you can do in Ruby without resorting to
ParseTree/Ruby2Ruby is something like this:

print_debug {"2**3"}
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
 
S

SpringFlowers AutumnMoon

Mental said:
print_debug {"2**3"}
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.
(e-mail address removed)
==================


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.
(e-mail address removed)
==================
 

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
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top