The split function

We can split the value of a string into an array of variables using the split function. Let's have a look at a quick example that demonstrates this:

irb(main):001:0> a = "mastering,metasploit"
=> "mastering,metasploit"
irb(main):002:0> b = a.split(",")
=> ["mastering", "metasploit"]
irb(main):003:0> b[0]
=> "mastering"
irb(main):004:0> b[1]
=> "metasploit"  

We can see that we have split the value of a string from the "," position into a new array, b. The "mastering,metasploit" string now forms the 0th and 1st element of the array, b, containing the values "mastering" and "metasploit", respectively.

Get Mastering Metasploit - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.