When collecting user input in Ruby, is there ever a time where using chomp on that input would not be the desired behavior? That is, when would simply using gets and not gets.chompbe appropriate.
Is there a time when 'gets' would be used without 'chomp'?
102 views Asked by scobo At
1
Yes, if you specify the maximum length for input, having a "\n" included in the
getsreturn value allows you to tell if Ruby gave youxcharacters because it encountered the "\n", or becausexwas the maximum input size:vs:
If the returned string contains no trailing newline, it means there are still characters in the buffer.
Without the limit on the input, the trailing newline, or any other delimiter, probably doesn't have much use, but it's kept for consistency.