Good process with Ruby redo

Question: Question:

For the first time, I learned that redo can be used in repetitive statements, and I knew how it worked.
As a concrete process, it seems that the value of a specific variable is changed every loop, but
I could only think of processes that were inefficient or difficult to read.

Could you give me an example of when you can use redo to write the process neatly?

Answer: Answer:

# -*- encoding: utf-8 -*-

items = ["名前", "ニックネーム", "出身地", "座右の銘"]

puts "あなたの情報を教えてください"

p items.inject(Hash.new) {|h, i|
    print "#{i}は?: "
    redo if (h[i] = gets.chomp).empty?
    h
}
Scroll to Top