.split method

August 4, 2016   

I've had some students ask me about the .split method in ruby :D

Here's a brief rundown of how it works. Hopefully the examples do a good job of explaining what it does :)

momos = “I love momos”
momos.split(“ ”)
# => [“I”, “love”, “momos”]
# returns an array of strings seperated by “ ”

string_2 = “A, bc, def, ghi” string_2.split(“,”) # => [“A”, “ bc”, “ def”, “ ghi”] # here, we are splitting the array with “,” # then we are storing them in an array

string_3 = “abcdefgabcdefg” string_3.split(“a”) # => [“”, “bcdefg”, “bcdefg”] # here, we are splitting the array by “a”, so we get this result

Originally posted in TECHRISE Community at www.techrise.me