title: How "vars" in Jenkins Shared Libraries work?
body: <p>I'm experiencing some behaviors in Jenkins Shared Libraries, and it'll be great if someone can explain this to me: <br></p>
  <h2>First issue</h2>
  <p>Let's say i have a file in the <code>vars</code> directory:</p>
  <pre><code>// MultiMethod.groovy
  
  def Foo() { ... }
  
  def Bar() { ... }
  </code></pre>
  <p>Now if i want to use those functions from the pipeline, what i did was:</p>
  <pre><code>// Jenkinsfile
  @Library('LibName') _
  
  pipeline {
    ...
    steps {
      script { 
          // Method (1): this will work
          def var = new MultiMethod()
          var.Foo()
          var.Bar()
  
          // Method (2) this will not work
            MultiMethod.Foo()
         }
    }
  
  }
  </code></pre>
  <p>(The (1) and (2) methods are methods of calling the methods in the groovy script. Don't be confused by these 2 uses of the word &quot;Method&quot; please.) <Br></p>
  <p>So it works only if I instantiate this <code>MultiMethod</code> with the <code>new</code> operator.</p>
  <p>But, if I name the file <code>multiMethod</code> (camelCased) instead of <code>MultiMethod</code>, i can use method (2) to call the methods in the script. Can someone explain this behavior? <br></p>
  <p>That seems to be working fine.</p>
  <h2>Second issue</h2>
  <p>Based on the example above. If I have the groovy file named <code>MultiMethod</code>, (We saw earlier that i can use its methods if I instantiate with with <code>new</code>), I can't seem to instantiate an object of <code>MultiMethod</code> when loading the library dynamically, like this:</p>
  <pre><code>// Jenkinsfile
  
  pipeline {
    ...
    steps {
      script { 
          // Method (1): this will not work
          library 'LibName'
          def var = new MultiMethod()
          var.Foo()
          var.Bar()
  
         }
    }
  
  }
  </code></pre>
  <p>If i try to do so, i get this:</p>
  <pre><code>Running in Durability level: MAX_SURVIVABILITY
  org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
  WorkflowScript: 11: unable to resolve class multiMethod 
   @ line 11, column 32.
                         def mult = new multiMethod()
                                    ^
  
  1 error
  
      at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
      at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:958)
      at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUni  
  ...
  </code></pre>
  <h2>EDIT</h2>
  <p>I notice that if I do this:</p>
  <pre><code>// Jenkinsfile
  
  pipeline {
    ...
    steps {
      script { 
          library 'LibName'
          MultiMethod.Foo()
          MultiMethod.Bar()
         }
    }
  
  }
  </code></pre>
  <p>It does work !!</p>
  <h2>Last Question</h2>
  <p>Another question if you may. I noticed that people use to write</p>
  <pre><code>return this
  </code></pre>
  <p>In the end of their scripts in the <code>vars</code> directory. Can someone explain what is it good for? I'd be happy if someone could explain this in the context of how does the mechanism of this works, like why are those scripts turns into Global Variables?</p>
  <p>Thanks</p>