w3resource

Ruby Basic Exercises: Check whether a string starts with 'if'

Ruby Basic: Exercise-5 with Solution

Write a Ruby program to check whether a string starts with "if".

Ruby Basic Exercises: Check whether a string starts with 'if'

Ruby Code:

def start_if(str)
   return str[0, 2] == "if";
end
print start_if("ifelse"),"\n"
print start_if("endif"),"\n"

Output:

true
false

Flowchart:

Flowchart: Check whether a string starts with 'if'

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program which accept the radius of a circle from the user and compute the parameter and area.
Next: Write a Ruby program which accept the user's first and last name and print them in reverse order with a space between them.

What is the difficulty level of this exercise?