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

Instance methods are not usable inside grammars #32

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

wdrexler
Copy link

It seems that when there is a method inside a class that returns a grammar, and that grammar uses the return values of other methods inside the class, RubySpeech throws an error.

Code looks like this: https://gist.github.com/wdrexler/f0b229d7dfb7a9629dbe
The error returned is:

/Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:277:in `method_missing': undefined local variable or method `my_method' for <item/>:RubySpeech::GRXML::Item (NameError)
    from ./rs_test.rb:14:in `block (3 levels) in grammar'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:126:in `instance_eval'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:126:in `eval_dsl_block'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:78:in `build'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:57:in `initialize'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:273:in `new'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:273:in `method_missing'
    from ./rs_test.rb:14:in `block (2 levels) in grammar'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:126:in `instance_eval'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:126:in `eval_dsl_block'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:78:in `build'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:57:in `initialize'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:273:in `new'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:273:in `method_missing'
    from ./rs_test.rb:13:in `block in grammar'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:126:in `instance_eval'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:126:in `eval_dsl_block'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/grxml.rb:25:in `block in draw'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/grxml.rb:23:in `tap'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/grxml.rb:23:in `draw'
    from ./rs_test.rb:12:in `grammar'
    from ./rs_test.rb:4:in `run'
    from ./rs_test.rb:20:in `<main>'

This error does not occur when either:

  1. The methods are declared at the top level (i.e. outside of a class) or
  2. If a local variable is assigned to the return value of the method before the RubySpeech::GRXML.draw block starts

This was tested against latest develop.

@benlangfeld
Copy link
Member

So current theory is that while this fails...

RubySpeech::GRXML.draw root: 'root', tag_format: 'semantics/1.0' do
  rule id: 'root', scope: 'public' do
    item { my_method }
  end
end

...this would work...

RubySpeech::GRXML.draw root: 'root', tag_format: 'semantics/1.0' do
  item { my_method }
end

The existing test for this needs to be extended to cover invocation in a nested block.

@wdrexler
Copy link
Author

So I've written a new test that covers invocation in a nested block. It fails, as expected. How would we fix this, @benlangfeld?

@benlangfeld
Copy link
Member

First off, submit the spec as a pull request to replace this issue (or convert this one).

As for a fix, either recursively walk up the tree in method_missing or directly grab the block context from the root of the tree. The latter is more efficient but the former more closely respects normal closure binding semantics. Thoughts?

'bar'
end
drawn_doc = GRXML.draw do
rule :id => foo

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the new Ruby 1.9 hash syntax.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I go ahead and use 1.9 syntax? The only reason I didn't in the first place was that the existing specs used 1.8 syntax.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In all touched lines here, yeah. We should migrate piece by piece.

@wdrexler
Copy link
Author

So I'm leaning towards walking up the tree instead of just grabbing the root element. Without knowing the actual performance impact of grabbing the block context of the root element (in, say, an Adhearsion app, is walking up the tree really going to be a bottleneck?), I'd say that we're better off going with the more semantically valid approach.

@benlangfeld
Copy link
Member

Go for it :)

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

Successfully merging this pull request may close these issues.

None yet

3 participants