×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

String creation in Ruby

By IncludeHelp Last updated : December 01, 2024

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.

String creation using 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.

String creation using 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.

String creation using 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.