# Unbalanced code snippet include

**URL:** <https://www.ruby-forum.com/t/unbalanced-code-snippet-include/72268>\
**Category:** Ruby\
**Created:** [October 16, 2006, 4:20am UTC](https://www.ruby-forum.com/t/unbalanced-code-snippet-include/72268 "2006-10-16T04:20:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [October 16, 2006, 4:20am UTC](https://www.ruby-forum.com/t/unbalanced-code-snippet-include/72268/1 "2006-10-16T04:20:25Z")

</div>

Is there a way to insert code which is unbalanced into Ruby source.  
For example, I have a condition which is checked in many places, but  
which i am not yet sure is the correct condition, so i want to keep only  
that condition in an “include” library called “not yet completed  
condition” or “condition which i reckon will change and it is used  
everywhere”.

This include library may contain this code -

if condition then

I would like to be able to insert this in my source code with an in situ  
“include”, but the Ruby include expects a complete block.

the source code would look like this-  
includesnippet “C:/INSERTS/IFNOTBROKEN.RB.INS”

While you may think this is crazy, and i can certainly see your point,  
this question is related to converting from another language to Ruby, so  
i just have to ask.

Regards,  
Peter

---

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [October 18, 2006, 10:41am UTC](https://www.ruby-forum.com/t/unbalanced-code-snippet-include/72268/2 "2006-10-18T10:41:11Z")

</div>

On 10/15/06, Peter L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I would like to be able to insert this in my source code with an in situ  
> “include”, but the Ruby include expects a complete block.
> 
> the source code would look like this-  
> includesnippet “C:/INSERTS/IFNOTBROKEN.RB.INS”
> 
> While you may think this is crazy, and i can certainly see your point,  
> this question is related to converting from another language to Ruby, so  
> i just have to ask.

Include doesn’t include a file, but a module.

You can do what you want with load but you need to work around the way  
ruby treats line breaks, note how I left the if unfinished and closed  
it after the load:

rick@frodo:/public/rubyscripts$ cat condition.rb  
true  
rick@frodo:/public/rubyscripts$ cat loadtest.rb  
if (  
load ‘condition.rb’  
)  
puts “included file evaluated to 1”  
else  
puts “included file evaluated to non-1”  
end

rick@frodo:/public/rubyscripts$ ruby loadtest.rb  
included file evaluated to 1

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

IPMS/USA Region 12 Coordinator  
[http://ipmsr12.denhaven2.com/](http://ipmsr12.denhaven2.com/)

Visit the Project Mercury Wiki Site  
[http://www.mercuryspacecraft.com/](http://www.mercuryspacecraft.com/)

---

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [October 18, 2006, 10:44am UTC](https://www.ruby-forum.com/t/unbalanced-code-snippet-include/72268/3 "2006-10-18T10:44:10Z")

</div>

On 10/15/06, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 10/15/06, Peter L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

As an afterthought, having answered your question without analysis of  
whether this is a good thing, there are probably saner approaches, two  
which come to mind are:

1. defining the condition separately as a function or a lambda.
2. defining it as a string and using one of the eval family of  
functions to evaluate it.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [October 18, 2006, 10:44am UTC](https://www.ruby-forum.com/t/unbalanced-code-snippet-include/72268/4 "2006-10-18T10:44:11Z")

</div>

On 2006.10.16 11:45, Rick DeNatale wrote:

> On 10/15/06, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > On 10/15/06, Peter L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> As an afterthought, having answered your question without analysis of  
> whether this is a good thing, there are probably saner approaches, two  
> which come to mind are:
> 
> 1. defining the condition separately as a function or a lambda.
> 2. defining it as a string and using one of the eval family of  
> functions to evaluate it.

Definitely. The best thing to do would be to define a top-level  
method for it, #require the file and then use the method in the  
conditional. Maintaining the proposed scheme would probably be  
a nightmare.
