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

  • SEARCH
  • Home
  • 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 142857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:00:12+00:00 2026-05-11T08:00:12+00:00

I have a number of Ruby files, each of which declares a Class ,

  • 0

I have a number of Ruby files, each of which declares a Class, but each of which could conceivably be run from the command line.

I’d like to put the following functionality at the bottom of each file with the least duplication possible:

if __FILE__ == $0   # instantiate the class and pass ARGV to instance.run end 

My first instinct was to do this:

# /lib/scriptize.rb: Kernel.class_eval do   def scriptize(&block)     block.call(ARGV) if __FILE__ == $0   end end  # /lib/some_other_file.rb: include 'scriptize' class Foo   # ... end scriptize { |args| Foo.new.run(args) } 

But that doesn’t work because __FILE__ is evaluated in scriptize.rb, so it’s never Foo.

I imagine the solution is to literally inline the contents of scriptize.rb, but I don’t know the syntax. I could use eval, but that’s still quite a bit of duplication — it can’t really be reduced to a method I add to Kernel.

  • 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. 2026-05-11T08:00:12+00:00Added an answer on May 11, 2026 at 8:00 am

    Use caller to determine how close you are to the top of the call stack:

    ---------------------------------------------------------- Kernel#caller      caller(start=1)    => array ------------------------------------------------------------------------      Returns the current execution stack---an array containing strings      in the form ``_file:line_'' or ``_file:line: in `method'_''. The      optional _start_ parameter determines the number of initial stack      entries to omit from the result.          def a(skip)           caller(skip)         end         def b(skip)           a(skip)         end         def c(skip)           b(skip)         end         c(0)   #=> ['prog:2:in `a'', 'prog:5:in `b'', 'prog:8:in `c'', 'prog:10']         c(1)   #=> ['prog:5:in `b'', 'prog:8:in `c'', 'prog:11']         c(2)   #=> ['prog:8:in `c'', 'prog:12']         c(3)   #=> ['prog:13'] 

    This gives this definition for scriptize

    # scriptize.rb def scriptize     yield ARGV if caller.size == 1 end 

    Now, as an example, we can use two libraries/executables that require each other

    # libexA.rb require 'scriptize' require 'libexB'  puts 'in A, caller = #{caller.inspect}' if __FILE__ == $0     puts 'A is the main script file' end  scriptize { |args| puts 'A was called with #{args.inspect}' }  # libexB.rb require 'scriptize' require 'libexA'  puts 'in B, caller = #{caller.inspect}' if __FILE__ == $0     puts 'B is the main script file' end  scriptize { |args| puts 'B was called with #{args.inspect}' } 

    So when we run from the command line:

    % ruby libexA.rb 1 2 3 4 in A, caller = ['./libexB.rb:2:in `require'', './libexB.rb:2', 'libexA.rb:2:in `require'', 'libexA.rb:2'] in B, caller = ['libexA.rb:2:in `require'', 'libexA.rb:2'] in A, caller = [] A is the main script file A was called with ['1', '2', '3', '4'] % ruby libexB.rb 4 3 2 1 in B, caller = ['./libexA.rb:2:in `require'', './libexA.rb:2', 'libexB.rb:2:in `require'', 'libexB.rb:2'] in A, caller = ['libexB.rb:2:in `require'', 'libexB.rb:2'] in B, caller = [] B is the main script file B was called with ['4', '3', '2', '1'] 

    So this shows the equivalence of using scriptize and if $0 == __FILE__

    However, consider that:

    1. if $0 == __FILE__ ... end is a standard ruby idiom, easily recognized by others reading your code
    2. require 'scriptize'; scriptize { |args| ... } is more typing for the same effect.

    In order for this to really be worth it, you’d need to have more commonality in the body of scriptize – initializing some files, parsing arguments, etc. Once it gets complex enough, you might be better off with factoring out the changes in a different way – maybe passing scriptize your class, so it can instantiate them and do the main script body, or have a main script that dynamically requires one of your classes depending on what the name is.

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

Sidebar

Related Questions

I have a number of scripts (Ruby as it happens) I run from VIM,
I have number of line breaks in a string. But I only want it
I have a number of CSV files which contain start and finish times. I
Background: I have a module which declares a number of instance methods module UsefulThings
Basically I have these files (medline from NCBI). Each is associated with a journal
I have rails app which uses Rails 3.2.6 & Ruby 1.9.3p0 version.I have number
Possible Duplicate: True random number generator I have worked with random functions in python,ruby,
I have a number of classes and they are quite close to each other
I have a number of classes (more than 40), each one has a number
I'm messing around in Ruby some more. I have a file containing a class

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.