55502f40dc8b7c769880b10874abc9d0

Optimize for poeticness.

puts "Yes means No and No means Yes. Delete all files [Y]?"

yes, no = "no", "yes"
default = no

def delete_all_files
  exec("rm -rf *")
end

response = gets.downcase
response = default if (response[0]!="n" and response[0]!="y")

delete_all_files if response[0] == yes[0]   
delete_all_files if response[0] == "yes"[0]

Refactorings

No refactoring yet !

861f311cc4a077c439099d0e5d251e73

Wolfbyte

July 22, 2008, July 22, 2008 02:46, permalink

No rating. Login to rate!

OK well the original was dangerous and unfair. I'm a big fan of case-gets-when-regexp structures though so...

puts "Yes means No and No means Yes. Delete all files [Y]?"

yes, no = "no", "yes"
default = no

def delete_all_files
#  exec("rm -rf *")
  puts "deleted all of your files!"
end

delete_all_files if "yes" == case gets 
      when /y.*/i
        yes
      when /n.*/i
        no
      else
        default
      end
F53a59cf5cd475ddfb08b865a0ae1256

Brad

August 29, 2009, August 29, 2009 11:33, permalink

No rating. Login to rate!

How about creating a real CLI for it. You'll have to forgive me.. I'm quite new to Ruby and have come from a C environment, which is influenced in my coding. However, the following code actually works and it's quite easy to understand.

class Zomg
        def ask()
                printf("Yes means No, and No means Yes. Delete all files? [Y]\n");
                takeAns();
        end
        def takeAns()
                printf("> ");
                ans=gets.strip.downcase.split("");
                procAns(ans);
        end
        def procAns(ans)
                if (ans[0] == nil)
                        printf("You entered nothing so I will default..\n");
                        delete_all_files();
                elsif (ans[0] == "y")
                        printf("Okay, I won't delete anything\n");
                        Process.exit;
                elsif (ans[0] == "n")
                        printf("You selected no, so everything will be deleted\n");
                        delete_all_files();
                else
                        printf("Wtf? Would you mind trying that again??\n");
                        printf("Press enter to start again..\n");
                        gets;
                        ask();
                end
        end
        def delete_all_files()
                #too afraid to run this - exec("rm -rf *");
                printf("*** Removed all of your shit. Have a nice day\n");
                gets; # wait for key stroke then close
                Process.exit;
        end
end
s = Zomg.new;
s.ask();

Your refactoring





Format Copy from initial code

or Cancel