[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

the level of Ruby programmers vs PHP's

SpringFlowers AutumnMoon

10/19/2007 6:00:00 AM

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#3288ee...
--
Posted via http://www.ruby-....

16 Answers

Shuaib Zahda

10/19/2007 8:03:00 AM

0

do you mean something like this

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

cheers
--
Posted via http://www.ruby-....

SpringFlowers AutumnMoon

10/19/2007 8:33:00 AM

0

Shuaib Zahda 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(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.

--
Posted via http://www.ruby-....

SpringFlowers AutumnMoon

10/19/2007 8:34:00 AM

0

SpringFlowers AutumnMoon wrote:

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

--
Posted via http://www.ruby-....

Shuaib Zahda

10/19/2007 9:42:00 AM

0


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.
--
Posted via http://www.ruby-....

Peña, Botp

10/19/2007 9:55:00 AM

0

From: Shuaib Zahda [mailto:shuaib.zahda@gmail.com]
# 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:0xb7d2a890@(irb):24>

> 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

mortee

10/19/2007 11:02:00 AM

0

Ben Giddings

10/19/2007 2:27:00 PM

0

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


Stefan Rusterholz

10/19/2007 2:45:00 PM

0

Ben Giddings wrote:
> On Oct 19, 2007, at 04:32, SpringFlowers AutumnMoon wrote:
>> 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
--
Posted via http://www.ruby-....

Phlip

10/19/2007 3:08:00 PM

0

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

--
Phlip
http://www.oreilly.com/catalog/9780...
^ assert_xpath

Daniel DeLorme

10/19/2007 3:20:00 PM

0

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