+ 2

Challenge question Explanation? (Ruby)

What is the output of this code? puts "sololearn".delete("sol"<<"cash" the answer is earncash. But I don't get it. I can memorize the answer but it would not help in understanding.

15th Jan 2018, 3:03 AM
Jeffrey Koh
Jeffrey Koh - avatar
4 Answers
+ 2
Thank you Faisal
15th Jan 2018, 3:19 AM
Jeffrey Koh
Jeffrey Koh - avatar
+ 2
Thank you John Wells.
15th Jan 2018, 4:48 AM
Jeffrey Koh
Jeffrey Koh - avatar
+ 1
delete removes all occurrences of the specified characters so "abcdeffedcba".delete("ae") yields "bcdffdcb". https://code.sololearn.com/cVqfxCl6E4cb
15th Jan 2018, 3:51 AM
John Wells
John Wells - avatar
- 1
I'm not exactly the best at Ruby, but it seems that the program is deleting any substring within the string 'sololearn' that consists of 'sol'. I think it may be deleting the second 'ol' because it is connected to the first 'sol' and consists of a substring within the string 'sol' (again, I'm not exactly the best at Ruby so that may be wrong). Then, once it deletes 'solol', it pushes 'cash' to the end of the string therefore resulting in 'earncash'.
15th Jan 2018, 3:15 AM
Faisal
Faisal - avatar