prepend a string to the beginning of another string in ruby

To prepend a string to the beginning of another string in Ruby, you can use the prepend method or the + operator. Here are examples of both methods:

main.rb
# Using prepend method
str1 = "world"
str1.prepend("Hello ")
puts str1   #=> "Hello world"

# Using + operator
str2 = "world"
str2 = "Hello " + str2
puts str2   #=> "Hello world"
179 chars
10 lines

Both of these methods will produce the same result, which is Hello world. The prepend method adds the string at the beginning of the receiver, while the + operator join both strings, returning a new string that has the combined value of both.

gistlibby LogSnag