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, 18h49   #1
Joel VanderWerf
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


def dbg(&bl)
s = yield
puts "#{s} = #{eval(s, bl).inspect}"
end

array=[0,1,2,3]
dbg{"array[3]+1"} # ==> array[3]+1 = 4


It seems a little awkward to use both {...} and "..." around the
expression, but the {} allows the #dbg method to capture the binding of
the caller.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

  Réponse avec citation
Vieux 16/09/2007, 00h17   #2
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, 10:49 am, Joel VanderWerf <vj...@path.berkeley.edu> wrote:
> kendear wrote:
> > i wonder in Ruby, is there a line method to do something like in C

>
> def dbg(&bl)
> s = yield
> puts "#{s} = #{eval(s, bl).inspect}"
> end
>
> array=[0,1,2,3]
> dbg{"array[3]+1"} # ==> array[3]+1 = 4
>
> It seems a little awkward to use both {...} and "..." around the
> expression, but the {} allows the #dbg method to capture the binding of
> the caller.


It works! both for globals and for instance variables...
but what is this call dbg{"n"} using { } instead of ( )
does it have a name? and does any book talk about it?

i guess there is no way to do

dbg(array[3] + 1) or dbg{array[3] + 1} without quoting it as
string huh? C was able to do it due to the preprocessor.



  Réponse avec citation
Vieux 16/09/2007, 00h42   #3
Joel VanderWerf
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

Summercool wrote:
>
> On Sep 15, 10:49 am, Joel VanderWerf <vj...@path.berkeley.edu> wrote:
>> kendear wrote:
>>> i wonder in Ruby, is there a line method to do something like in C

>> def dbg(&bl)
>> s = yield
>> puts "#{s} = #{eval(s, bl).inspect}"
>> end
>>
>> array=[0,1,2,3]
>> dbg{"array[3]+1"} # ==> array[3]+1 = 4
>>
>> It seems a little awkward to use both {...} and "..." around the
>> expression, but the {} allows the #dbg method to capture the binding of
>> the caller.

>
> It works! both for globals and for instance variables...
> but what is this call dbg{"n"} using { } instead of ( )
> does it have a name? and does any book talk about it?


The {..} is just ruby's block notation, just as in "each {...}"

> i guess there is no way to do
>
> dbg(array[3] + 1) or dbg{array[3] + 1} without quoting it as
> string huh? C was able to do it due to the preprocessor.


Not easy.

Another advantage of using the block notation is that you can disable
evaluation with a global flag:

def dbg
if $debug
s = yield
puts "#{s} = #{eval(s, Proc.new).inspect}"
end
end

If the flag is set, the dbg call is just a method call and a variable
test. It doesn't even instantiate a Proc (note that I removed the &bl
and added Proc.new).

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

  Réponse avec citation
Vieux 16/09/2007, 00h43   #4
Patrick Hurley
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 9/15/07, Summercool <Summercoolness@gmail.com> wrote:
> It works! both for globals and for instance variables...
> but what is this call dbg{"n"} using { } instead of ( )
> does it have a name? and does any book talk about it?
>
> i guess there is no way to do


Using other people's magic try this:

require 'rubygems'
require 'ruby2ruby'

def annotated(&blk)
# the real magic, thanks ZenSpider :-)
lines = blk.to_ruby

# Turn the results into an array
lines = lines.split(/\n/)

# Chop off the proc stuff
lines = lines[1..-2]

# Find longest expression in block
max_width = lines.map { |l| l.size }.max

# scope result, so we can be polite and return it
result = nil

# Process each line, why limit ourselves to one
lines.each do |line|
# get the result, be sure to keep the correct binding
result = eval(line, blk.binding)

# format and display the output line
puts "#{line} #{" " * (max_width - line.size)}=> #{result.inspect}"
end

result
end

array = [1, 2, 3, 4]
annotated do
array.map { |v| v * 2 }
array[3] + 1
end

### Needs better exception handling to be "real"
### pth

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


É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,12856 seconds with 12 queries