slice a string in Ruby
Code snippet on how to slice a string in Ruby
string = "Hello World!"
sliced_string = string[0,5]
This Ruby code creates a variable called string which is assigned the value of “Hello World!“. The next line takes the first 5 characters of the string and assigns it to a new variable called sliced_string. This ‘slicing’ is done using the index brackets and the slice operator (the comma in this example). The index of the first character is 0 and the index of the last character is 5 so they are used as the parameters of the slice operator. The result is the string “Hello” being stored in the variable sliced_string.