# Need help with $end vs kEND error

**URL:** <https://www.ruby-forum.com/t/need-help-with-end-vs-kend-error/71676>\
**Category:** Ruby\
**Created:** [October 10, 2006, 6:38pm UTC](https://www.ruby-forum.com/t/need-help-with-end-vs-kend-error/71676 "2006-10-10T18:38:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![highlander872](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ac8455/32.png) [@highlander872](https://www.ruby-forum.com/u/highlander872)\
**Post date:** [October 10, 2006, 6:38pm UTC](https://www.ruby-forum.com/t/need-help-with-end-vs-kend-error/71676/1 "2006-10-10T18:38:57Z")

</div>

I have been trying to write a program for my class assignment but  
everytime i do i get an error on the last line of the code that says  
‘unexpected $end, expecting kEND’  
this happens on every program i try to run  
any ideas?

i am using fedora core 5 if that makes a difference

---

<div class="post-metadata">

**Author:** ![highlander872](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ac8455/32.png) [@highlander872](https://www.ruby-forum.com/u/highlander872)\
**Post date:** [October 10, 2006, 6:44pm UTC](https://www.ruby-forum.com/t/need-help-with-end-vs-kend-error/71676/2 "2006-10-10T18:44:35Z")

</div>

Kd Kushelduv wrote:

> I have been trying to write a program for my class assignment but  
> everytime i do i get an error on the last line of the code that says  
> ‘unexpected $end, expecting kEND’  
> this happens on every program i try to run  
> any ideas?
> 
> i am using fedora core 5 if that makes a difference

Often times this error is a result of missing an end statement somewhere  
within your program. For example, you may have forgotten to end a  
method, if statement or class. Can you post some code so we can take a  
look?

---

<div class="post-metadata">

**Author:** ![highlander872](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ac8455/32.png) [@highlander872](https://www.ruby-forum.com/u/highlander872)\
**Post date:** [October 10, 2006, 6:46pm UTC](https://www.ruby-forum.com/t/need-help-with-end-vs-kend-error/71676/3 "2006-10-10T18:46:14Z")

</div>

You have mismatched block openings / endings. Check your def / end  
do / end if / end, etc…

---

<div class="post-metadata">

**Author:** ![highlander872](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ac8455/32.png) [@highlander872](https://www.ruby-forum.com/u/highlander872)\
**Post date:** [October 10, 2006, 6:46pm UTC](https://www.ruby-forum.com/t/need-help-with-end-vs-kend-error/71676/4 "2006-10-10T18:46:13Z")

</div>

I’m just learning ruby myself, but typically when this  
happens, I’m missing an end to a function, or some other  
block:

#!/usr/bin/ruby

def joe  
print “JOE MAMMA\n”

Produces:

[alibby@localhost ~]$ ./t.rb  
./t.rb:6: syntax error, unexpected $end, expecting kEND

Perhaps this is it? If you’re willing to post the code (if  
it’s not too long) we may be able to provide more guidance.

Andy

---

<div class="post-metadata">

**Author:** ![highlander872](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ac8455/32.png) [@highlander872](https://www.ruby-forum.com/u/highlander872)\
**Post date:** [October 10, 2006, 8:23pm UTC](https://www.ruby-forum.com/t/need-help-with-end-vs-kend-error/71676/5 "2006-10-10T20:23:02Z")

</div>

as far as i can tell nothing is missing  
here is the code (its only three methods):

def promptToQuit  
print "\nYou did not enter any numbers \n are you sure you wish to  
quit? (y/n): "  
answer = getc  
answer = answer.downcase  
while (answer != ‘y’ && answer != ‘n’)  
print "Are you sure you wish to quit? (y/n): "  
answer = getc  
answer = answer.downcase  
end

return answer  
end

def getNumbers (num)  
$high = num  
$low = num

while (num != 0)  
print "Please enter a number (0 to quit): "  
num = geti

```
  if ( (num > $high) && (num != 0) ) then
     $high = num
  else if ( (num < $low) && (num != 0) ) then
     $low = num
  end

```

end  
end

answer = ‘n’

print "\nPlease enter a number (0 to quit): "  
num = geti

if (num == 0) then  
answer = promptToQuit  
end

if (answer == ‘y’) then  
print “\nGoodbye”  
else  
num = 1  
getNumbers(num)  
end

puts "Highest Number: " + $high.to\_s  
puts "Lowest Number: " + $low.to\_s  
puts “\n”

---

<div class="post-metadata">

**Author:** ![highlander872](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ac8455/32.png) [@highlander872](https://www.ruby-forum.com/u/highlander872)\
**Post date:** [October 10, 2006, 8:40pm UTC](https://www.ruby-forum.com/t/need-help-with-end-vs-kend-error/71676/6 "2006-10-10T20:40:01Z")

</div>

huh that was the problem but my text book had everything as  
elseif

i added the space because it wouldn’t accept that  
but it seems the author still mistyped the entire thing

o well

---

<div class="post-metadata">

**Author:** ![highlander872](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ac8455/32.png) [@highlander872](https://www.ruby-forum.com/u/highlander872)\
**Post date:** [October 10, 2006, 8:34pm UTC](https://www.ruby-forum.com/t/need-help-with-end-vs-kend-error/71676/7 "2006-10-10T20:34:59Z")

</div>

Kd Kushelduv wrote:

> ```
> if ( (num > $high) && (num != 0) ) then
> $high = num
> else if ( (num < $low) && (num != 0) ) then
> $low = num
> end
> 
> ```

This should be

```
   if ( (num > $high) && (num != 0) ) then
      $high = num
   elsif ( (num < $low) && (num != 0) ) then
      $low = num
   end

```

Your else is starting a new block context (not the Ruby term I’m sure;  
that’s kinda what I’d call it in Perl 🙂 and Ruby thinks what you’re  
doing is:

if …

else  
if …  
end  
end

But you’ve only got a single end so it’s confuzzled.

---

<div class="post-metadata">

**Author:** ![highlander872](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ac8455/32.png) [@highlander872](https://www.ruby-forum.com/u/highlander872)\
**Post date:** [October 11, 2006, 3:01am UTC](https://www.ruby-forum.com/t/need-help-with-end-vs-kend-error/71676/8 "2006-10-11T03:01:27Z")

</div>

Kd Kushelduv wrote:

> huh that was the problem but my text book had everything as  
> elseif
> 
> i added the space because it wouldn’t accept that  
> but it seems the author still mistyped the entire thing

Umm, just curious, who is the author, what is the book or article?

---

<div class="post-metadata">

**Author:** ![highlander872](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ac8455/32.png) [@highlander872](https://www.ruby-forum.com/u/highlander872)\
**Post date:** [October 10, 2006, 9:12pm UTC](https://www.ruby-forum.com/t/need-help-with-end-vs-kend-error/71676/9 "2006-10-10T21:12:53Z")

</div>

On 2006.10.11 03:23, Kd Kushelduv wrote:

> ```
> answer = getc
> 
> ```
> 
> while (num != 0)  
> print "Please enter a number (0 to quit): "  
> num = geti
> 
> ```
> if ( (num > $high) && (num != 0) ) then
> $high = num
> else if ( (num < $low) && (num != 0) ) then
> $low = num
> 
> ```

Right here. You want elsif, otherwise you are missing  
the closing end for the nested if. Also, drop the thens,  
they are not needed 🙂

> answer = promptToQuit  
> puts "Lowest Number: " + $low.to\_s  
> puts “\n”

The error message is a bit ambiguous–$end means EOF, which  
Ruby encountered while it was still waiting for a literal  
end (the token is called kEND).
