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.
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}!!"
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.
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.
$ 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!!
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