Afficher un message
Vieux 15/09/2007, 13h11   #1
Dan Zwell
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: one line to print the statement AS WELL AS the evaluated valuelike in C

kendear wrote:
> i wonder in Ruby, is there a line method to do something like in C
>
>
> print_val("Array[3] + 1")
>
> and get the printout of
>
>
> Array[3] + 1 is 3.345
>


Here you go:
def print_val(str)
puts "#{str} is #{eval(str)}"
end


This should do what you want. Be wary of side effects, though. For
example, to display the value of "a += 3" requires evaluation, and that
changes the value of "a" (and when you remove this debugging line, your
program no longer works the way you expect).

Have fun,
Dan

  Réponse avec citation
 
Page generated in 0,06124 seconds with 9 queries