[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Pulling a class from a ruby file

Abhishek Ray

5/16/2008 4:30:00 PM

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.
--
Posted via http://www.ruby-....

4 Answers

Avdi Grimm

5/16/2008 4:59:00 PM

0

On Fri, May 16, 2008 at 12:30 PM, Abhishek Ray <hey_a_ray@hotmail.com> 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:...
Developer Blog: http:.../devblog/
Twitter: http://twitte...
Journal: http://avdi.livej...

Joel VanderWerf

5/17/2008 8:49:00 PM

0

Avdi Grimm wrote:
> On Fri, May 16, 2008 at 12:30 PM, Abhishek Ray <hey_a_ray@hotmail.com> 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

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

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Mark Wilden

5/17/2008 11:18:00 PM

0

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

> def external
> yield unless $testing
> end
>
> external do
> class C
> end
>
> def foo
> end
> end
>
> C
> foo

This is cool, but is it any different than

unless $testing
class C
end
end

///ark

Joel VanderWerf

5/17/2008 11:25:00 PM

0

Mark Wilden wrote:
> On May 17, 2008, at 1:48 PM, Joel VanderWerf wrote:
>
>> def external
>> yield unless $testing
>> end
>>
>> external do
>> class C
>> end
>>
>> def foo
>> end
>> end
>>
>> C
>> foo
>
> This is cool, but is it any different than
>
> unless $testing
> class C
> 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....

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407