PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Re: one line to print the statement AS WELL AS the evaluated valuelike in C
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Re: one line to print the statement AS WELL AS the evaluated valuelike in C

Réponse
 
LinkBack Outils de la discussion
Vieux 15/09/2007, 12h11   #1 (permalink)
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
Vieux 15/09/2007, 13h22   #2 (permalink)
Morton Goldberg
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: one line to print the statement AS WELL AS the evaluated value like in C

On Sep 15, 2007, at 7:11 AM, Dan Zwell wrote:

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


That didn't work for me. I had modify it like so:

foo = [1, 2, 3, 4, 5]
def print_val(str)
puts "#{str} is #{eval(str, TOPLEVEL_BINDING)}"
end
print_val("foo[3] + 1")

Regards, Morton

  Réponse avec citation
Vieux 15/09/2007, 13h43   #3 (permalink)
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

Morton Goldberg wrote:
> On Sep 15, 2007, at 7:11 AM, Dan Zwell wrote:
>
>> Here you go:
>> def print_val(str)
>> puts "#{str} is #{eval(str)}"
>> end

>
> That didn't work for me. I had modify it like so:
>
> foo = [1, 2, 3, 4, 5]
> def print_val(str)
> puts "#{str} is #{eval(str, TOPLEVEL_BINDING)}"
> end
> print_val("foo[3] + 1")
>
> Regards, Morton
>
>


You're right, thanks. When I tested this, I used the first poster's
variable name, "Array". So it worked for me, but only because I happened
to use a name with global slope.

Dan

  Réponse avec citation
Vieux 15/09/2007, 23h55   #4 (permalink)
Summercool
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: one line to print the statement AS WELL AS the evaluated value like in C


On Sep 15, 5:22 am, Morton Goldberg <m_goldb...@ameritech.net> wrote:
> That didn't work for me. I had modify it like so:
>
> foo = [1, 2, 3, 4, 5]
> def print_val(str)
> puts "#{str} is #{eval(str, TOPLEVEL_BINDING)}"
> end
> print_val("foo[3] + 1")



it works but then if print_val is called inside an instance method,
then it won't work.



  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 00h57.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,07741 seconds with 12 queries