Ruby Method of the Day - String.*

Posted by Kelly McCauley on Aug 29, 2007

Signature

string * positive_integer   #=> new_string

string.* returns a new string that is composed of positive_integer copies of string.

Examples

1
2
3
4
5
6
7
8
9
10
11
"x" * 5         #=> "xxxxx"
"x".*(5)        #=> "xxxxx"
"x" * 0         #=> ""

# * will not accept a negative integer
y = 'foo'
begin
  y * -2
rescue Exception => e
  e.inspect     #=> #<ArgumentError: negative argument>
end

Documentation Reference

www.ruby-doc.org : String.*