# Pulling a class from a ruby file

**URL:** <https://www.ruby-forum.com/t/pulling-a-class-from-a-ruby-file/137830>\
**Category:** Ruby\
**Created:** [May 16, 2008, 6:30pm UTC](https://www.ruby-forum.com/t/pulling-a-class-from-a-ruby-file/137830 "2008-05-16T18:30:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![twdev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@twdev](https://www.ruby-forum.com/u/twdev)\
**Post date:** [May 16, 2008, 6:30pm UTC](https://www.ruby-forum.com/t/pulling-a-class-from-a-ruby-file/137830/1 "2008-05-16T18:30:00Z")

</div>

I have a single ruby file, consisting of a class definition, and some  
code outside the class. Our tech lead wants this keep all this in a  
single file. I also have a unit test in a seperate ruby file which tests  
the class. The problem is that I want the unit test to load only the  
class without executing the code outside the class, which I can’t do  
with ‘require’ or ‘load’.

Is there any way in Ruby to load only a single class from another file.

---

<div class="post-metadata">

**Author:** ![twdev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@twdev](https://www.ruby-forum.com/u/twdev)\
**Post date:** [May 16, 2008, 7:00pm UTC](https://www.ruby-forum.com/t/pulling-a-class-from-a-ruby-file/137830/2 "2008-05-16T19:00:10Z")

</div>

On Fri, May 16, 2008 at 12:30 PM, Abhishek R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Is there any way in Ruby to load only a single class from another file.

No.

If you only want the external code the be executed when the file is  
run as an executable, and not when it is loaded by another file, you  
can enclose that code in a conditional, thus:

if $0 == **FILE**  
# … code to execute when run as an executable …  
end

I haven’t tested the above code. You might need to alter it to make  
it more robust, along the lines of:

if File.expand\_path($0) == File.expand\_path( **FILE** )  
# …  
end

–  
Avdi

Home: [http://avdi.org](http://avdi.org)  
Developer Blog: [Avdi Grimm, Code Cleric](http://avdi.org/devblog/)  
Twitter: [http://twitter.com/avdi](http://twitter.com/avdi)  
Journal: [http://avdi.livejournal.com](http://avdi.livejournal.com)

---

<div class="post-metadata">

**Author:** ![twdev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@twdev](https://www.ruby-forum.com/u/twdev)\
**Post date:** [May 17, 2008, 10:49pm UTC](https://www.ruby-forum.com/t/pulling-a-class-from-a-ruby-file/137830/3 "2008-05-17T22:49:14Z")

</div>

Avdi G. wrote:

> ```
> # ... code to execute when run as an executable ...
> 
> ```
> 
> end

or put the external code inside of some specialized construct:

def external  
yield unless $testing  
end

external do  
class C  
end

def foo  
end  
end

C  
foo

---

<div class="post-metadata">

**Author:** ![twdev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@twdev](https://www.ruby-forum.com/u/twdev)\
**Post date:** [May 18, 2008, 1:18am UTC](https://www.ruby-forum.com/t/pulling-a-class-from-a-ruby-file/137830/4 "2008-05-18T01:18:42Z")

</div>

On May 17, 2008, at 1:48 PM, Joel VanderWerf wrote:

> end
> 
> C  
> foo

This is cool, but is it any different than

unless $testing  
class C  
end  
end

///ark

---

<div class="post-metadata">

**Author:** ![twdev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@twdev](https://www.ruby-forum.com/u/twdev)\
**Post date:** [May 18, 2008, 1:27am UTC](https://www.ruby-forum.com/t/pulling-a-class-from-a-ruby-file/137830/5 "2008-05-18T01:27:31Z")

</div>

Mark W. wrote:

> > def foo  
> > end  
> > end

Oh, that would too and maybe even be better, if your condition is just a  
boolean. I was expecting there might be more to it than that…
