What is the difference between these two (String#scan and String#split) in Ruby?
What is the difference between these two ( String#scan and String#split ) in Ruby?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
They serve entirely different purposes.
String#scanis used to extract matches of a regular expression from a string and return the matches in an array, whileString#splitis intended to split a string up into an array, based on a delimiter. The delimiter may be either a static string (like;to split on a single semicolon) or a regular expression (like/\s/+to split on any whitespace characters).The output of
String#splitdoesn’t include the delimiter. Rather, everything except the delimiter would be returned in the output array, while the output ofString#scanwould only include what is matched by the delimiter.Both of the above would also accept a regular expression in place of the simple string
"|".