Forum: Ruby End matching

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1b62a85b59ccab03b84ee5ec378f75b4?d=identicon&s=25 Steve Litt (Guest)
on 2005-12-16 17:18
(Received via mailing list)
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
slitt@troubleshooters.com
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2005-12-16 17:30
(Received via mailing list)
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

    robert
8ecb8bb62c3283f8069a54056c7dc25f?d=identicon&s=25 Jim Freeze (Guest)
on 2005-12-16 17:30
(Received via mailing list)
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.

The rubyvim doesn't match 'end'? I used to use this,
but haven't needed it for years.
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2005-12-16 17:33
(Received via mailing list)
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"?

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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2005-12-16 17:36
(Received via mailing list)
On Dec 16, 2005, at 10:27 AM, Robert Klemme wrote:

> 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
2abf5beb51d5d66211d525a72c5cb39d?d=identicon&s=25 Paul Battley (Guest)
on 2005-12-16 17:39
(Received via mailing list)
> 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.
457cf540784a12ba2f30e06565a2c189?d=identicon&s=25 Hugh Sasse (Guest)
on 2005-12-16 17:39
(Received via mailing list)
On Sat, 17 Dec 2005, 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.

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
>
        Hugh
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2005-12-16 17:42
(Received via mailing list)
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:

   send(:do, *args)

James Edward Gray II
Ff63c03fd68754adbadd2c6314646bef?d=identicon&s=25 Bill Guindon (agorilla)
on 2005-12-16 17:51
(Received via mailing list)
On 12/16/05, James Edward Gray II <james@grayproductions.net> wrote:
>
> 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:
>
>    send(:do, *args)

and speaking of 'cases', don't forget 'case'.
82e62c756d89bc6fa0a0a2d7f2b1e617?d=identicon&s=25 Ross Bamford (Guest)
on 2005-12-16 19:04
(Received via mailing list)
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.
>

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
Fe9b2d0628c0943af374b2fe5b320a82?d=identicon&s=25 Eero Saynatkari (rue)
on 2005-12-16 23:17
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
682fff6db11e1a150d6ce17f3b862448?d=identicon&s=25 Doug H (Guest)
on 2005-12-18 20:29
(Received via mailing list)
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
2cf6d8e639314abd751f83a72e9a2ac5?d=identicon&s=25 Martin DeMello (Guest)
on 2005-12-18 23:20
(Received via mailing list)
James Edward Gray II <james@grayproductions.net> wrote:
>
> 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:
>
>    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
82e62c756d89bc6fa0a0a2d7f2b1e617?d=identicon&s=25 Ross Bamford (Guest)
on 2005-12-19 04:33
(Received via mailing list)
On Sun, 18 Dec 2005 18:10:08 -0000, Martin DeMello
<martindemello@yahoo.com> wrote:

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

	 /usr/share/vim/vim63/indent/ruby.vim
	 /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 ;)
This topic is locked and can not be replied to.