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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:07:38+00:00 2026-05-16T00:07:38+00:00

Normally to use Ruby libraries from command line I can do something like: ruby

  • 0

Normally to use Ruby libraries from command line I can do something like:

ruby -rfastercsv -e 'code'

Unfortunately this doesn’t work with rubygems, as they’re not enabled by default, and whatever rubygems does to override require doesn’t seem to work with -r switch, so I’m forced to do this instead:

ruby -e 'require "rubygems"; require "fastercsv"; code'

Quite annoying for a one-liner – 42 characters of overhead compared with just 13 for non-rubygems libraries. Is there any way to avoid that?

I wrote this script to work around the problem (it works as multiple -e "code" are allowed, and require is idempotent so it shouldn’t interfere with -p/-n or anything else as far as I can tell), but it’s all rather ugly, and I wouldn’t mind a more elegant solution:

args = []
until ARGV.empty?
  arg = ARGV.shift
  if arg =~ /\A-r(.*)\Z/
    args << "-e" << "require 'rubygems'; require '#{$1.empty? ? ARGV.shift : $1}'"
  else
    args << arg
  end
end
exec "ruby", *args
  • 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-16T00:07:39+00:00Added an answer on May 16, 2026 at 12:07 am

    My first attempt (above) only works with -e. It wouldn’t work with scripts in files like ruby -rfoo bar.rb. This scripts supports both by self-requiring trick (its file name must end in .rb):

    #!/usr/bin/env ruby
    
    if $0 == __FILE__
      args = []
      libs = []
      last_lib = nil
      while arg = ARGV.shift
        if arg == "--"
          args << arg
          args += ARGV
          break
        elsif arg =~ /\A-r(.*)\Z/
          libs << ($1.empty? ? ARGV.shift : $1)
          last_lib = args.size
        else
          args << arg
        end
      end
      if last_lib
        libself = __FILE__.chomp(".rb")
        ENV["RUBYGEMS_LIBS"] = libs.join(":")
        args[last_lib, 0] = ["-r", libself]
      end
      exec "ruby", *args
    else
      require "rubygems"
      libs = ENV["RUBYGEMS_LIBS"]||""
      libs.split(":").each{|lib|
        require lib
      }
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code that where I would normally use one line: if (tableView
I normally use the following code in SQL Server: IF EXISTS (SELECT * FROM
So i normally use this syntax to create as symbolic link from powershell. PS
I normally use ; to combine more than one command in a line, but
I'm looking for a ruby script that will use something like last to count
I have created this Ruby (1.9.2) code and the last line fails: File.open(test äöü.txt,
TL;DR - how can I use something like improved_markdown :some_file to do custom rendering,
Normally to realized linked objects, i usually use getter and setter methods and this
Normally in OpenRasta there is some configuration like this: ResourceSpace.Has.ResourcesOfType<Customers>() .AtUri(/customers/region/{region}) ... // and
I normally use this step to set up records with factory_girl: Given /^the following

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.