Feature #14565
Updated by sawa (Tsuyoshi Sawada) about 5 years ago
Hello. I have quite a bit of code like this: ```ruby begin require 'x/tools/cdrskin.rb' rescue LoadError; end ``` I also use the longer variant, e.g., ```ruby e. g. begin require 'foobar' rescue LoadError puts 'project foobar is not available - consider '\ 'installing it via gem install foobar' end ``` Often, However had, often I really do not need even want to or notneed to inform the user about a missing gems/projects that gem/project, because some of these gems are tiny and not very important. In my larger ruby projects, projects I handle the cases where a smaller project is not available or available, so I can proceed either way. It Notifying the user, who is usually me, is a bit pointless to notify the user when in these cases; that is me; that is why I would like to have a one-liner. I am thinking of an API such as any of the following: 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 for really just the first variant, that is, loading with a rescue LoadError LoadError, without notification. If (If I need to notify a user then I am fine with the longer variant. variant.) If 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`, require/import feature of ruby, including the possibility to refer to `.rb` .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", "labels" but that is for another suggestion; I only want to mention it because Hiroshi Shibata made some suggestion as extension to require, 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). eventually) Please feel free to close this issue at any time if it is not a good fit. Thanks!