To prompt the user for their full name, you can use the gets
method to get the user input from the command line. Then, you can split the user input by space to separate the first name and last name. Finally, you can use string interpolation to greet the user with their full name.
Here's an example Ruby code that accomplishes this:
main.rb195 chars6 lines
In this code, we start by prompting the user to enter their full name using puts
. Then, we use gets.chomp
to obtain the user's input and remove any trailing newline characters.
Next, we split the full name into first name and last name using the split
method, passing in a space as the separator. The split
method returns an array with the individual names, and we assign each name to separate variables.
Finally, we use string interpolation (#{...}
) to greet the user with their full name, printing "Salam, " followed by the first_name
, a space, and the last_name
.
Remember to ensure you have the correct spacing between the inputs and outputs in your program!
gistlibby LogSnag