Project

General

Profile

Feature #14565

Simpler, one-liner, failsafe require in ruby? [Suggested names: require_failsafe, require_safe, require_try, require_add)

Added by shevegen (Robert A. Heiler) about 1 year ago. Updated about 1 year ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:85886]

Description

Hello.

I have quite a bit of code like this:

begin
  require 'x/tools/cdrskin.rb'
rescue LoadError; end

I also use the longer variant, e. g.

begin
  require 'foobar'
rescue LoadError
  puts 'project foobar is not available - consider '\
       'installing it via gem install foobar'
end

However had, often I really do not even want to or notneed
to inform the user about a missing gem/project, because
some of these gems are tiny and not very important. In
my larger ruby projects I handle the cases where a smaller
project is not available or available, so I can proceed
either way. Notifying the user, who is usually me, is
a bit pointless in these cases; that is why I would like
to have a one-liner.

I am thinking of API such as any of the following, belonging
to the require family (we already have a bit variety in the
require family such as require_relative())

require_failsafe
require_safe
require_try
require_add

This is really just the first variant, that is, loading with
a rescue LoadError, without notification. (If I need to notify
a user then I am fine with the longer variant.)

So mostly this is just a suggestion for me to be lazy/lazier
but to also get rid of a few lines in my code base.

The name is of course important but the functionality is, to
me, more important, so if anyone has better names, feel free
to add them! I just tried to be somewhat explicit which is
why I suggested the first variant. I think people are more
likely to remember the require-family, e. g. require 'foo.rb'
or require_relative 'bar.rb' and so forth. (I may be wrong
but I think there are many more cases of require than load
in ruby code bases out there in the wild.)

I am not sure if others may find this semi-useful or not.

In my ruby code, there are not so many instances where I use
it; perhaps 100 or so. But still, it would be nice if we could
have a one-liner.

Anyway, I think I mentioned all I wanted to mention here
about it. I also wanted to propose a stronger require/import
feature of ruby, including the possibility to refer to .rb
files without a hardcoded path (if the .rb file is moved,
all explicit requires to it, in particular from external
projects, would have to change; and my vague idea is to
replace this with some kind of project-specific way to
"label" files and load these files based on these "labels"
but that is for another suggestion; I only want to mention
it because Hiroshi Shibata made some suggestion as
extension to require and I think the use case he mentioned
may also be useful to see whether ruby may get a stronger
"load code in files" functionality for ruby 3.x eventually)

Please feel free to close this issue at any time if it is
not a good fit. Thanks!

History

Updated by shevegen (Robert A. Heiler) about 1 year ago

(I have a few typos above; is there a way to edit the first post in the
bug tracker? I only seem to be able to edit in subsequent posts... hmm)

Updated by zverok (Victor Shepelev) about 1 year ago

shevegen (Robert A. Heiler) If you press "Edit", the form has Description [Edit] field, and you can edit description there.

Also available in: Atom PDF