OneBite.Dev - Coding blog in a bite size

count a string length in Ruby

Code snippet on how to count a string length in Ruby

  str = "This is a string"
  len = str.length
  puts len

This code is written in the Ruby programming language. It shows how to count the length of a string. The first line assigns the string “This is a string” to a variable named str. The second line calls the length method on the new variable str and stores that value in a new variable called len. The last line prints out the value stored in len. In this case the output would be 16, which is the length of the string.

ruby