@@ -741,7 +741,7 @@ public RubyString eachChar(VirtualFrame frame, RubyString string, RubyProc block
741741
742742 }
743743
744- @ CoreMethod (names = "each_line" )
744+ @ CoreMethod (names = "each_line" , optional = 1 )
745745 public abstract static class EachLineNode extends YieldingCoreMethodNode {
746746
747747 public EachLineNode (RubyContext context , SourceSection sourceSection ) {
@@ -753,31 +753,41 @@ public EachLineNode(EachLineNode prev) {
753753 }
754754
755755 @ Specialization
756- public RubyArray eachLine (RubyString string ) {
756+ public RubyArray eachLine (RubyString string , @ SuppressWarnings ("unused" ) UndefinedPlaceholder separator ) {
757+ notDesignedForCompilation ();
758+
759+ final RubyBasicObject globals = getContext ().getCoreLibrary ().getGlobalVariablesObject ();
760+ final RubyString recordSeparator = (RubyString ) globals .getInstanceVariable ("$/" );
761+ return eachLine (string , recordSeparator );
762+ }
763+
764+ @ Specialization
765+ public RubyArray eachLine (RubyString string , RubyString separator ) {
757766 notDesignedForCompilation ();
758767
759768 final List <Object > lines = new ArrayList <>();
760769
761770 String str = string .toString ();
771+ String sep = separator .toString ();
772+
762773 int start = 0 ;
763774
764775 while (start < str .length ()) {
765- int end = str .indexOf ('\n' , start );
776+ int end = str .indexOf (sep , start );
766777
767778 if (end == -1 ) {
768779 lines .add (getContext ().makeString (str .substring (start )));
769780 break ;
770781 }
771782
772- String line = str .substring (start , end + 1 );
773- start = end + 1 ;
783+ String line = str .substring (start , end + sep . length () );
784+ start = end + sep . length () ;
774785
775786 lines .add (getContext ().makeString (line ));
776787 }
777788
778789 return RubyArray .fromObjects (getContext ().getCoreLibrary ().getArrayClass (), lines .toArray (new Object [lines .size ()]));
779790 }
780-
781791 }
782792
783793 @ CoreMethod (names = "empty?" )
0 commit comments