mardi 3 janvier 2017

Can someone explain why the .split method in Ruby works this way?

Why does .split create an empty character when its argument is the first letter of the string, and it doesn't do the same when the argument is the last letter of the string? In the second example, doesn't it "say", since nothing is on my right I'll output "" ? (Is there a 'nil' at the end of the string?)

I know this is not a very relevant question, however, I'd like to understand why the method behaves this way. Thank you!

string = "aware"
string.split("a") --> # outputs: ["", "w", "re"]
string.split("e") --> # outputs: ["awar"]

Aucun commentaire:

Enregistrer un commentaire