Putting it all together
Reading and understanding someone else’s code is essential to learning Ruby. With that intent, we will now look at the following example, which was written with some of the techniques we learned about in this chapter, and figure out what the script is doing:
# main.rb # Section 1: Ruby version validation if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.6') puts "Please verify the Ruby version!" Kernel::exit(1) end # Section 2: Open or create user_name file file_instance = File.open("user_name.txt", "a+") user_name = file_instance.read # Section 3: Empty name validation if user_name.empty? puts "Enter your name:" name = gets.chomp File.write("user_name.txt", name) # Section 4: Program main log File.write("main.log", "Writing #{name} as the entry to user_name.txt at #{Time.now}\n"...