{"id":7016,"date":"2014-05-17T00:20:49","date_gmt":"2014-05-17T00:20:49","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/can-i-conditionally-skip-loading-further-ruby-code-in-the-same-file-collection-of-common-programming-errors\/"},"modified":"2014-05-17T00:20:49","modified_gmt":"2014-05-17T00:20:49","slug":"can-i-conditionally-skip-loading-further-ruby-code-in-the-same-file-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/can-i-conditionally-skip-loading-further-ruby-code-in-the-same-file-collection-of-common-programming-errors\/","title":{"rendered":"Can I conditionally skip loading &ldquo;further&rdquo; ruby code in the same file?-Collection of common programming errors"},"content":{"rendered":"<p>Can I conditionally skip loading &#8220;further&#8221; ruby code in the same file,<br \/>\nif a library (loaded via require) is not found ?<\/p>\n<pre><code>begin\n  require 'aws-sdk'\nrescue LoadError\n  puts \"aws-sdk gem not found\"\n  return #does not work. nor does next\nend\n\n# code after here should not be executed as `aws-sdk` gem was not found\nputs \"=== should not get executed\"\n\nnamespace :db do\n  desc \"import local postgres database to heroku. user and database name is hardcoded\"\n  task :import_to_heroku =&gt; [:environment, \"db:dump_for_heroku\"] do\n    # code using aws-sdk gem\n  end\nend\n<\/code><\/pre>\n<p>In the above code, can I ask Ruby not to load the file further after hitting a <code>rescue LoadError<\/code>.<\/p>\n<p><strong>Like an early return but for loading a file and not for a function.<\/strong><\/p>\n<p>Need it because i have i have a rake task which needs <code>aws-sdk<\/code> rubygem but i use it only on my local machine. If <code>aws-sdk<\/code> is not found it does not make sense for me to load code afterwards in the same file. I guess i can split the code into smaller files and warp it in a require call<\/p>\n<pre><code>if Rails.env.development?\n  require 'import_to_heroku'\nend\n<\/code><\/pre>\n<p>But do not want to warp or modify my existing code<\/p>\n<p>Also, i can wrap the whole code in an conditional but that is inelegant. A begin-rescue block is also a form of explicit control flow. I do not want to wrap or touch the original code is any manner<\/p>\n<p>Maybe an api such as<\/p>\n<pre><code>require_or_skip_further_loading 'aws-ruby`\n<\/code><\/pre>\n<p>So i want my code to be functionally equivalent to<\/p>\n<pre><code>begin\n  require 'aws-sdk'\n\n  namespace :db do\n    desc \"import local postgres database to heroku. user and database name is hardcoded\"\n    task :import_to_heroku =&gt; [:environment, \"db:dump_for_heroku\"] do\n      # code using aws-sdk gem\n    end\n  end\nrescue LoadError\n  puts \"aws-sdk gem not found\"\nend\n<\/code><\/pre>\n<p>Or via an if conditional<\/p>\n<pre><code>library_found = false\nbegin\n  require 'aws-sdk'\n  library_found = true\nrescue LoadError\n  puts \"aws-sdk gem not found\"\n  return #does not work\nend\n\nif library_found      \n  namespace :db do\n    desc \"import local postgres database to heroku. user and database name is hardcoded\"\n    task :import_to_heroku =&gt; [:environment, \"db:dump_for_heroku\"] do\n    # code using aws-sdk gem\n    end\n  end\nend\n<\/code><\/pre>\n<p><strong>Want program execution to continue after <code>LoadError<\/code> is raised. ie. gracefully handle <code>LoadError<\/code> and do not load code written after <code>LoadError<\/code> in the same file. cannot raise <code>exit<\/code> or <code>abort<\/code> on <code>LoadError<\/code> And particularly the code after <code>LoadError<\/code> should not be executed (or loaded) by the ruby interpreter<\/strong><\/p>\n<p>Had originally asked How to skip require in ruby? but i did not ask the question properly. hope this is better worded<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Can I conditionally skip loading &#8220;further&#8221; ruby code in the same file, if a library (loaded via require) is not found ? begin require &#8216;aws-sdk&#8217; rescue LoadError puts &#8220;aws-sdk gem not found&#8221; return #does not work. nor does next end # code after here should not be executed as `aws-sdk` gem was not found puts [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7016","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7016","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7016"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7016\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}