Franck Verrot

Software Craftsman, Journey Man, Open Source Enthusiast

Method Definitions in Ruby 2.1.0 Will Not Be Void Anymore

| Comments

Ruby 2.1.0 will be released at the end of the year and among other features, a change done in the parser caught my eyes…

This hack, showed as an example for the real implementation, in the related ticket does something interesting:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Index: vm.c
===================================================================
--- vm.c(リビジョン 29124)
+++ vm.c(作業コピー)
@@    -1893,7 +1893,7 @@ m_core_define_method(VALUE self, VALUE c
     REWIND_CFP({
 vm_define_method(GET_THREAD(), cbase, SYM2ID(sym), iseqval, 0, rb_vm_cref());
     });
-    return Qnil;
+    return sym;
 }

 static VALUE
@@ -1902,7 +1902,7 @@ m_core_define_singleton_method(VALUE sel
     REWIND_CFP({
 vm_define_methodethod(GET_THREAD(), cbase, SYM2ID(sym), iseqval, 1, rb_vm_cref());
     });
-    return Qnil;
+    return sym;
 }

 

Instead of returning nothing (Qnil) after a method definition, the proposal is to return the method’s name symbol.

So before revision 42337 with Ruby 2.0.0:

1
2
3
4
>> RUBY_VERSION
=> "2.0.0"
>> def foo; end
=> nil

 

And tomorrow with Ruby 2.1.0:

1
2
3
4
>> RUBY_VERSION
=> "2.1.0"
>> def foo; end
=> :foo

 

If you ever tried Rubinius, you might have noticed that it returns a CompiledMethod object:

1
2
def foo; end
=> #<Rubinius::CompiledMethod foo file=(irb)>

 

In my opinion the behavior of Rubinius makes more sense but I don’t how hard it would be to mimic that in MRI.

Comments