Proc question and instance_methods

Continuing with the sets of newbie questions…

Some doubts about proc:

  1. I assume the question will be no, but is it possible to evaluate a
    piece of proc code for syntax errors? That is, I have a construct of
    the form:

    text = "some text coming from somewhere"
    code = proc { text }
    

I’d like to know whether text is valid ruby code (syntactically),
without having to run the proc. In perl, this would be done by
running eval on the code, as perl compiles to bytecode the proc even
if not running it.
But in ruby, it seems the proc is not “compiled” until run. Is there
any work-around for this?

  1. I am currently using DelimScanner, a module written by Michael
    Granger.
    Under 1.8, I get the message:

…/DelimScanner.rb:143: warning: instance_methods: parameter will
default to ‘true’ as of 1.8.1

for the following line in the code:

def_delegators :@scanner,
    *( StringScanner.instance_methods -

NeedCastingDelegators.collect {|sym| sym.id2name} )

Now, if instance_methods is to be obsolete, what should be the proper
replacement? Both methods() and public_methods() seem to return a
different (ie. more) set of functions than instance_methods().

GGarramuno wrote:

Continuing with the sets of newbie questions…

Some doubts about proc:

  1. I assume the question will be no, but is it possible to evaluate a
    piece of proc code for syntax errors? That is, I have a construct of
    the form:

    text = “some text coming from somewhere”
    code = proc { text }

I’d like to know whether text is valid ruby code (syntactically),
without having to run the proc. In perl, this would be done by
running eval on the code, as perl compiles to bytecode the proc even
if not running it.
But in ruby, it seems the proc is not “compiled” until run. Is there
any work-around for this?

If you pass the code to eval, you can then test for a SyntaxError
exception, like so:

text = “some text coming from somewhere”
begin
eval “text”
rescue SyntaxError => e
# do something when it has a syntax error
end

  1. I am currently using DelimScanner, a module written by Michael
    Granger.
    Under 1.8, I get the message:

…/DelimScanner.rb:143: warning: instance_methods: parameter will
default to ‘true’ as of 1.8.1

for the following line in the code:

def_delegators :@scanner,
*( StringScanner.instance_methods -
NeedCastingDelegators.collect {|sym| sym.id2name} )

Now, if instance_methods is to be obsolete, what should be the proper
replacement? Both methods() and public_methods() seem to return a
different (ie. more) set of functions than instance_methods().

I don’t think it’s saying that instance_methods will be obsolete, I
think it’s just saying the (optional) boolean parameter it expects will
default to true in 1.8.1. I can only assume it defaulted to false before.

  • Jamis
···


Jamis Buck
jgb3@email.byu.edu

ruby -h | ruby -e ‘a=;readlines.join.scan(/-(.)[e|Kk(\S*)|le.l(…)e|#!(\S*)/) {|r| a << r.compact.first };puts “\n>#{a.join(%q/ /)}<\n\n”’

Jamis Buck wrote:

GGarramuno wrote:

  1. I assume the question will be no, but is it possible to evaluate a
    piece of proc code for syntax errors? That is, I have a construct of
    the form:

    text = “some text coming from somewhere”
    code = proc { text }

I’d like to know whether text is valid ruby code (syntactically),
without having to run the proc. In perl, this would be done by
running eval on the code, as perl compiles to bytecode the proc even
if not running it.
But in ruby, it seems the proc is not “compiled” until run. Is there
any work-around for this?

If you pass the code to eval, you can then test for a SyntaxError
exception, like so:

text = “some text coming from somewhere”
begin
eval “text”
rescue SyntaxError => e
# do something when it has a syntax error
end

But that would execute the code, wouldn’t it?

“Rasputin” rasputnik@hellooperator.net schrieb im Newsbeitrag
news:3FFAA3AE.9000602@hellooperator.net

Jamis Buck wrote:

GGarramuno wrote:

  1. I assume the question will be no, but is it possible to evaluate a
    piece of proc code for syntax errors? That is, I have a construct of
    the form:

    text = “some text coming from somewhere”
    code = proc { text }

I’d like to know whether text is valid ruby code (syntactically),
without having to run the proc. In perl, this would be done by
running eval on the code, as perl compiles to bytecode the proc even
if not running it.
But in ruby, it seems the proc is not “compiled” until run. Is there
any work-around for this?

If you pass the code to eval, you can then test for a SyntaxError
exception, like so:

text = “some text coming from somewhere”
begin
eval “text”
rescue SyntaxError => e
# do something when it has a syntax error
end

But that would execute the code, wouldn’t it?

Yes. How about:

text = “some text coming from somewhere”
begin
eval “def foo; #{text} ; end”
rescue SyntaxError => e

do something when it has a syntax error

end

robert

Robert Klemme wrote:

text = “some text coming from somewhere”
begin
eval “def foo; #{text} ; end”
rescue SyntaxError => e

do something when it has a syntax error

end

robert

Hey, that’s clever. I’ll have to remember that trick. :slight_smile:

···


Jamis Buck
jgb3@email.byu.edu

ruby -h | ruby -e ‘a=;readlines.join.scan(/-(.)[e|Kk(\S*)|le.l(…)e|#!(\S*)/) {|r| a << r.compact.first };puts “\n>#{a.join(%q/ /)}<\n\n”’

Jamis Buck wrote:

Robert Klemme wrote:

text = “some text coming from somewhere”
begin
eval “def foo; #{text} ; end”
rescue SyntaxError => e

do something when it has a syntax error

end

robert

Hey, that’s clever. I’ll have to remember that trick. :slight_smile:

Except, if you’re paranoid…

text = "end; puts ‘Gotcha’; begin; "

Hi,

···

At Wed, 7 Jan 2004 05:25:46 +0900, Joel VanderWerf wrote:

Except, if you’re paranoid…

text = "end; puts ‘Gotcha’; begin; "

See valid_syntax? in sample/test.rb or test/ruby/test_system.rb.


Nobu Nakada