Skip to content

Commit 128a892

Browse files
committed
Update method missing handling to allow method_missing to accept one argument
1 parent fdabe1f commit 128a892

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

spec/ruby/core/kernel/method_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,28 @@
3434
m.should be_an_instance_of(Method)
3535
m.call(1, 2, 3).should == "Done handled_privately([1, 2, 3])"
3636
end
37+
38+
it "can call a #method_missing accepting zero or one arguments" do
39+
cls = Class.new do
40+
def respond_to_missing?(name, *)
41+
name == :foo or super
42+
end
43+
def method_missing
44+
:no_args
45+
end
46+
end
47+
m = cls.new.method(:foo)
48+
-> { m.call }.should raise_error(ArgumentError)
49+
50+
cls = Class.new do
51+
def respond_to_missing?(name, *)
52+
name == :bar or super
53+
end
54+
def method_missing(m)
55+
m
56+
end
57+
end
58+
m = cls.new.method(:bar)
59+
m.call.should == :bar
60+
end
3761
end

src/main/java/org/truffleruby/language/methods/SharedMethodInfo.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
*/
1010
package org.truffleruby.language.methods;
1111

12-
import java.util.Arrays;
13-
1412
import org.truffleruby.RubyLanguage;
1513
import org.truffleruby.core.klass.RubyClass;
1614
import org.truffleruby.core.module.RubyModule;
@@ -19,7 +17,6 @@
1917
import org.truffleruby.language.RubyGuards;
2018
import org.truffleruby.language.backtrace.BacktraceFormatter;
2119
import org.truffleruby.parser.ArgumentDescriptor;
22-
import org.truffleruby.parser.ArgumentType;
2320

2421
import com.oracle.truffle.api.source.SourceSection;
2522

@@ -76,10 +73,6 @@ public SharedMethodInfo forDefineMethod(RubyModule declaringModule, String metho
7673
}
7774

7875
public SharedMethodInfo convertMethodMissingToMethod(RubyModule declaringModule, String methodName) {
79-
final ArgumentDescriptor[] oldArgs = getArgumentDescriptors();
80-
final ArgumentDescriptor[] newArgs = Arrays.copyOfRange(oldArgs, 1, oldArgs.length);
81-
newArgs[0] = new ArgumentDescriptor(ArgumentType.anonrest);
82-
8376
return new SharedMethodInfo(
8477
sourceSection,
8578
staticLexicalScope,
@@ -88,7 +81,7 @@ public SharedMethodInfo convertMethodMissingToMethod(RubyModule declaringModule,
8881
blockDepth,
8982
moduleAndMethodName(declaringModule, methodName),
9083
notes,
91-
newArgs);
84+
ArgumentDescriptor.ANY);
9285
}
9386

9487
public SharedMethodInfo withArity(Arity newArity) {

src/main/java/org/truffleruby/parser/ArgumentDescriptor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public final class ArgumentDescriptor {
3434
/** The name of the argument */
3535
public final String name;
3636

37+
public static final ArgumentDescriptor[] ANY = { new ArgumentDescriptor(ArgumentType.anonrest) };
3738
public static final ArgumentDescriptor[] AT_LEAST_ONE = {
3839
new ArgumentDescriptor(ArgumentType.anonreq),
3940
new ArgumentDescriptor(ArgumentType.anonrest) };

0 commit comments

Comments
 (0)