# How to abruptly end a program?

**URL:** <https://www.ruby-forum.com/t/how-to-abruptly-end-a-program/132010>\
**Category:** Ruby\
**Created:** [March 15, 2008, 7:01am UTC](https://www.ruby-forum.com/t/how-to-abruptly-end-a-program/132010 "2008-03-15T07:01:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [March 15, 2008, 7:01am UTC](https://www.ruby-forum.com/t/how-to-abruptly-end-a-program/132010/1 "2008-03-15T07:01:50Z")

</div>

I read in \<Programming Ruby 2nd ed.\> (p. 303) - "“If Ruby comes across a  
line anywhere in the source containing just “\_ _END_ \_”, with no leading  
or trailing whitespace, it treats that line as the end of the  
program—any subsequent lines will not be treated as program code.”

That’s not what’s happening for me. The following -  
\_ _END_ \_  
**END**  
_END_

all produce the same result for me - “undefined local variable or  
method”  
That DOES bring the program to an abrupt end, but its rather ungraceful.  
Is this the intended result - a crash due to an interpreter error?

Tom

–

```auto
Tom C., MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< removed_email_address@domain.invalid >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)

```

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [March 15, 2008, 7:12am UTC](https://www.ruby-forum.com/t/how-to-abruptly-end-a-program/132010/2 "2008-03-15T07:12:23Z")

</div>

Hi,

On Sat, Mar 15, 2008 at 5:01 PM, Tom C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> all produce the same result for me - “undefined local variable or method”  
> That DOES bring the program to an abrupt end, but its rather ungraceful.  
> Is this the intended result - a crash due to an interpreter error?

Certainly not 🙂 Though I guess any identifier could do that then. Are  
you  
sure there’s no whitespace or other content on the line? Can you show us  
an  
example?

celtic@sohma:~$ cat test.rb  
#!/usr/bin/env ruby

puts “Hi there!”

**END**

This is just great.

celtic@sohma:~$ ruby test.rb  
Hi there!  
celtic@sohma:~$

Thanks!

Arlen

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [March 15, 2008, 7:14am UTC](https://www.ruby-forum.com/t/how-to-abruptly-end-a-program/132010/3 "2008-03-15T07:14:10Z")

</div>

Tom C. wrote:

> all produce the same result for me - “undefined local variable or method”  
> That DOES bring the program to an abrupt end, but its rather ungraceful.  
> Is this the intended result - a crash due to an interpreter error?

What the quote (a bit ambiguous perhaps) means is that the **END** line  
and following lines are not treated as part of the program text. It has  
nothing to do with control flow. It’s like comment block that runs to  
the end of the file without any explicit termination. (It’s more than  
that because you can read it with DATA.)

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [March 15, 2008, 7:54am UTC](https://www.ruby-forum.com/t/how-to-abruptly-end-a-program/132010/4 "2008-03-15T07:54:38Z")

</div>

Tom C. wrote:

> > > **END**  
> > > It has nothing to do with control flow. It’s like comment block that  
> > > runs to the end of the file without any explicit termination. (It’s  
> > > more than that because you can read it with DATA.)
> 
> OK, but couldn’t ANY uninitialized variable achieve the same messy  
> result? What’s the point? This surely can’t be the intended result.  
> Makes no sense to me.

**END** by itself on a line shouldn’t exit the program. If you’re  
getting that error, maybe there is an extra char on the line. A space  
perhaps?

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [March 15, 2008, 7:48am UTC](https://www.ruby-forum.com/t/how-to-abruptly-end-a-program/132010/5 "2008-03-15T07:48:10Z")

</div>

Joel VanderWerf wrote:

> > _END_  
> > runs to the end of the file without any explicit termination. (It’s  
> > more than that because you can read it with DATA.)

OK, but couldn’t ANY uninitialized variable achieve the same messy  
result? What’s the point? This surely can’t be the intended result.  
Makes no sense to me.

t.

–

```auto
Tom C., MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< removed_email_address@domain.invalid >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)

```

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [March 15, 2008, 8:38am UTC](https://www.ruby-forum.com/t/how-to-abruptly-end-a-program/132010/6 "2008-03-15T08:38:35Z")

</div>

Tom C. wrote:

> result:  
> test under way  
> input var:  
> 5  
> 5
> 
> Here, we don’t get an error. We just get ignored.  
> This is nuts.

Looks correct. The **END** line and everything after it is ignored.  
What’s your example with an error?

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [March 15, 2008, 8:19am UTC](https://www.ruby-forum.com/t/how-to-abruptly-end-a-program/132010/7 "2008-03-15T08:19:51Z")

</div>

Joel VanderWerf wrote:

> > > > \_ _END_ \_  
> > > > line and following lines are not treated as part of the program  
> > > > perhaps?

Yes, that would explain it, but I made sure this problem didn’t occur  
from the beginning, which is one reason I’m puzzled. I really don’t see  
anything wrong with what I’m feeding the interpreter.

So, I did a different test program -

require ‘readline’  
puts "test under way\ninput var: "  
#opt = readline( “=–\> \n”)  
opt = gets.chomp  
puts( opt)  
**END**  
puts( opt)

result:  
test under way  
input var:  
5  
5

Here, we don’t get an error. We just get ignored.  
This is nuts.

t.

–

```auto
Tom C., MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< removed_email_address@domain.invalid >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)

```

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [March 15, 2008, 8:42am UTC](https://www.ruby-forum.com/t/how-to-abruptly-end-a-program/132010/8 "2008-03-15T08:42:41Z")

</div>

On Saturday 15 March 2008, Tom C. wrote:

> > > > > That’s not what’s happening for me. The following -  
> > > > > What the quote (a bit ambiguous perhaps) means is that the **END**  
> > > > > getting that error, maybe there is an extra char on the line. A space  
> > > > > #opt = readline( “=–\> \n”)
> 
> Here, we don’t get an error. We just get ignored.  
> This is nuts.
> 
> t.

As others said, when ruby sees the **END** identifier on a line by  
itself  
(without leading or trailing spaces), it treats it, and any text  
following it,  
approximately as a comment. This means that the code is not parsed  
(syntax  
errors after that line aren’t recognized) and, obviously, not executed.  
Besides, the constant DATA is set to a File object which contains that  
text.

This is consistent with the result you get from your piece of code:  
without  
the **END** , 5 would have been written 3 times: the first when you  
entered it  
from the keyboard, the other two because of the two puts. Since the  
second  
puts is after **END** , instead, you only get it twice: the second puts  
is  
ignored.

Try this, for example:

puts “This is the class of DATA: #{DATA.class}”  
puts “These are the contents of DATA:\n\n#{DATA.read}”  
**END**  
This is not valid ruby code

The output is:  
This is the class of DATA: File  
These are the contents of DATA:

This is not valid ruby code

Of course, removing the **END** line causes a syntax error:

./prova.rb:6: syntax error, unexpected kNOT, expecting $end  
This is not valid ruby code

Regarding your first piece of code (the one which produces the  
NoMethodError),  
are you sure the error isn’t produced before end?

Stefano

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [March 15, 2008, 8:56am UTC](https://www.ruby-forum.com/t/how-to-abruptly-end-a-program/132010/9 "2008-03-15T08:56:09Z")

</div>

Stefano C. wrote:

> > > > > > I read in \<Programming Ruby 2nd ed.\> (p. 303) - ""If Ruby comes  
> > > > > > all produce the same result for me - "undefined local variable or
> > 
> > anything wrong with what I’m feeding the interpreter.
> 
> puts is after **END** , instead, you only get it twice: the second puts is  
> This is the class of DATA: File  
> are you sure the error isn’t produced before end?
> 
> Stefano

Stefano,  
You’re right. I misread my own output - seeing two 5’s, I thought them  
to be output, without realizing that the first wasn’t. Yes, it’s getting  
a bit late here. Maybe it’s time to stop.

Thanks to you and Joel for your comments!

t.

–

```auto
Tom C., MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< removed_email_address@domain.invalid >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)

```
