Project

General

Profile

Actions

Bug #10353

closed

weird behavior when dynamically adding method using instance_eval

Added by dyegocosta (Dyego Costa) over 9 years ago. Updated over 9 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
2.1.2
[ruby-core:65577]

Description

Earlier this week I cross by a weird bug using a gem and after digging into the problem I found this issue with MRI.
I could not reproduce it on 1.9.3-p545 nor on the 2.2.0-preview1, the issue occurs on the 2.1.2

2.1.2 :001 > class Foo
2.1.2 :002?>   def bar(msg)
2.1.2 :003?>     puts msg
2.1.2 :004?>   end
2.1.2 :005?> end
 => :bar
2.1.2 :006 > foo = Foo.new
 => #<Foo:0x007f9e1410e780>
2.1.2 :007 > foo.instance_eval("def bar2(*args, &block) bar(*args, &block) end")
 => :bar2
2.1.2 :008 > foo.respond_to?(:bar2)
 => true
2.1.2 :009 > foo.bar2
ArgumentError: wrong number of arguments (0 for 1)
        from (irb):2:in `bar'
        from (eval):1:in `bar2'
        from (irb):9
        from /Users/dyego/.rvm/rubies/ruby-2.1.2/bin/irb:11:in `<main>'
2.1.2 :010 > foo.bar2('bar')
bar
 => nil
2.1.2 :011 > foo.bar2
 => nil
2.1.2 :012 > foo.bar2
 => nil

It behaves as expected on the 1.9.3 as it does on the 2.2.0-preview1

1.9.3-p545 :001 > class Foo
1.9.3-p545 :002?>   def bar(msg)
1.9.3-p545 :003?>     puts msg
1.9.3-p545 :004?>   end
1.9.3-p545 :005?> end
 => nil
1.9.3-p545 :006 > foo = Foo.new
 => #<Foo:0x007fb114043130>
1.9.3-p545 :007 > foo.instance_eval("def bar2(*args, &block) bar(*args, &block) end")
 => nil
1.9.3-p545 :008 > foo.respond_to?(:bar2)
 => true
1.9.3-p545 :009 > foo.bar2
ArgumentError: wrong number of arguments (0 for 1)
        from (irb):2:in `bar'
        from (eval):1:in `bar2'
        from (irb):9
        from /Users/dyego/.rvm/rubies/ruby-1.9.3-p545/bin/irb:12:in `<main>'
1.9.3-p545 :010 > foo.bar2('bar')
bar
 => nil
1.9.3-p545 :011 > foo.bar2
ArgumentError: wrong number of arguments (0 for 1)
        from (irb):2:in `bar'
        from (eval):1:in `bar2'
        from (irb):11
        from /Users/dyego/.rvm/rubies/ruby-1.9.3-p545/bin/irb:12:in `<main>'

Related issues 1 (0 open1 closed)

Is duplicate of Ruby master - Bug #9622: Extra method arguments are magically swallowed after first successful call, possible method cache bugClosed03/11/2014Actions

Updated by nobu (Nobuyoshi Nakada) over 9 years ago

  • Is duplicate of Bug #9622: Extra method arguments are magically swallowed after first successful call, possible method cache bug added

Updated by nobu (Nobuyoshi Nakada) over 9 years ago

  • Description updated (diff)
  • Status changed from Open to Closed
Actions

Also available in: Atom PDF

Like0
Like0Like0