Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It's not exactly clear when you want your code to run. Do you want it to run when Ruby starts the program, or when Bundler builds the list of gems for an app?</p> <p>If you're talking about when Ruby loads the code to run it...</p> <p>Ruby supports <code>BEGIN {...}</code> and <code>END {...}</code> blocks that get run before variables are initialized. They're run as they're seen, but, obviously, <code>END {...}</code> would run at the end of the program, just prior to the interpreter exiting.</p> <p>See <a href="http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UA" rel="nofollow">"Programming Ruby"</a>:</p> <blockquote> <p>BEGIN and END Blocks</p> <p>Every Ruby source file can declare blocks of code to be run as the file is being loaded (the BEGIN blocks) and after the program has finished executing (the END blocks).</p> </blockquote> <pre><code>BEGIN { begin code } END { end code } </code></pre> <blockquote> <p>A program may include multiple BEGIN and END blocks. BEGIN blocks are executed in the order they are encountered. END blocks are executed in reverse order. </p> </blockquote> <p>You can also add code to a gem, class, or module file, that will get run as the file is loaded, to initialize variables/constants that get dynamically defined. There's no special requirement for that, just don't put it in a <code>class</code> or <code>def</code>. Code at the main level will run as expected, including if it's in a module -- it's that "main level" part that is important.</p> <p>Do any of these carefully because you don't want to impact the load or exit time of the gem, adversely affecting the program.</p> <p>Also, be very careful about monkey-patching existing classes in Ruby. If you change the functionality of an existing method, you can badly break a user's Ruby app. If you add a method name that conflicts with one the user has defined then the same situation can occur too, and, in either case, that can be difficult to track down and fix.</p> <hr> <p>To elaborate:</p> <p>When Ruby requires a class or module file, it loads it, then runs the code it finds in there. Any classes or constants get initialized and any code at the main level runs. At the bottom of the file Ruby will move on to the next "require" statement.</p> <p>Consider a file containing:</p> <pre><code>class Foo def initialize puts "Inside Foo.new()" end end </code></pre> <p>Running that will return nothing, nor will anything be executed. The class will be parsed, but since nothing invokes <code>Foo.new</code> we don't even see the output. That's why we don't embed code we want to run automatically inside the class, because it won't be run unless something explicitly calls it. Requiring it will have the same result.</p> <p>Changing the code to the following and running it outputs "Inside Foo.new()":</p> <pre><code>class Foo def initialize puts "Inside Foo.new()" end end Foo.new() # &gt;&gt; Inside Foo.new() </code></pre> <p>That's explicitly calling the class initializer.</p> <p>If the first code was required by another file, when it's run nothing will happen because the class isn't invoked until something in the requiring code or a subsequently loaded file tells Ruby to run it. Having the class initializer run results in:</p> <pre><code>Inside Foo.new() </code></pre> <p>In any case, the method or class definition wouldn't be run until it's invoked by <code>foo</code> or <code>Foo.new()</code>, which then causes the new instance to be defined, allowing the initializer to run.</p> <p>If the OP wants to run something in his own gem, putting it inside a <code>def</code> or <code>class</code> will hide it. It's got to be at the main level for Ruby to run it. And, again, this assumes the question is about code running when Ruby loads the script, and NOT about code Rubygems or Bundler can run at installation time.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload