question about context, binding, eval and begin/rescue

Hello everyone,

I'm working on a tool similar to Cucumber, where a text file is parsed
line-by-line and matched to a second file which contains ruby commands to
be executed. Something like this:

* calculate one plus one
-> should equal 2
-> should *not* equal 3

Those three lines would be parsed step-definition style into

On 'should equal 2' do
  (1 + 1).should eq 2
end

In the core of the engine, I'm using eval like so:
binding = eval(cmd, binding)

So far, that gets me @ variables which is good. However, I can't use a
begin/rescue block like so:

On 'should *not* equal 3' do
  begin
    (1 + 1).should_not eq 3
  rescue Exception =>e
     return true
  end
end

My line-by-line use of eval, even when I pass the current binding, doesn't
like a line which is only a block of code. Any suggestions on how one might
pass a begin/rescue block to eval ?

Thanks :slight_smile:

···

--
A musician must make music, an artist must paint, a poet must write, if he
is to be ultimately at peace with himself.
- Abraham Maslow

Descriptions like "can't use" and "doesn't like" are not data. Please provide actual error output so we can actually help.

9958 % ruby -e 'p eval "begin; 42; rescue => e; true; end"'
42
9959 % ruby -e 'p eval "begin; 42; end"'
42

P.S. Please don't rescue Exception. Like, ever.

···

On Oct 18, 2020, at 20:32, Sean Felipe Wolfe <ether.joe@gmail.com> wrote:

So far, that gets me @ variables which is good. However, I can't use a begin/rescue block like so:

On 'should *not* equal 3' do
  begin
    (1 + 1).should_not eq 3
  rescue Exception =>e
     return true
  end
end

My line-by-line use of eval, even when I pass the current binding, doesn't like a line which is only a block of code. Any suggestions on how one might pass a begin/rescue block to eval ?

I was actually able to resolve this pretty easily. turns out you can pass
begin/rescue/end to eval with a binding, by just using semicolons.

···

On Tue, Oct 20, 2020 at 4:20 PM Ryan Davis <ryand-ruby@zenspider.com> wrote:

> On Oct 18, 2020, at 20:32, Sean Felipe Wolfe <ether.joe@gmail.com> > wrote:
>
> So far, that gets me @ variables which is good. However, I can't use a
begin/rescue block like so:
>
> On 'should *not* equal 3' do
> begin
> (1 + 1).should_not eq 3
> rescue Exception =>e
> return true
> end
> end
>
> My line-by-line use of eval, even when I pass the current binding,
doesn't like a line which is only a block of code. Any suggestions on how
one might pass a begin/rescue block to eval ?

Descriptions like "can't use" and "doesn't like" are not data. Please
provide actual error output so we can actually help.

9958 % ruby -e 'p eval "begin; 42; rescue => e; true; end"'
42
9959 % ruby -e 'p eval "begin; 42; end"'
42

P.S. Please don't rescue Exception. Like, ever.

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

--
A musician must make music, an artist must paint, a poet must write, if he
is to be ultimately at peace with himself.
- Abraham Maslow