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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:13:38+00:00 2026-05-23T10:13:38+00:00

I want to write a Ruby script something like this: class Foo # instance

  • 0

I want to write a Ruby script something like this:

class Foo
  # instance methods here

  def self.run
    foo = Foo.new
    # do stuff here
  end
end

# This code should only be executed when run as a script, but not when required into another file
unless required_in?  # <-- not a real Kernel method
  Foo.run
end
# ------------------------------------------------------------------------------------------

I want to be able to unit test it, which is why I don’t want the code outside of the class to run unless I execute the script directly, i.e. ruby foo_it_up.rb.

I know I can simply put the Foo class in another file and require 'foo' in my script. In fact, that is probably a better way to do it, just in case Foo‘s functionality is needed somewhere else. So my question is more academic than anything, but I’d still be interested in knowing how to do this in Ruby.

  • 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-23T10:13:38+00:00Added an answer on May 23, 2026 at 10:13 am

    This is usually done with

    if __FILE__ == $0
      Foo.run
    end
    

    but I prefer

    if File.identical?(__FILE__, $0)
      Foo.run
    end
    

    because programs like ruby-prof can make $0 not equal __FILE__ even when you use --replace-progname.

    $0 refers to the name of the program ($PROGRAM_NAME), while __FILE__ is the name of the current source file.

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

Sidebar

Related Questions

I want to write a script in Ruby to clean up some messed up
I was working with the lotrepls Ruby interpreter, and I want like to write
I want to write code in Ruby witch net::ssh that run commands one by
I wrote a Ruby script that I want to run in the background repeatedly
I want to write a simple chat-client in Ruby for the terminal. The Problem
I want to write a web application and want to use Ruby. I have
I am running a bit of code that looks like this: result = system(ruby
Can anyone explain why this happens? mybox:$ ruby script/console Loading development environment (Rails 2.3.5)
I want to write a client for megaupload.com in Ruby and I wanted to
I want write a ruby wrapper for a existing C library (.so files). Can

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.