0

I found some Ruby code that made use of:

require "active_support/core_ext/time"
require "active_support/core_ext/numeric"

On my Mac with macOS Monterey, I don't have rails or rvm yet, and if I install rvm, it requires Homebrew to install a new Ruby and it may complicate things further, so with the system Ruby (ruby 2.6.8p205), how can active_support/core_ext/time be installed so that it can be used?

I looked at this question and it doesn't solve the problem.

I recalled I can install rails:

gem install rails

However, is it true that I have to use sudo to do it? (I try to minimize the use of sudo to keep the system more secure).

Stefanie Gauss
  • 425
  • 3
  • 9
  • You can install `active_support`, or any other gem for that matter, without `sudo` too. See here: https://apple.stackexchange.com/a/421449 – Casper May 17 '22 at 18:30
  • And here: https://stackoverflow.com/a/45347703/823617 – Casper May 17 '22 at 18:33
  • If you are willing to modify the source code of this program that you are using, it is possible to convert it to use Bundler. In that case there is no need to install any system gems at all. You would just use Bundler, and it would take care of adding the required dependencies locally into the app itself. (N.B. assuming Bundler exists in your current Ruby installation). – Casper May 18 '22 at 01:28

3 Answers3

1

You can do the same thing using

sudo gem install active_support

You have to use sudo because you are writing to a default system library.

I would highly recommend using rbenv or rvm so you aren't meddling with the system Ruby constantly. It is slightly more setup, but both Homebrew and RVM are well known and supported libraries.

Romuloux
  • 1,027
  • 1
  • 8
  • 24
  • so I just have to bite the bullet and install Homebrew as well I think? thanks so much – Stefanie Gauss May 17 '22 at 16:50
  • Yes, it really isn't too bad and plays pretty nice with the default system so you shouldn't have too much trouble. – Romuloux May 17 '22 at 16:54
  • I have used rvm before but not rbenv... I just read the README of rbenv and it looks like that's why a lot of software engineers work overtime – Stefanie Gauss May 17 '22 at 16:55
  • 3
    **DO NOT, EVER, UNDER ANY CIRCUMSTANCE, MODIFY APPLE'S RUBY INSTALLATION. IF YOU EVER NEED TO USE SUDO TO INSTALL A GEM, YOU ARE DOING IT WRONG. IF YOU EVER FIND SOMEONE TELLING YOU TO DO USE SUDO TO INSTALL A GEM, IGNORE THEM.** – Jörg W Mittag May 17 '22 at 18:56
  • There are two big problems in this answer: 1. `sudo` (never use it to install gems). 2. `active_support` (there is no such gem). Therefore the only correct part of `sudo gem install active_support` command is `gem install` – mechnicov May 17 '22 at 22:29
  • @JörgWMittag I trust you, but can you add a statement as to why not modify Apple's Ruby installation? For example, if we update XCode or Final Cut or GarageBand, it is all ok, so why not Ruby? – Stefanie Gauss May 17 '22 at 22:55
  • The correct name of the gem is `activesupport`. – Casper May 18 '22 at 00:18
1

Using sudo to install gem is completely bad idea

It's better to use some ruby manager in your system

For example:

You can also use docker to isolate project

When you get some Ruby using one of this variant, you can install ActiveSupport gem with Gemfile or just with terminal command

gem install activesupport

Then you can choose file(s) that will be required here:

https://github.com/rails/rails/tree/main/activesupport/lib

For example you need all Time monkeypatches

It is here:

https://github.com/rails/rails/blob/main/activesupport/lib/active_support/core_ext/time.rb

So you need to use:

require "active_support/core_ext/time"
mechnicov
  • 12,025
  • 4
  • 33
  • 56
0

To add to @mechnicov's answer, yes, first install something like RVM, and then

gem install activesupport

and you can use

require "active_support/core_ext/time"

as before. (which supposedly loads everything in https://github.com/rails/rails/blob/main/activesupport/lib/active_support/core_ext/time.rb )

If you are using a recent version of Ruby, depending on what you need in numeric, instead of:

require "active_support/core_ext/numeric"

which supposedly loads https://github.com/rails/rails/blob/main/activesupport/lib/active_support/core_ext/numeric.rb

you can use:

require "active_support/core_ext/numeric/time.rb"

as in the Rails docs.

You can also include everything by:

require "active_support/all"

as in this Rails docs. See https://github.com/rails/rails/blob/main/activesupport/lib/active_support/all.rb for details.

nonopolarity
  • 146,324
  • 131
  • 460
  • 740