Create a name input function

I would like to create a function to enter a name. However, if there is a period or space other than the name, an error will be displayed.

I would like to think about it using the include? method. The include? method is a method that determines whether the specified element is included in an array or character string. reference: Ruby 3.0.0 Reference Manual, include?

Example)

array = ["foo", "bar"]
puts array.include?("bar")
# => true
puts array.include?("hoge")
# => false

I want to create a method to check if the name can be registered properly, so the method name is check_name.

Output example is

Please enter your name(Example)SuzukiIchiro

SuzukiIchiro(The name you entered)
→ Registration is complete
Suzuki.Ichiro(The name you entered)
→!error!Symbols cannot be registered
Suzuki Ichiro(The name you entered)
→!error!Space cannot be registered

I will make it output like this.

First, we will use a guide for you to enter and a method for entering characters.

puts "Please enter your name(Example)SuzukiIchiro"
str = gets

By using the gets method and setting str = gets, the name entered in the terminal will be assigned to str.

Next, create a check_name method and write a description that calls it.

def check_name(str)

end
puts "Please enter your name(Example)SuzukiIchiro"
str_1 = gets
check_name(str_1)

The actual argument (str_1) of check_name (str_1) is set to the name entered in the gets method. When the check_name method is called, the value set in the simultaneous actual argument is passed to the formal argument (str).

Finally, we will describe the processing in the check_name method. We will describe by combining conditional branching and include? Method.

def check_name(str)
  if str.include?(".")
    puts "!error!Symbols cannot be registered"
  elsif str.include?(" ")
    puts "!error!Space cannot be registered"
  else
  puts "Registration has been completed"
  end
end

puts "Please enter your name(Example)SuzukiIchiro"
str_1 = gets
check_name(str_1)

Since the if statement ends processing when the condition is met, the conditional expression that indicates whether there is a period or space first is described first.

You have now created a conditional name entry function.

Recommended Posts

Create a name input function
[Rails withdrawal] Create a simple withdrawal function with rails
Create a login function using Swift's Optional
Create a tool for name identification in Salesforce
Create an EC site with Rails 5 ⑨ ~ Create a cart function ~
Create a login authentication screen using the session function
[Rails DM] Let's create a notification function when DM is sent!
[Java] Create a temporary file
Create a VS Code Plugin.
Create a playground with Xcode 12
Create a fortune using Ruby
About adding a like function
How to create a method
Let's create a TODO application in Java 4 Implementation of posting function
How to create a registration / update function where the table crosses
I can't create a Java class with a specific name in IntelliJ
[Rails 6] How to create a dynamic form input screen using cocoon
Let's create a TODO application in Java 6 Implementation of search function
[Implementation procedure] Create a user authentication function using sorcery in Rails
Let's create a TODO application in Java 8 Implementation of editing function
(Ruby on Rails6) Create a function to edit the posted content
Create a Vue3 environment with Docker!
Create a lightweight STNS Docker image
Preparing to create a Rails application
Create a tomcat project using Eclipse
[Rails Tutorial Chapter 5] Create a layout
Create your own Solr Function Query
Create pagination function with Rails Kaminari
Create a database in a production environment
Create a new app in Rails
[Android] Create validation for date input!
Create a Java project using Eclipse
[Java] How to create a folder
Confirm name input using include? method
I made a simple recommendation function.
Create a Servlet program in Eclipse
Try to create a server-client app
Create exceptions with a fluid interface
[Kotlin / Android] Create a custom view
Call a C function from Swift
Create a Maven project with a command
Create a fluentd server for testing
Create a simple CRUD with SpringBoot + JPA + Thymeleaf ② ~ Screen and function creation ~
How to create a validator that allows only input to any one field
Create a SPA with authentication function with Rails API mode + devise_token_auth + Vue.js 3 (Rails edition)