Project

General

Profile

Actions

Bug #10194

closed

OpenStruct does not throw an exception when calling missing method with no arguments.

Added by alex-pub.ruby-bugs@reflexion.net (Alex Pogrebnyak) over 9 years ago. Updated over 9 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]
[ruby-core:64712]

Description

Below is the sample that shows the problem:
#!/usr/bin/env ruby

require 'ostruct'

class TestMethodMissing < OpenStruct
  def initialize ()

    super(
      { "foo" => "bar" }
    )

  end


  def main()

    self.no_such_method

    puts "BUG! Should fail on the previous line"
  end

end

TestMethodMissing.new( ).main( )

I expect when calling self.no_such_method to generate an error.

The error can be traced to ostruct.rb method_missing implementation

Here is the dump of the method with >> showing the trouble line:

def method_missing(mid, *args) # :nodoc:
  mname = mid.id2name
  len = args.length
  if mname.chomp!('=')
    if len != 1
      raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
    end
    modifiable[new_ostruct_member(mname)] = args[0]
  elsif len == 0
>>    @table[mid]
  else
     err = NoMethodError.new "undefined method `#{mid}' for #{self}", mid, args
    err.set_backtrace caller(1)
    raise err
  end
end

There should be a check that @table[mid] does not return nil, and in case of nil raise an error

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0