@@ -741,7 +741,7 @@ public RubyString eachChar(VirtualFrame frame, RubyString string, RubyProc block
741
741
742
742
}
743
743
744
- @ CoreMethod (names = "each_line" )
744
+ @ CoreMethod (names = "each_line" , optional = 1 )
745
745
public abstract static class EachLineNode extends YieldingCoreMethodNode {
746
746
747
747
public EachLineNode (RubyContext context , SourceSection sourceSection ) {
@@ -753,31 +753,41 @@ public EachLineNode(EachLineNode prev) {
753
753
}
754
754
755
755
@ 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 ) {
757
766
notDesignedForCompilation ();
758
767
759
768
final List <Object > lines = new ArrayList <>();
760
769
761
770
String str = string .toString ();
771
+ String sep = separator .toString ();
772
+
762
773
int start = 0 ;
763
774
764
775
while (start < str .length ()) {
765
- int end = str .indexOf ('\n' , start );
776
+ int end = str .indexOf (sep , start );
766
777
767
778
if (end == -1 ) {
768
779
lines .add (getContext ().makeString (str .substring (start )));
769
780
break ;
770
781
}
771
782
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 () ;
774
785
775
786
lines .add (getContext ().makeString (line ));
776
787
}
777
788
778
789
return RubyArray .fromObjects (getContext ().getCoreLibrary ().getArrayClass (), lines .toArray (new Object [lines .size ()]));
779
790
}
780
-
781
791
}
782
792
783
793
@ CoreMethod (names = "empty?" )
0 commit comments