Skip to content


In Ruby 2.0, String#lines returns an Array, rather than an Enumerator #945

merged 1 commit into from

2 participants


Ruby 2.0 has changed the return type of String#lines to Array, from Enumerator. The difference in behaviour between MRI 2.0.0 and JRuby 1.7.4 with the --2.0 argument supplied is:

In MRI 2.0.0-p247:
sinjo@widdershins:~/projects/jruby-file-encoding$ rvm use 2.0.0
Using /home/sinjo/.rvm/gems/ruby-2.0.0-p247

sinjo@widdershins:~/projects/jruby-file-encoding$ ./demo input_file.txt 
["first line\r\n", "second line\r\n", "third line"]
"first line\r\n"


In JRuby 1.7.4 with JRUBY_OPTS=--2.0:
sinjo@widdershins:~/projects/jruby-file-encoding$ rvm use jruby
Using /home/sinjo/.rvm/gems/jruby-1.7.4

sinjo@widdershins:~/projects/jruby-file-encoding$ ./demo input_file.txt 
[#<Enumerator: "first line\r\nsecond line\r\nthird line":lines>]
#<Enumerator: "first line\r\nsecond line\r\nthird line":lines>

Output taken from this gist

@headius headius merged commit 436b5f7 into jruby:master

1 check passed

Details default The Travis CI build passed
JRuby Team member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 13 additions and 0 deletions.
  1. +13 −0 core/src/main/java/org/jruby/
13 core/src/main/java/org/jruby/
@@ -82,6 +82,7 @@
import static org.jruby.CompatVersion.RUBY1_8;
import static org.jruby.CompatVersion.RUBY1_9;
+import static org.jruby.CompatVersion.RUBY2_0;
import static org.jruby.RubyEnumerator.enumeratorize;
import static org.jruby.anno.FrameField.BACKREF;
import static org.jruby.runtime.Helpers.invokedynamic;
@@ -7179,6 +7180,18 @@ public IRubyObject lines(ThreadContext context, IRubyObject arg, Block block) {
enumeratorize(context.runtime, this, "lines", arg);
+ @JRubyMethod(name = "lines", compat = RUBY2_0)
+ public IRubyObject lines20(ThreadContext context, Block block) {
+ return block.isGiven() ? each_lineCommon19(context, block) :
+ enumeratorize(context.runtime, this, "lines").callMethod(context, "to_a");
+ }
+ @JRubyMethod(name = "lines", compat = RUBY2_0)
+ public IRubyObject lines20(ThreadContext context, IRubyObject arg, Block block) {
+ return block.isGiven() ? each_lineCommon19(context, arg, block) :
+ enumeratorize(context.runtime, this, "lines", arg).callMethod(context, "to_a");
+ }
private IRubyObject each_lineCommon19(ThreadContext context, Block block) {
return each_lineCommon19(context, context.runtime.getGlobalVariables().get("$/"), block);
Something went wrong with that request. Please try again.