# End matching

**URL:** <https://rubytalk.org/t/end-matching/23354>\
**Category:** ruby-talk\
**Created:** [16 December 2005 16:16 UTC](https://rubytalk.org/t/end-matching/23354 "2005-12-16T16:16:51Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve\_Litt](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@Steve\_Litt](https://rubytalk.org/u/Steve_Litt)\
**Post date:** [16 December 2005 16:16 UTC](https://rubytalk.org/t/end-matching/23354/1 "2005-12-16T16:16:51Z")

</div>

Hi all,

That's it, THAT'S IT! I'm tired of forgetting an end or inserting  
one too many, and traipsing through the whole program to find it.  
Vim doesn't really match End statements with their keywords.

So I'm going to write an end matching program. Could somebody please  
list all the keywords that are ended by "end"? Do any of these  
keywords ever not end in "end"?

Thanks

SteveT

Steve Litt  
[http://www.troubleshooters.com](http://www.troubleshooters.com)  
[slitt@troubleshooters.com](mailto:slitt@troubleshooters.com)

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [16 December 2005 16:27 UTC](https://rubytalk.org/t/end-matching/23354/2 "2005-12-16T16:27:46Z")

</div>

Steve Litt wrote:

> Hi all,
> 
> That's it, THAT'S IT! I'm tired of forgetting an end or inserting  
> one too many, and traipsing through the whole program to find it.  
> Vim doesn't really match End statements with their keywords.
> 
> So I'm going to write an end matching program. Could somebody please  
> list all the keywords that are ended by "end"? Do any of these  
> keywords ever not end in "end"?

"class", "module", "def", "do", "begin", "while", "until", "if",  
"unless" - control flow statements can be used as post expression in which  
case they do not end in "end":

puts foo if foo \> 10

You might be able to make your life easier by using a Ruby parser. There  
are even implementation(s) in Ruby.

Btw, there's an easier cure for this: just get used to typing the "end"  
immediately after typing the starting token, then go back a word and type  
the containing text. Same works for all sorts of brackets etc.

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Jim\_Freeze2](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@Jim\_Freeze2](https://rubytalk.org/u/Jim_Freeze2)\
**Post date:** [16 December 2005 16:29 UTC](https://rubytalk.org/t/end-matching/23354/3 "2005-12-16T16:29:10Z")

</div>

The rubyvim doesn't match 'end'? I used to use this,  
but haven't needed it for years.

> **···**
>
> On 12/16/05, Steve Litt \<slitt@earthlink.net\> wrote:
> 
> > Hi all,
> > 
> > That's it, THAT'S IT! I'm tired of forgetting an end or inserting  
> > one too many, and traipsing through the whole program to find it.  
> > Vim doesn't really match End statements with their keywords.
> 
> --  
> Jim Freeze

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [16 December 2005 16:31 UTC](https://rubytalk.org/t/end-matching/23354/4 "2005-12-16T16:31:09Z")

</div>

class / module / def  
begin / do  
if / unless (end not requires when used as statement modifiers)  
while / until (end not requires when used as statement modifiers)

Hope that's all of them, but I may have missed some.

James Edward Gray II

> **···**
>
> On Dec 16, 2005, at 10:16 AM, Steve Litt wrote:
> 
> > Hi all,
> > 
> > That's it, THAT'S IT! I'm tired of forgetting an end or inserting  
> > one too many, and traipsing through the whole program to find it.  
> > Vim doesn't really match End statements with their keywords.
> > 
> > So I'm going to write an end matching program. Could somebody please  
> > list all the keywords that are ended by "end"? Do any of these  
> > keywords ever not end in "end"?

---

<div class="post-metadata">

**Author:** ![Paul\_Battley](https://avatars.discourse-cdn.com/v4/letter/p/f07891/32.png) [@Paul\_Battley](https://rubytalk.org/u/Paul_Battley)\
**Post date:** [16 December 2005 16:36 UTC](https://rubytalk.org/t/end-matching/23354/5 "2005-12-16T16:36:59Z")

</div>

> So I'm going to write an end matching program. Could somebody please  
> list all the keywords that are ended by "end"? Do any of these  
> keywords ever not end in "end"?

One wrinkle: 'do' does not always end in 'end'. It can be a method,  
and is used as such in DBI (e.g. 'dbh.do(...)'). It should be easy  
enough to handle that case by checking for a preceding period,  
however.

Paul.

---

<div class="post-metadata">

**Author:** ![Hugh\_Sasse](https://avatars.discourse-cdn.com/v4/letter/h/d6d6ee/32.png) [@Hugh\_Sasse](https://rubytalk.org/u/Hugh_Sasse)\
**Post date:** [16 December 2005 16:37 UTC](https://rubytalk.org/t/end-matching/23354/6 "2005-12-16T16:37:48Z")

</div>

> Hi all,
> 
> That's it, THAT'S IT! I'm tired of forgetting an end or inserting  
> one too many, and traipsing through the whole program to find it.

It's a bit of a problem, I agree. I usually create them when I  
create the /if|def|do|case|module|class|while/ and create the contents after.

There have been limitations imposed on Ruby by having the parser  
implemented in YACC which have come up before. One is that it is  
nontrivial to get really good error diagnostics out.

> Vim doesn't really match End statements with their keywords.

:he matchit

You need to do a little installation of matchit for it to work  
properly, because vim defaults to behaving like VI.

> So I'm going to write an end matching program. Could somebody please  
> list all the keywords that are ended by "end"? Do any of these  
> keywords ever not end in "end"?

Before you go to the trouble of all that, whwat is your vimrc like?  
maybe there are more things you can turn on that will help.

Mine, somewhat pruned, is like this:  
\<quote\>  
set autoindent  
set expandtab  
set shiftwidth=2 " Mostly Ruby development -- std convention 2, not 4  
set textwidth=68 " Do want this generally applied now...  
"set ttyscroll=0  
"  
runtime ftplugin/man.vim  
filetype plugin on "if you want the ftplugin to run  
filetype indent on "if you want indenting support  
syntax on  
autocmd FileType \* exec('setlocal dict+='.$VIMRUNTIME.'/syntax/'.expand('\<amatch\>').'.vim')  
runtime plugin/matchit.vim

"nmap \<M-R\> :exe ":undo|:set paste|:normal .:set nopaste"

"Give a status line always  
set ls=2  
\</quote\>

> Thanks
> 
> SteveT

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hugh

> **···**
>
> On Sat, 17 Dec 2005, Steve Litt wrote:

---

<div class="post-metadata">

**Author:** ![Ross\_Bamford2](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@Ross\_Bamford2](https://rubytalk.org/u/Ross_Bamford2)\
**Post date:** [16 December 2005 18:02 UTC](https://rubytalk.org/t/end-matching/23354/7 "2005-12-16T18:02:46Z")

</div>

Been there 😑

I have the indent plugin enabled, and my Vim seems to match 'end's quite well with that (I didn't try it without, and I suppose I may have forgotten about some extra stuff I installed?). Because the end token is indented to the same position as the start, and highlighted the same colour, it makes it pretty easy to see what's what most of the time.

It's not the same as proper end-matching, of course, but if nothing else it might mean that, somewhere inside that plugin('s configuration), there is a list of start/end tokens...

Ross

> **···**
>
> On Fri, 16 Dec 2005 16:16:51 -0000, Steve Litt \<slitt@earthlink.net\> wrote:
> 
> > That's it, THAT'S IT! I'm tired of forgetting an end or inserting  
> > one too many, and traipsing through the whole program to find it.  
> > Vim doesn't really match End statements with their keywords.
> 
> --  
> Ross Bamford - rosco@roscopeco.remove.co.uk  
> "\e[1;33mL"

---

<div class="post-metadata">

**Author:** ![Eero\_Saynatkari1](https://avatars.discourse-cdn.com/v4/letter/e/46a35a/32.png) [@Eero\_Saynatkari1](https://rubytalk.org/u/Eero_Saynatkari1)\
**Post date:** [16 December 2005 22:18 UTC](https://rubytalk.org/t/end-matching/23354/8 "2005-12-16T22:18:07Z")

</div>

Steve Litt wrote:

> Hi all,
> 
> That's it, THAT'S IT! I'm tired of forgetting an end or inserting  
> one too many, and traipsing through the whole program to find it.  
> Vim doesn't really match End statements with their keywords.
> 
> So I'm going to write an end matching program. Could somebody please  
> list all the keywords that are ended by "end"? Do any of these  
> keywords ever not end in "end"?

Actually, I recommend a proactive approach: just condition  
yourself to input the ending character at the same time you  
write the starting block. So, type a 'do', type an 'end' and  
then backtrack your cursor betwixt.

Also, the better editors support brace/block matching. Vim's  
'%' works in most ruby instances, for example.

> Thanks
> 
> SteveT

E

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [16 December 2005 16:33 UTC](https://rubytalk.org/t/end-matching/23354/9 "2005-12-16T16:33:32Z")

</div>

> Btw, there's an easier cure for this:

Definitely.

> just get used to typing the "end" immediately after typing the starting token

Or better have your editor do that busy work for you.

Proper indentation should make missing ends very obvious as well.

James Edward Gray II

> **···**
>
> On Dec 16, 2005, at 10:27 AM, Robert Klemme wrote:

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [16 December 2005 16:41 UTC](https://rubytalk.org/t/end-matching/23354/10 "2005-12-16T16:41:42Z")

</div>

I imagine all the words in that list have the same issue.

Checking for the period is a good start, but watch out for edge cases:

&nbsp;&nbsp;&nbsp;send(:do, \*args)

James Edward Gray II

> **···**
>
> On Dec 16, 2005, at 10:36 AM, Paul Battley wrote:
> 
> > > So I'm going to write an end matching program. Could somebody please  
> > > list all the keywords that are ended by "end"? Do any of these  
> > > keywords ever not end in "end"?
> > 
> > One wrinkle: 'do' does not always end in 'end'. It can be a method,  
> > and is used as such in DBI (e.g. 'dbh.do(...)'). It should be easy  
> > enough to handle that case by checking for a preceding period,  
> > however.

---

<div class="post-metadata">

**Author:** ![Doug\_H1](https://avatars.discourse-cdn.com/v4/letter/d/cc9497/32.png) [@Doug\_H1](https://rubytalk.org/u/Doug_H1)\
**Post date:** [18 December 2005 19:27 UTC](https://rubytalk.org/t/end-matching/23354/11 "2005-12-18T19:27:46Z")

</div>

Has anyone ever written a proposal for optional labels after an "end".  
I've written a parser for another language that allows that. So for  
example you can type "end class" at the end of a class. If you forget  
to type an "end def" inside the class it will tell you the error you  
made more clearly instead of matching "end class". And you can still  
just type "end" alone to match anything.

Oh, and just to mention this for the heck of it (not that it should be  
seriously considered for ruby), you can actually parse the same code  
with no "end" at all if the lines in a block are all indented the same.  
You don't need the colon like in python. Just noting that after  
seeing a failed ruby proposal that seemed to imply the colon was  
required: [http://www.rcrchive.net/rcr/show/299](http://www.rcrchive.net/rcr/show/299)

---

<div class="post-metadata">

**Author:** ![Bill\_Guindon1](https://avatars.discourse-cdn.com/v4/letter/b/4da419/32.png) [@Bill\_Guindon1](https://rubytalk.org/u/Bill_Guindon1)\
**Post date:** [16 December 2005 16:48 UTC](https://rubytalk.org/t/end-matching/23354/12 "2005-12-16T16:48:36Z")

</div>

> \>\> So I'm going to write an end matching program. Could somebody please  
> \>\> list all the keywords that are ended by "end"? Do any of these  
> \>\> keywords ever not end in "end"?  
> \>  
> \> One wrinkle: 'do' does not always end in 'end'. It can be a method,  
> \> and is used as such in DBI (e.g. 'dbh.do(...)'). It should be easy  
> \> enough to handle that case by checking for a preceding period,  
> \> however.
> 
> I imagine all the words in that list have the same issue.
> 
> Checking for the period is a good start, but watch out for edge cases:
> 
> &nbsp;&nbsp;&nbsp;send(:do, \*args)

and speaking of 'cases', don't forget 'case'.

> **···**
>
> On 12/16/05, James Edward Gray II \<james@grayproductions.net\> wrote:
> 
> > On Dec 16, 2005, at 10:36 AM, Paul Battley wrote:
> 
> > James Edward Gray II
> 
> --  
> Bill Guindon (aka aGorilla)

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello1](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@Martin\_DeMello1](https://rubytalk.org/u/Martin_DeMello1)\
**Post date:** [18 December 2005 18:12 UTC](https://rubytalk.org/t/end-matching/23354/13 "2005-12-18T18:12:47Z")

</div>

Would probablly be better to check for everything that \*can\* precede the  
'do' and have it be a real end-ended do.

martin

> **···**
>
> James Edward Gray II \<james@grayproductions.net\> wrote:
> 
> > On Dec 16, 2005, at 10:36 AM, Paul Battley wrote:
> > 
> > \>\> So I'm going to write an end matching program. Could somebody please  
> > \>\> list all the keywords that are ended by "end"? Do any of these  
> > \>\> keywords ever not end in "end"?  
> > \>  
> > \> One wrinkle: 'do' does not always end in 'end'. It can be a method,  
> > \> and is used as such in DBI (e.g. 'dbh.do(...)'). It should be easy  
> > \> enough to handle that case by checking for a preceding period,  
> > \> however.
> > 
> > I imagine all the words in that list have the same issue.
> > 
> > Checking for the period is a good start, but watch out for edge cases:
> > 
> > &nbsp;&nbsp;&nbsp;send(:do, \*args)

---

<div class="post-metadata">

**Author:** ![Ross\_Bamford2](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@Ross\_Bamford2](https://rubytalk.org/u/Ross_Bamford2)\
**Post date:** [19 December 2005 03:32 UTC](https://rubytalk.org/t/end-matching/23354/14 "2005-12-19T03:32:48Z")

</div>

Guys, take a look at Vim's implementation. On my system the pertinent files are:

&nbsp;&nbsp;&nbsp;/usr/share/vim/vim63/indent/ruby.vim  
&nbsp;&nbsp;&nbsp;/usr/share/vim/vim63/syntax/ruby.vim

The indent one seems to have everything you need, but the syntax is concerned with end matching too (for colouring ends belonging to ifs different to ends belonging to blocks, and what have you).

Now all you need is to port it to Ruby 😉

> **···**
>
> On Sun, 18 Dec 2005 18:10:08 -0000, Martin DeMello \<martindemello@yahoo.com\> wrote:
> 
> > James Edward Gray II \<james@grayproductions.net\> wrote:
> > 
> > > On Dec 16, 2005, at 10:36 AM, Paul Battley wrote:
> > > 
> > > \>\> So I'm going to write an end matching program. Could somebody please  
> > > \>\> list all the keywords that are ended by "end"? Do any of these  
> > > \>\> keywords ever not end in "end"?  
> > > \>  
> > > \> One wrinkle: 'do' does not always end in 'end'. It can be a method,  
> > > \> and is used as such in DBI (e.g. 'dbh.do(...)'). It should be easy  
> > > \> enough to handle that case by checking for a preceding period,  
> > > \> however.
> > > 
> > > I imagine all the words in that list have the same issue.
> > > 
> > > Checking for the period is a good start, but watch out for edge cases:
> > > 
> > > &nbsp;&nbsp;&nbsp;send(:do, \*args)
> > 
> > Would probablly be better to check for everything that \*can\* precede the  
> > 'do' and have it be a real end-ended do.
> > 
> > martin
> 
> --  
> Ross Bamford - rosco@roscopeco.remove.co.uk
