OneBite.Dev - Coding blog in a bite size

split a string by empty space in Ruby

Code snippet on how to split a string by empty space in Ruby

  str = "Hello, my name is Bob"
  words = str.split(" ")

This code snippet is written using the Ruby programming language. It begins by creating a variable called ‘str’ which is assigned the value “Hello, my name is Bob”. We then use the split() method to separate the string into individual words. This method takes an argument, which is ” ” (empty space) in this case. Finally, the words are stored in an array called ‘words’. The split() method separates the string at each empty space and returns an array with the separated words. Therefore, the output of this code would be: [“Hello,”, “my”, “name”, “is”, “Bob”].

ruby