2

Can I conditionally skip requiring a file in Ruby?

begin
  require 'aws-sdk'
rescue LoadError
  puts "aws-sdk gem not found"
end

namespace :db do
  desc "import local postgres database to heroku. user and database name is hardcoded"
  task :import_to_heroku => [:environment, "db:dump_for_heroku"] do
    # code using aws-sdk gem
  end
end

In the above code, can I ask Ruby not to read after rescue LoadError
I can wrap the whole code in an conditional but that is inelegant. I tried next and return.

EDIT: added a new question at Can I conditionally skip loading "further" ruby code in the same file?. sorry. Did not ask this question properly

Community
  • 1
  • 1
deepak
  • 7,230
  • 5
  • 24
  • 26
  • Sure can. You can conditionally `require` something, or use a `rescue` to catch a failure to find it. – the Tin Man Jan 25 '13 at 13:55
  • i have rescued `LoadError` but i want that if `LoadError` is executed., further code should not be executed. In the example given, the rake task `db:import_to_heroku` should not be called – deepak Jan 25 '13 at 14:41
  • 1
    See "[Safely require gems in Ruby](http://stackoverflow.com/questions/7307785/safely-require-gems-in-ruby?rq=1)" – the Tin Man Jan 25 '13 at 15:09
  • Possible duplicate of [How to cancel evaluating a required Ruby file?](https://stackoverflow.com/questions/34347134/how-to-cancel-evaluating-a-required-ruby-file) – user513951 Feb 01 '19 at 13:39

3 Answers3

7

Maybe add an exit after the log:

begin
  require 'aws-sdk'
rescue LoadError
  puts "aws-sdk gem not found"
  exit
end

namespace :db do
  desc "import local postgres database to heroku. user and database name is hardcoded"
  task :import_to_heroku => [:environment, "db:dump_for_heroku"] do
    # code using aws-sdk gem
  end
end

Also the abort function is to log and exit in the same call:

abort("aws-sdk gem not found")
Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
1

i have rescued LoadError but i want that if LoadError is executed., further code should not be executed. In the example given, the rake task db:import_to_heroku should not be called

Then do:

begin
  require 'aws-sdk'

  namespace :db do
    desc "import local postgres database to heroku. user and database name is hardcoded"
    task :import_to_heroku => [:environment, "db:dump_for_heroku"] do
      # code using aws-sdk gem
    end
  end
rescue LoadError
  puts "aws-sdk gem not found"
end
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • nice :-) i wanted to, not wrap the whole code in an conditional. This seems like a conditional but better looking i guess. The only advantage of the previous code was that i know that 'aws-sdk' had a `LoadError`. But i can match the error message for that i guess. – deepak Jan 25 '13 at 14:59
  • was actually hoping for a ruby `syntax` what would skip loading the file. do not want to wrap or modify my main code in any way – deepak Jan 25 '13 at 15:00
  • actually wanted something like http://stackoverflow.com/questions/14526268/can-i-conditionally-skip-loading-further-ruby-code-in-the-same-file – deepak Jan 25 '13 at 16:42
  • accepting it. even though wanted something like http://stackoverflow.com/questions/14526268/can-i-conditionally-skip-loading-further-ruby-code-in-the-same-file. As i would use this code on production – deepak Jan 25 '13 at 16:43
  • So, you asked the question twice? Don't do that. Edit this one, phrase it correctly, and delete the other one. If a question's answer doesn't meet your needs it's OK to not accept an answer. But, most of all, take the time to think your question through, put in what is necessary for YOU to know to answer a question, such as code samples, errors received, essential data, etc. – the Tin Man Jan 25 '13 at 16:54
  • ok. are u sure? following all these comments and threads, would give my future self a headache. And i created another question as two people told me that the question was improperly worded. After editing it, it seemed like another question – deepak Jan 25 '13 at 16:58
  • Improperly worded just means edit it and be clearer about what you want. Following two threads that are related would be worse than one. You can always flag it and ask a moderator what to do. – the Tin Man Jan 25 '13 at 19:08
0

The "top-level return" feature has been added.

It is now possible to use the return keyword at the top level, which as you say, did not work at the time the question was asked. Further discussion here.

user513951
  • 12,445
  • 7
  • 65
  • 82