Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't change block context if defined within module context #229

Closed
jhass opened this issue Oct 13, 2014 · 6 comments
Closed

Can't change block context if defined within module context #229

jhass opened this issue Oct 13, 2014 · 6 comments

Comments

@jhass
Copy link
Member

jhass commented Oct 13, 2014

class Foo
  def foo
    p "hi"
  end
end

def a
  with Foo.new yield
end

module Bar
  a do
    foo
  end
end
Error in /home/jhass/projects/crystal/with.cr:12: instantiating 'a()'

  a do
  ^

in /home/jhass/projects/crystal/with.cr:12: instantiating 'a()'

  a do
  ^

in /home/jhass/projects/crystal/with.cr:13: undefined local variable or method 'foo'

    foo
@asterite
Copy link
Member

I'm starting to think that with ... yield is not a very good idea :-P

@jhass
Copy link
Member Author

jhass commented Oct 13, 2014

Yet the only way to make any sensible DSL possible that doesn't put all methods into the global namespace.

@asterite
Copy link
Member

But you can always carry the object as a block argument. More verbose but it's much easier to understand the code and doesn't lead to ambiguities.

@jhass
Copy link
Member Author

jhass commented Oct 13, 2014

I'm toying with a testsuite implementation that's closer to something like RSpec 3, so run order randomization, possibly single context execution, minimal global methods or core extensions, before/after callbacks and so on.
You wouldn't want a test syntax that's

Spec.describe "foo" do |c|
  c.context "bar" do |c|
   c.before(:each) do |b|
     @foo = true
   end
    c.it "baz" do |e|
      e.expect(@foo).to e.be_false
    end
  end
end

Or would you?

@asterite
Copy link
Member

I guess not :-)

@asterite
Copy link
Member

We also have code in our specs that uses that feature, something like:

assert_type("1 + 2") { int32 }

So, yes, it's a nice feature to have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants