Is there any method to do syntax checking of a specific block but not
whole file?
···
--
Posted via http://www.ruby-forum.com/.
Is there any method to do syntax checking of a specific block but not
whole file?
--
Posted via http://www.ruby-forum.com/.
If the code allows it, you could isolate the block into a separate file
while checking.
Apart from that, I think you'd have to rely on options in your IDE.
--
Posted via http://www.ruby-forum.com/.
Could you perhaps explain a little of why you want to do this? If you want
to check syntax programmatically, for example, you could eval the code as a
string, rescuing the result and checking for any syntax errors, etc. This
is what irb and pry are doing, effectively, at a very very basic level.
On Wed, Dec 4, 2013 at 6:30 AM, Asmita Chauhan <lists@ruby-forum.com> wrote:
Is there any method to do syntax checking of a specific block but not
whole file?
Actually, I want to separate out parsing(for syntax checking) and
execution.
In our Application, we are using eval, instace_eval, class_eval etc.
what i want to do is that parse code block for syntax checking at early
stage and defer the execution at later stage.
--
Posted via http://www.ruby-forum.com/.
Thanks Robert Klemme
I would try this in my application. Looks like this would be helpful.
--
Posted via http://www.ruby-forum.com/.
in your repl you can read in the current file and parse with regex. Pry has
it built in nowdays. I've used one of my own scripts for years which
defeats the repl -> editor -> run cycle. Ruby's ability to reopen classes
makes it ideal for this style of computation and experimentation.
On Wed, Dec 4, 2013 at 6:30 AM, Asmita Chauhan <lists@ruby-forum.com> wrote:
Is there any method to do syntax checking of a specific block but not
whole file?--
Posted via http://www.ruby-forum.com/\.
begin; eval(code); rescue Object; $!; end }.value; p 42' "... blech"
#<SyntaxError: (eval):1: syntax error, unexpected tDOT3
... blech
^>
42
a:~ $ ruby -e 'code = ARGV.shift; p Thread.new(code){|code| $SAFE = 12;
--
Posted via http://www.ruby-forum.com/.
You can create a tempfile and then run
ruby -c filename # for syntax check
ruby -w filename # for warnings
ruby -wc filename # for both
I'd love to learn if there's a programmatic way to do this within Ruby,
but I haven't found it in the source code, and I'd rather than bring in
the rubinius melbourne parser.
This is how sandi_meter does it
Open3.capture3('ruby -wc', stdin_data: source)
But that's potentially pretty buggy on non *NIX OS's or non-mri rubies.
Does that possibly help? (Does anyone know how to do this within ruby?)
--
Posted via http://www.ruby-forum.com/.
referring to?
- JRuby bug Open3.capture3 error on Windows · Issue #1187 · jruby/jruby · GitHub
- Just anecdotal reports from talking to people. Back 1.8 days,
win32-open3 was required, but apparently no longer in 1.9+
Just the evidence of these bugs are why I'm not sure.
On Sat, Dec 14, 2013 at 11:01 PM, Stu <stu@rubyprogrammer.net> wrote:
Open3.capture3('ruby -wc', stdin_data: source)
But that's potentially pretty buggy on non *NIX OS's or non-mri rubies.
Why wouldn't that work on windows? Better yet which non-mri ruby are you
--
Posted via http://www.ruby-forum.com/\.
That's a great idea. Especially if the user wants to verify system calls or FileUtils.rm_rf and the like.
On Dec 4, 2013, at 16:25, tamouse pontiki <tamouse.lists@gmail.com> wrote:
On Wed, Dec 4, 2013 at 6:30 AM, Asmita Chauhan <lists@ruby-forum.com> wrote:
Is there any method to do syntax checking of a specific block but not
whole file?Could you perhaps explain a little of why you want to do this? If you want to check syntax programmatically, for example, you could eval the code as a string, rescuing the result and checking for any syntax errors, etc. This is what irb and pry are doing, effectively, at a very very basic level.
Sure, just like you can do with irb and pry.
On Wed, Dec 4, 2013 at 7:18 PM, Ryan Davis <ryand-ruby@zenspider.com> wrote:
On Dec 4, 2013, at 16:25, tamouse pontiki <tamouse.lists@gmail.com> wrote:
> On Wed, Dec 4, 2013 at 6:30 AM, Asmita Chauhan <lists@ruby-forum.com> > wrote:
> Is there any method to do syntax checking of a specific block but not
> whole file?
>
> Could you perhaps explain a little of why you want to do this? If you
want to check syntax programmatically, for example, you could eval the code
as a string, rescuing the result and checking for any syntax errors, etc.
This is what irb and pry are doing, effectively, at a very very basic level.That's a great idea. Especially if the user wants to verify system calls
or FileUtils.rm_rf and the like.
Just wrap the piece of code:
irb(main):007:0> code = '1 + 2'
=> "1 + 2"
irb(main):008:0> compiled = eval("lambda { #{code} }")
=> #<Proc:0x0000060036f988@(eval):1 (lambda)>
irb(main):009:0> compiled.call
=> 3
Now with a broken piece:
irb(main):010:0> code = '1 + '
=> "1 + "
irb(main):011:0> compiled = eval("lambda { #{code} }")
SyntaxError: (eval):1: syntax error, unexpected '}'
lambda { 1 + }
^
from (irb):11:in `eval'
from (irb):11
from /usr/bin/irb:12:in `<main>'
Cheers
robert
On Thu, Dec 5, 2013 at 7:09 AM, Asmita Chauhan <lists@ruby-forum.com> wrote:
Actually, I want to separate out parsing(for syntax checking) and
execution.
In our Application, we are using eval, instace_eval, class_eval etc.
what i want to do is that parse code block for syntax checking at early
stage and defer the execution at later stage.
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
Watch out for #eval; this never finishes:
ruby -e 'code = ARGV.shift; p Thread.new(code){|code| $SAFE = 12;
begin; eval(code); rescue Object; $!; end }.value; p 42' 'loop {}'
On 12/10/2013 02:44 PM, dojo 4. wrote:
a:~ $ ruby -e 'code = ARGV.shift; p Thread.new(code){|code| $SAFE = 12;
begin; eval(code); rescue Object; $!; end }.value; p 42' "... blech"
#<SyntaxError: (eval):1: syntax error, unexpected tDOT3
... blech
^>
42
I'm sorry, guys, this wrapped around my brain and I couldn't unwrap it so
far:
In
def check_syntax src
catch(:good) do
eval("throw :good; #{src}")
end
true
rescue SyntaxError
false
end
Why insert the *throw* before the code to be checked? If *throw* comes
first, shouldn't it always jump out of the *eval* before checking *src*?
Sorry for slowing the conversation down, just seeing an opportunity to
learn =]
2013/12/11 Joel VanderWerf <joelvanderwerf@gmail.com>
On 12/10/2013 02:44 PM, dojo 4. wrote:
a:~ $ ruby -e 'code = ARGV.shift; p Thread.new(code){|code| $SAFE = 12;
begin; eval(code); rescue Object; $!; end }.value; p 42' "... blech"
#<SyntaxError: (eval):1: syntax error, unexpected tDOT3
... blech
^>
42Watch out for #eval; this never finishes:
ruby -e 'code = ARGV.shift; p Thread.new(code){|code| $SAFE = 12;
begin; eval(code); rescue Object; $!; end }.value; p 42' 'loop {}'
--
Thyago Barbosa Rodrigues [Natal-RN/Brasil]
"Who loves not wine, women and song,
remains a fool his whole life long"
(Martin Luther [Martinho Lutero], Teólogo Alemão)
"Phantasie ist wichtiger als Wissen, denn Wissen ist begrenzt." (Albert
Einstein)
Why wouldn't that work on windows? Better yet which non-mri ruby are you
referring to?
On Thu, Dec 12, 2013 at 1:38 PM, Benjamin F. <lists@ruby-forum.com> wrote:
You can create a tempfile and then run
ruby -c filename # for syntax check
ruby -w filename # for warnings
ruby -wc filename # for bothI'd love to learn if there's a programmatic way to do this within Ruby,
but I haven't found it in the source code, and I'd rather than bring in
the rubinius melbourne parser.This is how sandi_meter does it
Open3.capture3('ruby -wc', stdin_data: source)
But that's potentially pretty buggy on non *NIX OS's or non-mri rubies.
Does that possibly help? (Does anyone know how to do this within ruby?)
--
Posted via http://www.ruby-forum.com/\.
Except that's not what they do. If they evaled, then they'd delete all your files before you got to type "if false".
On Dec 4, 2013, at 20:52, tamouse pontiki <tamouse.lists@gmail.com> wrote:
Sure, just like you can do with irb and pry.
The other one I've seen looks like:
def check_syntax src
catch(:good) do
eval("throw :good; #{src}")
end
true
rescue SyntaxError
false
end
and you can, of course, use RubyParser.new.parse(src).
On Dec 5, 2013, at 1:14, Robert Klemme <shortcutter@googlemail.com> wrote:
On Thu, Dec 5, 2013 at 7:09 AM, Asmita Chauhan <lists@ruby-forum.com> wrote:
Actually, I want to separate out parsing(for syntax checking) and
execution.
In our Application, we are using eval, instace_eval, class_eval etc.
what i want to do is that parse code block for syntax checking at early
stage and defer the execution at later stage.Just wrap the piece of code:
irb(main):007:0> code = '1 + 2'
=> "1 + 2"
irb(main):008:0> compiled = eval("lambda { #{code} }")
=> #<Proc:0x0000060036f988@(eval):1 (lambda)>
irb(main):009:0> compiled.call
=> 3
It is to ensure that you don't execute the code just yet. You want
only to check the syntax, not execute it.
eval will parse the code, raising a SyntaxError if something is
syntactically wrong, then start executing it. First thing is the
throw, so it throws, skipping all the rest.
Jesus.
On Wed, Dec 11, 2013 at 11:08 AM, Thyago Barbosa Rodrigues <thyagobr@gmail.com> wrote:
I'm sorry, guys, this wrapped around my brain and I couldn't unwrap it so
far:In
def check_syntax src
catch(:good) do
eval("throw :good; #{src}")
end
true
rescue SyntaxError
false
endWhy insert the throw before the code to be checked? If throw comes first,
shouldn't it always jump out of the eval before checking src?
Sorry for slowing the conversation down, just seeing an opportunity to learn
Use BEGIN to be a bit safer:
def check_syntax_unsafe src
catch(:good) do
eval("throw :good; #{src}")
end
true
rescue SyntaxError
false
end
def check_syntax_safe src
catch(:good) do
eval("BEGIN {throw :good}; #{src}")
end
true
rescue SyntaxError
false
end
puts "UNSAFE"
p check_syntax_unsafe %{
BEGIN {puts "haha"}
}
puts
puts "SAFE"
p check_syntax_safe %{
BEGIN {puts "haha"}
}
__END__
Output:
UNSAFE
haha
true
SAFE
true
On 12/06/2013 01:49 PM, Ryan Davis wrote:
def check_syntax src
catch(:good) do
eval("throw :good; #{src}")
end
true
rescue SyntaxError
false
end
This is nice! Thank you for that.
Kind regards
robert
On Fri, Dec 6, 2013 at 10:49 PM, Ryan Davis <ryand-ruby@zenspider.com> wrote:
The other one I've seen looks like:
def check_syntax src
catch(:good) do
eval("throw :good; #{src}")
end
true
rescue SyntaxError
false
end
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/