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"

  • برمجة روبي
  • برمجة

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.

الأجوبة

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
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...