In Ruby, you can create an empty string by initializing a variable with an empty string literal or using the String.new method without arguments.
An example to initialize a variable with an empty string using empty String literal is:
x = ''
An example to initialize a variable with an empty string using String.new
is:
x = String.new
In this example,
empty_string
and initialize it with an empty string literal ''
.empty_string
now holds an empty string.empty_string
to output using puts.# Empty String Example
empty_string = ''
puts "Empty string created: '#{empty_string}'"
Empty string created: ''
In this example,
String.new
method without arguments.empty_string
now holds an empty string.empty_string
to output using puts.# Using String.new Method
empty_string = String.new
puts "Empty string created: '#{empty_string}'"
Empty string created: ''
In this tutorial, we learned how to Create an Empty String in Ruby language using empty string literal, or String.new
, with well detailed examples.