Try to implement Yubaba in Ruby

Introduction

Recently, it seems that Implementing Yubaba is popular in various languages, but it seems that it is not yet in Ruby, so I will ride it.

code

puts "It's a contract. Write your name there."

name = gets.chomp

puts "Hung.#{name}I mean. It's a luxurious name."

new_name_index = rand(name.size)
new_name = name[new_name_index]

puts "From now on your name is#{new_name}It is. Mind you,#{new_name}That's right. I'll reply when I understand#{new_name}!!"

Commentary

contract

First, give the contract.

puts "It's a contract. Write your name there."

name = gets.chomp

Read standard input with gets. This also includes the last newline, so use chomp to remove the newline.

Steal the name

new_name_index = rand(name.size)
new_name = name[new_name_index]

Generate a random number of 0 <= n <name.size with rand and extract one character.

Execution result

$ ruby yubaba.rb
It's a contract. Write your name there.
Yamada Taro
Hung. Is it Taro Yamada? It's a luxurious name.
From now on your name is Ta. It ’s a rice field. I'll reply when I understand, Ta!!

If you don't enter anything

Unlike the original family, no error occurs even if you press Enter without entering anything.

$ ruby yubaba.rb
It's a contract. Write your name there.

Hung. I mean. It's a luxurious name.
From now on your name is. Is that okay? I'll reply when I understand!!

However, pressing Ctrl + D instead of Enter causes an error.

$ ruby yubaba.rb
It's a contract. Write your name there.
Traceback (most recent call last):
yubaba.rb:3:in `<main>': undefined method `chomp' for nil:NilClass (NoMethodError)

Recommended Posts

Try to implement Yubaba in Ruby
Try to implement Yubaba in Java
Try to implement n-ary addition in Java
How to implement Pagination in GraphQL (for ruby)
Try implementing Yubaba in Kinx
Try using gRPC in Ruby
How to iterate infinitely in Ruby
Try to implement iOS14 Widget function
How to install Bootstrap in Ruby
Implement a gRPC client in Ruby
[Java] Try to implement using generics
Try file locking in Ruby directory
How to implement search functionality in Rails
How to implement date calculation in Java
How to implement Kalman filter in Java
How to get date data in Ruby
Implement the algorithm in Ruby: Day 1 -Euclidean algorithm-
Convert numbers to Roman numerals in Ruby
Try to implement login function with Spring-Boot
How to implement coding conventions in Java
Try to implement UIAlertController by separating files
How to implement ranking functionality in Rails
How to implement asynchronous processing in Outsystems
Introduction to Ruby 2
Class in Ruby
How to implement gem "summer note" in wysiwyg editor in Ruby on Rails
Try Ruby Minitest
Heavy in Ruby! ??
[Ruby] Basic key to be strong in refactoring
I tried to implement polymorphic related in Nogizaka.
I want to use arrow notation in Ruby
Try to create a bulletin board in Java
Try to link Ruby and Java with Dapr
Implement the algorithm in Ruby: Day 3 -Binary search-
I tried to implement deep learning in Java
Try to implement login function with Spring Boot
How to implement a like feature in Rails
Try to get redmine API key with ruby
Implement the algorithm in Ruby: Day 4-Linear search-
Try to implement TCP / IP + NIO with JAVA
How to implement optimistic locking in REST API
[Ruby on Rails] How to install Bootstrap in Rails
How to build the simplest blockchain in Ruby
Implement the algorithm in Ruby: Day 2 -Bubble sort-
I want to get the value in Ruby
I get an error when I try to use "^" or "$" in ruby ​​regular expression
How to implement image posting function using Active Storage in Ruby on Rails
How to implement UICollectionView in Swift with code only
[Introduction] Try to create a Ruby on Rails application
[Ruby] How to use standard output in conditional branching
[Introduction to Computer Science No. 0: Try Machine Learning] Let's implement k-means clustering in Java
How to implement guest login in 5 minutes in rails portfolio
Try to release gem
How to implement a like feature in Ajax in Rails
[Ruby on Rails] How to write enum in Japanese
I tried to implement Firebase push notification in Java
About eval in Ruby
Introduce devise in Rails to implement user management functionality
Try LetCode in Ruby-TwoSum
Converting TSV files to CSV files (with BOM) in Ruby
To debug in eclipse