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 > eval bug?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
eval bug?

Réponse
 
LinkBack Outils de la discussion
Vieux 08/06/2008, 04h30   #1
jzakiya
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut eval bug?

I have this code to do timing tests:

def tm(code); s=Time.now; eval code; Time.now-s end

I use it like this:

tm 'some code'

I have this function that can take two inputs.
When I run it with just one input like this:

tm 'Pn(130)'

I can time it with no problems.

When I use a second input like this,

tm 'Pn(130, mcps)'

running in irb (1.8.6 and 1.9.0-1) it returns an
unknown method or variable message for the
second input.

Is this a bug, or expected behavior?
Can I work around this?

Thanks in advance.
  Réponse avec citation
Vieux 08/06/2008, 06h41   #2
miles.sterrett@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: eval bug?

On Jun 7, 11:30pm, jzakiya <jzak...@mail.com> wrote:
> I have this code to do timing tests:
>
> def tm(code); s=Time.now; eval code; Time.now-s end
>
> I use it like this:
>
> tm 'some code'
>
> I have this function that can take two inputs.
> When I run it with just one input like this:
>
> tm 'Pn(130)'
>
> I can time it with no problems.
>
> When I use a second input like this,
>
> tm 'Pn(130, mcps)'
>
> running in irb (1.8.6 and 1.9.0-1) it returns an
> unknown method or variable message for the
> second input.
>
> Is this a bug, or expected behavior?
> Can I work around this?
>
> Thanks in advance.


I don't believe that is a bug. At the time that 'eval' is running the
code, the variable 'mcps' is out of scope (assuming it is a
variable). You may need to look into bindings:
http://onestepback.org/index.cgi/Tec...yBindings.rdoc.

Let me know if I'm way off base. I do hope that s.

--
MilesZS
  Réponse avec citation
Vieux 08/06/2008, 17h10   #3
Mike Cargal
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: eval bug?

I think a more "ruby" implementation of tm would be...

def tm
start = Time.now
yield
return Time.now - start
end

duration = tm {
# any code you want to time goes here
}
On Jun 7, 2008, at 11:33 PM, jzakiya wrote:

> I have this code to do timing tests:
>
> def tm(code); s=Time.now; eval code; Time.now-s end
>
> I use it like this:
>
> tm 'some code'
>
> I have this function that can take two inputs.
> When I run it with just one input like this:
>
> tm 'Pn(130)'
>
> I can time it with no problems.
>
> When I use a second input like this,
>
> tm 'Pn(130, mcps)'
>
> running in irb (1.8.6 and 1.9.0-1) it returns an
> unknown method or variable message for the
> second input.
>
> Is this a bug, or expected behavior?
> Can I work around this?
>
> Thanks in advance.
>



  Réponse avec citation
Vieux 08/06/2008, 20h22   #4
Mike Cargal
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: eval bug?

of course... it's very "unruby" to say return, so...

def tm
start = Time.now
yield
Time.now - start
end

or (if you seem to prefer the one line syntax...

def tm; s=Time.now;yield;Time.now-s end

On Jun 8, 2008, at 12:10 PM, Mike Cargal wrote:

> I think a more "ruby" implementation of tm would be...
>
> def tm
> start = Time.now
> yield
> return Time.now - start
> end
>
> duration = tm {
> # any code you want to time goes here
> }
> On Jun 7, 2008, at 11:33 PM, jzakiya wrote:
>
>> I have this code to do timing tests:
>>
>> def tm(code); s=Time.now; eval code; Time.now-s end
>>
>> I use it like this:
>>
>> tm 'some code'
>>
>> I have this function that can take two inputs.
>> When I run it with just one input like this:
>>
>> tm 'Pn(130)'
>>
>> I can time it with no problems.
>>
>> When I use a second input like this,
>>
>> tm 'Pn(130, mcps)'
>>
>> running in irb (1.8.6 and 1.9.0-1) it returns an
>> unknown method or variable message for the
>> second input.
>>
>> Is this a bug, or expected behavior?
>> Can I work around this?
>>
>> Thanks in advance.
>>

>
>


Mike Cargal
mike@cargal.net
http://www.cargal.net




  Réponse avec citation
Vieux 09/06/2008, 01h07   #5
Ben Bleything
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: eval bug?

On Mon, Jun 09, 2008, Mike Cargal wrote:
> of course... it's very "unruby" to say return, so...


I don't think this is true at all. There are certainly people who
prefer the implicit return, but likewise there are people who do not.
I'm in the latter camp, as should be obvious

Ben

  Réponse avec citation
Vieux 09/06/2008, 23h08   #6
jzakiya
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: eval bug?

On Jun 8, 3:22 pm, Mike Cargal <m...@cargal.net> wrote:
> of course... it's very "unruby" to say return, so...
>
> def tm
> start = Time.now
> yield
> Time.now - start
> end
>
> or (if you seem to prefer the one line syntax...
>
> def tm; s=Time.now;yield;Time.now-s end
>
> On Jun 8, 2008, at 12:10 PM, Mike Cargal wrote:
>
>
>
> > I think a more "ruby" implementation of tm would be...

>
> > def tm
> > start = Time.now
> > yield
> > return Time.now - start
> > end

>
> > duration = tm {
> > # any code you want to time goes here
> > }
> > On Jun 7, 2008, at 11:33 PM, jzakiya wrote:

>
> >> I have this code to do timing tests:

>
> >> def tm(code); s=Time.now; eval code; Time.now-s end

>
> >> I use it like this:

>
> >> tm 'some code'

>
> >> I have this function that can take two inputs.
> >> When I run it with just one input like this:

>
> >> tm 'Pn(130)'

>
> >> I can time it with no problems.

>
> >> When I use a second input like this,

>
> >> tm 'Pn(130, mcps)'

>
> >> running in irb (1.8.6 and 1.9.0-1) it returns an
> >> unknown method or variable message for the
> >> second input.

>
> >> Is this a bug, or expected behavior?
> >> Can I work around this?

>
> >> Thanks in advance.

>
> Mike Cargal
> m...@cargal.nethttp://www.cargal.net


Thanks, this works great. I learned something. ;-)

It sort of acts like a lamda.

But is that still a bug in eval?

Jabari
  Réponse avec citation
Vieux 09/07/2008, 05h49   #7
Roger Pack
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: eval bug?

> But is that still a bug in eval?
>
> Jabari


My question is why does
eval("b = 6")
print b

err and

b = nil
eval("b = 6")
print b

succeed? Eval has its own sub-scope?

Note also that
eval("b = 6", binding)
print b

also fails.

Is this intended, or just a side-effect of the 'parse then execute'
stratagem?

Also as an interesting note,
if you have
code1
eval("code")
code2

with 1.8.6, It appears to 'reparse' code2 after the eval has completed.
I assume to make sure that it has accommodated for new variables or
something. Except that you actually can't add new local variables, so
it must be something else.

Thanks.
-R
--
Posted via http://www.ruby-forum.com/.

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


É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,14663 seconds with 15 queries