0

Can someone tell me what I am doing wrong here, please?

wtf.rb

require 'minitest/autorun' class MyPlugin def self.valid_plugin?(plugin_class) begin plugin_class.ancestors.include?(self) rescue NameError false end end end class MyPluginTest < Minitest::Test def test_valid_plugin_handles_missing_constant assert_equal false, MyPlugin.valid_plugin?(MyMissingConstant) end end 

Environment

$ ruby -v ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0] $ gem list --local *** LOCAL GEMS *** bigdecimal (1.2.4) bundler (1.7.3) io-console (0.4.2) json (1.8.1) minitest (5.4.2, 4.7.5) psych (2.0.5) rake (10.1.0) rdoc (4.1.0) test-unit (2.1.2.0) $ ruby wtf.rb Run options: --seed 32486 # Running: E Finished in 0.001228s, 814.3322 runs/s, 0.0000 assertions/s. 1) Error: MyPluginTest#test_valid_plugin_handles_missing_constant: NameError: uninitialized constant MyPluginTest::MyMissingConstant wtf.rb:15:in `test_valid_plugin_handles_missing_constant' 1 runs, 0 assertions, 0 failures, 1 errors, 0 skips 

1 Answer 1

3

MyMissingConstant is evaluated before the valid_plugin? method is called. You have to either rescue at the call site, or pass a string and look up the constant within your method.

Kernel.const_get is probably the simplest way to do that. For more detail, look at question slike this one:

How to convert a string to a constant in Ruby?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.