OneBite.Dev - Coding blog in a bite size

cut a string in Ruby

Code snippet on how to cut a string in Ruby

  str = "Hello World"
  new_str = str.slice(0, 5)

This code cuts a string in Ruby. The code first defines a string variable and assigns it a value of “Hello World”. Then, it defines another variable, new_str which contains the first 5 characters of the original string. The slice() method takes two parameters - the starting index and the length of the string to be cut. In this case, 0 indicates the starting index which is the first character of the original string and 5 indicates the length of the string which is 5 characters.

ruby