Skip to content

Commit

Permalink
Fix infinite recursion problem on Ruby 1.9.
Browse files Browse the repository at this point in the history
Check whether object responds to pluralized method before sending it to
avoid recursing into method_missing until the stack overflows. The
resulting SystemStackError was caught by the rescue statement modifier
in Ruby 1.8 (with a noticeable pause during the recursion), but isn't
in Ruby 1.9.

Also, remove an assertion that fails now that respond_to? is called.
Could find a way to fix it, but it's redundant.

This fixes test_method_missing on Ruby 1.9; however, there are still 4
unit tests failing on 1.9 for other reasons.
  • Loading branch information
Todd Thomas committed Feb 29, 2012
1 parent 3b8fd45 commit 934534a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 9 deletions.
4 changes: 3 additions & 1 deletion lib/structures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ module Singular
# Object contains an array called 'alphas', which looks like [:a, :b, :c].
# Call object.alpha and :a is returned.
def method_missing(name, *args)
return self.send(:"#{name}s").first rescue super(name, *args)
plural_name = :"#{name}s"
return self.send(plural_name).first if respond_to?(plural_name)
super(name, *args)
end

def respond_to?(x, y=false)
Expand Down
8 changes: 0 additions & 8 deletions test/test_feednormalizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,6 @@ def test_yaml
def test_method_missing
assert_raise(NoMethodError) { FeedNormalizer::FeedNormalizer.parse(XML_FILES[:rss20]).nonexistent }

assert_raise(NoMethodError) do
obj = Object.new
class << obj
include Fn::Singular
end
obj.foo
end

# Another test of Singular's method_missing: sending :flatten to a 2-D array of FeedNormalizer::Entrys
# causes :to_ary to be sent to the Entrys.
assert_nothing_raised { [[Fn::Entry.new], [Fn::Entry.new]].flatten }
Expand Down

0 comments on commit 934534a

Please sign in to comment.