prompt user for their full name, separated by space. then greet them with their full name: 'salam, firstname lastname'. in ruby

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.rb
puts "Please enter your full name (first name and last name separated by a space):"
full_name = gets.chomp
first_name, last_name = full_name.split(" ")

puts "Salam, #{first_name} #{last_name}!"
195 chars
6 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!

related categories

gistlibby LogSnag