Q:

Write a Ruby program to create a new string where "if" is added to the front of a given string. If the string already begins with "if"

0

Write a Ruby program to create a new string where "if" is added to the front of a given string. If the string already begins with "if", return the string unchanged.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Ruby Code:

def if_string(str)
    str[0, 3] == "if " ? str : "if " << str 
end
print if_string("if else"),"\n" 
print if_string("else"),"\n" 
Output:
if else
if else

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now