RUBY (part-3)

RUBY (part-3)

Introduction

In the previous 2 sections we just only concentrated on how to get the output when the input is given in the code itself. But in this, we will try to interact with the user from the console.

Prompting

It's not a special coding concept to learn but in simple words, we can say that telling the user what has to do to get input from the user, we will first need to print on the screen.

print "It is the prompted message"
#After running the program in the console the print message will appear
output:
It is the prompted message

Getting the input

Prompting only showing something that we wish on the console screen and it is not storing user response. To make it possible ruby allows us through the gets method. It is used to take input from the user.

puts "Enter you name"  #promting
name = gets  #Takes the input from the user
puts name  # prints that in the console screen

String Interpolation

It was an interestingly unique way of representing the strings in the formatted way.In general, terms wouldn't be very cool if we can add user-entered data in the print statement. First look at the conventional way (string concatenation).

puts "Enter the first name" #Malavi
first_name = gets
puts "Enter the second name" #Pande
second_name = gets
puts "My first name is" + first_name + "last name is " + last_name

output:
My first name isMalavilast name isPande

OMG !!! The output seems clumsy and awkward. But thanks to ruby we have a superb alternative to make this even better. string interpolation techniques allow us to write clean and clear code.

The syntax for this is #{variable_data_we_want_to_interpret}

Let's see the same code using this.

puts "Enter the first name" #Malavi
first_name = gets
puts "Enter the second name" #Pande
second_name = gets
puts "My first name is #{first_name} last name is #{last_name}"

output:
My first name is Malavi last name is Pande

Control Flow

If you are new to the programming DOn't get overwhelmed by seeing the terms. It is as simple as the name. As we know program going to execute from the top to the bottom. While executing because of a few keywords the flow control will change. We are going to talk about what exactly the keywords are.

If you have some good knowledge of any programming language I already know what I am going to write. They are

If

In the whole code if we write the if condition ruby executes the indentation block of code corresponding to the if condition. Look that into clearly using code

if 10 > 1 #Condition True
    puts true
end

Note: In ruby, we have to close the conditional statements with the end keyword.

else

If only executes when the condition is true what if the condition is false? else is the alternative for this. The major difference in both of them is we can't able to place any condition in the else statement.

if 10 < 2 #condition False it executes the else statement
    puts "it is true"
else
    puts "it is false"
end

elsif

Both if and else are working on a true or false basis, what if we want to check multiple conditions on the single input we need to define and check multiple conditions right? But what is the exact process for that? for the question elsif going to answer single-handedly. Keep an eye on the below code for better understanding.

Unlike the else statement, it allows us to define the condition.

num = gets #11
if num > 10: #This condition going to execute
    puts "#{num} Greater than 10" 
elsif num < 10:
    puts "#{num} Less than 10"
else 
    puts "Invalid"
end
output:
11 Greater than 10

Note: we can define as many elsif conditions as possible.

Conclusion

This is the end of the blog readers hope you learn something. In my opinion, ruby seems like python with its simple syntax and clean code. Let me know your opinion in the below.

Did you find this article valuable?

Support Malavi Pande by becoming a sponsor. Any amount is appreciated!