Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Ruby | String * Method

  • Last Updated : 07 Jan, 2020

String# * is a String class method in Ruby which is used to returns a new String containing integer copies of the receiver. Here, integer must be greater than or equal to 0.

Syntax: str * Integer

Parameters: Here, str is the required string and integer is the number of copies.

Returns: This method returns the String containing integer copies of the receiver.

Example 1:






# Ruby program to demonstrate
# the * method
     
# Taking a string and
# using the method
puts "Ruby " * 4
puts "Hey " * 2

Output:

Ruby Ruby Ruby Ruby 
Hey Hey 

Example 2:




# Ruby program to demonstrate
# the * method
     
# Taking a string and
# using the method
puts "Hello " * 0
puts "Copy " * 5

Output:


Copy Copy Copy Copy Copy 
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!