Home » Ruby programming

String creation in Ruby

Ruby | String creation: Here, we are going to learn the different ways to create string objects in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on April 10, 2020

Ruby | String creation

There are various methods through which we can create string objects. We will understand them with the help of syntaxes and their supporting program codes in the rest of the content.

Method 1: new(str = "")

This method is a public class method defined and declared for String class in Ruby's library. This method works in a way that it returns a new String instance which is a duplicate of str String object. You will have to invoke this method with the help of a new keyword along with the String class.

Syntax:

    String.new(str = "")

Example:

=begin
  Ruby program to demonstrate String generation		
=end

puts "String.new(str) method implementation"

String.new(str="Hello there! You are at includehelp.com")

puts "The string object generated is: #{str}"

Output

String.new(str) method implementation
The string object generated is: Hello there! You are at includehelp.com

Explanation:

In the above code, you can observe that we are generating an instance of String class with the help of the above method. We have invoked the new method along with String class and a duplicate copy of str has been generated.

Method 2: new(str="",encoding:enc)

This method is a variant of the method which we have discussed in method 1. You can pass an optional encoding argument along with the method which specifies the encoding methodology used in the new String object. If you are not specifying the encoding parameter then by default ASCII 8 bit encoding is used.

Syntax:

    String.new(str = "")
    str.encode(Encoding::enc)

Example:

=begin
  Ruby program to demonstrate String generation		
=end

puts "String.new(str) method implementation"

String.new(str="Hello there! You are at includehelp.com")
puts "Encoding used is: #{str.encoding}"

puts "String.new(str) and encode method implementation"

String.new(str1="Hello there! You are at includehelp.com")
string=str1.encode(Encoding::ISO_8859_1)
puts "Encoding used is: #{string.encoding}"

Output

String.new(str) method implementation
Encoding used is: UTF-8
String.new(str) and encode method implementation
Encoding used is: ISO-8859-1

Explanation:

In the above code, you can observe that we can set the type of encoding we want to use in our string object with the help of the method discussed above. You can see that the default encoding is UTF-8 and we have set it to ISO_8859_1.

Method 3: new(str="",capacity)

This method is a variant of the methods discussed above. You can set the capacity of Strings as well for specifying the size of the internal buffer. This improves performance when the String object is concatenated many times.

Syntax:

    String.new(str = "",capacity:size)

Example:

=begin
  Ruby program to demonstrate String generation		
=end

puts "String.new(str,capacity) method implementation"

String.new(str="Hello there! You are at includehelp.com",capacity:120)
puts "Encoding used is: #{str.encoding} and str is #{str}"

Output

String.new(str,capacity) method implementation
Encoding used is: UTF-8 and str is Hello there! You are at includehelp.com

Explanation:

In the above code, you can observe that how capacity of the String can be set with the help of new method.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.