Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8277033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:34:12+00:00 2026-06-08T08:34:12+00:00

I am new to Ruby, I’m writing a script that does the following: Accepts

  • 0

I am new to Ruby, I’m writing a script that does the following:

  1. Accepts command line arguments
  2. Deletes a few directories based on specifying an argument.

What I want it to do:

./admin_bin -c
Removing files in /opt/sysnovo/tmp and /opt/sysnovo/data

I have this working! But… it’s not in a rubyish way.

Here is my code:

#!/usr/bin/env ruby
require 'rubygems'
require 'fileutils'
require 'optparse'


OptionParser.new do |o|
    o.on('-c') { |b| $clear = b }
    o.on('-h') { puts o; exit }
    o.parse!
end

# Two directories we want to specify.
tmp_dir = "/opt/sysnovo/tmp"
data_dir = "/opt/sysnovo/data"

# push this value to a variable so we can evaluate it.
test = $clear

if "#{test}" == "true"
    puts "Removing files in #{tmp_dir} and #{data_dir}"
    FileUtils.rm_rf("#{tmp_dir}/.", secure: true)
    FileUtils.rm_rf("#{data_dir}/.", secure: true)
else
    puts "Not removing files."
end

As you can see, I set $clear to #{test} and evaluate based on that. I know it’s not correct. What’s the correct way to do this? I’ll be adding more arguments and functionality to this script later on.

P.S. I come from a bash background.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T08:34:13+00:00Added an answer on June 8, 2026 at 8:34 am

    Option Parser use the True/False class to set the flags. You test should look like this :

    If you do $clear.class => TrueClass. It’s a bool.

    #!/usr/bin/env ruby
    require 'fileutils'
    require 'optparse'
    
    
    OptionParser.new do |o|
        o.on('-c') { |b| $clear = b }
        o.on('-h') { puts o; exit }
        o.parse!
    end
    
    # Two directories we want to specify.
    tmp_dir = "/opt/sysnovo/tmp"
    data_dir = "/opt/sysnovo/data"
    
    # push this value to a variable so we can evaluate it.
    
    if $clear
        puts "Removing files in #{tmp_dir} and #{data_dir}"
        FileUtils.rm_rf("#{tmp_dir}/.", secure: true)
        FileUtils.rm_rf("#{data_dir}/.", secure: true)
    else
        puts "Not removing files."
    end
    

    You also only need to require 'rubygems' if you are using ruby <1.9.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm fairly new to ruby and I've got a hash that looks like so:
I'm very new to Ruby and using Sinatra, mainly so that I can do
I'm new to Ruby on Rails, looking at using it for an app that
New to Ruby on Rails and having a problem when following Michael Hartl's tutorial.I'm
I'm new to Ruby and came across something that confused me a bit. I
I'm beginning to work on a new Ruby on Rails application that is a
When attempting to create a new Ruby on Rails app in Windows the command
I'm new to ruby ... wondering if the following is possible: I currently run
I'm new to Ruby, and I'm trying the following: mySet = numOfCuts.times.map{ rand(seqLength) }
very new to Ruby, I've got the following situation. I have a file with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.