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 302229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:04:41+00:00 2026-05-12T07:04:41+00:00

I have a directory full of ruby scripts for common tasks, and I’ve started

  • 0

I have a directory full of ruby scripts for common tasks, and I’ve started to accumulate a set of common variables and methods that I find myself defining in each new script. The next step in improving this would seem to be creating a file (say, commonstuff.rb) and “require”ing that file from the other scripts so that the common variables and methods are all available everywhere and defined only once.

A simple attempt that didn’t work:

commonstuff.rb

username=ENV['USER']
home_dir_path=ENV['HOME']

def print_and_execute(command, &block)
  puts command

  process_io = IO.popen(command + "2>&1")

  while(line=process_io.gets)
    if (block != nil)
      yield line
    else
      puts line
      STDOUT.flush
    end
  end
end

script1.rb

#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/common_stuff'

puts home_dir_path         # Fail
print_and_execute "date"   # Fail

I’ve used ruby libraries and frameworks, but I don’t have any of that available in my current environment. I just have straight ruby, and I’m a little rusty on some of the basic idioms that would work well here, or that would look right to a ruby expert.

Any help appreciated!

  • 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-05-12T07:04:42+00:00Added an answer on May 12, 2026 at 7:04 am

    Wrap your methods and variables in a module, e.g.

    module CommonStuff
        USERNAME=ENV['USER']
        HOME_DIR_PATH=ENV['HOME']
    
        def print_and_execute(command, &block)
            ...
        end
    end
    

    Then script1.rb might be like:

    #!/usr/bin/env ruby
    require File.dirname(__FILE__) + '/commonstuff.rb'
    include CommonStuff
    
    puts HOME_DIR_PATH         # Win
    print_and_execute "date"   # Win
    

    Or, if you don’t want to include the module in your namespace:

    #!/usr/bin/env ruby
    require File.dirname(__FILE__) + '/commonstuff.rb'
    
    puts CommonStuff::HOME_DIR_PATH         # Win
    CommonStuff.print_and_execute "date"   # Win
    

    See also Modules and the Programming Ruby page on modules.

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

Sidebar

Ask A Question

Stats

  • Questions 181k
  • Answers 181k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer $('a').text() would return the combination of every anchor nodes text… May 12, 2026 at 4:18 pm
  • Editorial Team
    Editorial Team added an answer java.security.ProtectionDomain is the Java equivalent of what AppDomains appear to… May 12, 2026 at 4:18 pm
  • Editorial Team
    Editorial Team added an answer There is something wrong in your function x. If you… May 12, 2026 at 4:18 pm

Related Questions

I have a directory full of ruby scripts for common tasks, and I've started
I have a directory full of files and I need to pull the headers
I have a directory full of legacy code from a VB6 application. I have
I have a directory full of files, some which have an ampersand in their
I have a directory full of images that I would like to resize to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.