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 value like 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 value like in C

Réponse
 
LinkBack Outils de la discussion
Vieux 15/09/2007, 18h13   #1
John Joyce
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 1:51 AM, 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


All you need is something like:

puts "myArray[3] + 1 is #{myArray[3] + 1} "

String interpolation. Great stuff.
Of course you have to create that array first to have something in it.

You could also do:

puts "myArray[3] + 1 is " + (myArray[3] + 1).to_s

Lots of ways, but the first one is most succinct.
Ruby is very flexible!
You can even go from this into a DSL with words that evaluate to
expressions.

  Réponse avec citation
Vieux 16/09/2007, 12h27   #2
Robert Klemme
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

On 15.09.2007 19:13, John Joyce wrote:
>
> On Sep 15, 2007, at 1:51 AM, 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


def print_val(expr,b=binding)
print( expr, " is ", eval(expr,b).inspect, "\n")
end

irb(main):026:0> a=%{foo bar baz}
=> "foo bar baz"
irb(main):027:0> print_val "a[1]", binding
a[1] is 111
=> nil
irb(main):028:0> print_val "a[1]"
NameError: undefined local variable or method `a' for main:Object
from (irb):21:in `print_val'
from (irb):21:in `print_val'
from (irb):28
from :0

You can even omit the "b" argument if you use something like
"Binding.of_caller".

Kind regards

robert
  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 02h07.


Édité par : vBulletin® version 3.7.3
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 ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,07150 seconds with 10 queries