indent for Ruby, to reformat it

P

Phlip

Rubies:

I'm writing a Ruby script and I'm too lazy to type the indentations and
linefeeds correctly.

I started on this jag assuming that an indent for Ruby would only be a
Google away.

I can't find one. Has anyone written a little script that even begins to
reformat Ruby into neat blocks?

(Don't flame; I promise not to need it again!)
 
J

Jacob Quinn Shenker

VW0uLi4uLiBWaW0hISEgOy0pCgpPbiAxMC85LzA1LCBQaGxpcCA8cGhsaXBjcHBAeWFob28uY29t
PiB3cm90ZToKPiBSdWJpZXM6Cj4KPiBJJ20gd3JpdGluZyBhIFJ1Ynkgc2NyaXB0IGFuZCBJJ20g
dG9vIGxhenkgdG8gdHlwZSB0aGUgaW5kZW50YXRpb25zIGFuZAo+IGxpbmVmZWVkcyBjb3JyZWN0
bHkuCj4KPiBJIHN0YXJ0ZWQgb24gdGhpcyBqYWcgYXNzdW1pbmcgdGhhdCBhbiBpbmRlbnQgZm9y
IFJ1Ynkgd291bGQgb25seSBiZSBhCj4gR29vZ2xlIGF3YXkuCj4KPiBJIGNhbid0IGZpbmQgb25l
LiBIYXMgYW55b25lIHdyaXR0ZW4gYSBsaXR0bGUgc2NyaXB0IHRoYXQgZXZlbiBiZWdpbnMgdG8K
PiByZWZvcm1hdCBSdWJ5IGludG8gbmVhdCBibG9ja3M/Cj4KPiAoRG9uJ3QgZmxhbWU7IEkgcHJv
bWlzZSBub3QgdG8gbmVlZCBpdCBhZ2FpbiEpCj4KPiAtLQo+ICAgUGhsaXAKPiAgIGh0dHA6Ly93
d3cuZ3JlZW5jaGVlc2Uub3JnL1plZWtMYW5kICA8LS0gTk9UIGEgYmxvZyEhIQo+Cj4KPgo+Cg==
 
J

Jacob Quinn Shenker

T2shIEp1c3QgcG9wIGl0IG9wZW4sIHRoZW4gdHlwZSAnRz1nZycuIFNlZSBtb3JlIFZpbSBpbmRl
bnRpbmcgdHJpY2tzIGF0Cmh0dHA6Ly93d3cudmltLm9yZy90aXBzL3RpcC5waHA/dGlwX2lkPTgz
CgpKYWNvYgoKT24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gSSBsZXQgcnVieS1tb2RlLmVsIGRvIGl0Cj4KPiA+IFRpbQo+Cj4gU29ycnkgLSBF
bWFjcyBpcyB0aGUgZGV2aWwgSSAvZG9uJ3QvIGtub3cuIDstKQo+Cj4gLS0KPiAgIFBobGlwCj4g
ICBodHRwOi8vd3d3LmdyZWVuY2hlZXNlLm9yZy9aZWVrTGFuZCAgPC0tIE5PVCBhIGJsb2chISEK
Pgo+Cj4KPgo=
 
J

James Britt

P

Phlip

Jacob said:
Vim's Ruby indentation has actually worked very well for me, although
the syntax coloring gets confused at times. And yes, Vim detects the
.rb and indented accordingly.

It seems to have identified SH scripts. It broke everything up at bizarre
intervals, screwed up the regular expressions, and turned => into = >.

(Fresh Fedora with the vim-common rpm installed, and Ruby installed from its
tar.gz file...)

I just did it by hand.
 
K

Kirk Haines

Ok! Just pop it open, then type 'G=gg'. See more Vim indenting tricks at
http://www.vim.org/tips/tip.php?tip_id=83

I use vim a lot, but have never asked it to indent my code for me. And
judging by the results that I just got, I won't do so again any time soon.
At least on my machine, with what is, admittedly, probably old and outdated
Ruby support, the results are awful.


Kirk Haines
 
D

David Vallner

Hack up three lines of Emacs Lisp to get the Emacs ruby-mode to do the
indentation for you? I usually get good results with XEmacs indenting my
code for me, although I don't have a quick and dirty way to do it via
the command line at hand.

David Vallner
 
T

Tim Heaney

David Vallner said:
Hack up three lines of Emacs Lisp to get the Emacs ruby-mode to do the
indentation for you? I usually get good results with XEmacs indenting my
code for me, although I don't have a quick and dirty way to do it via
the command line at hand.

Yes, that's what I meant.

xemacs -batch foo.rb -eval '(indent-region (point-min) (point-max) nil)' -f save-buffer -kill

Tim
 
G

Gavin Sinclair

Kirk said:
I use vim a lot, but have never asked it to indent my code for me. And
judging by the results that I just got, I won't do so again any time soon.
At least on my machine, with what is, admittedly, probably old and outdated
Ruby support, the results are awful.

With a modern setup, bad results are an exception rather than a rule.

Gavin
 
J

Jacob Quinn Shenker

SnVzdCBmeWksIHRvIHVwZGF0ZSB5b3VyIHZpbS1ydWJ5IHN0dWZmLCBkbzoKJCBnZW0gaW5zdGFs
bCB2aW0tcnVieQokIHZpbS1ydWJ5LWluc3RhbGwucmIgKHBvaW50IGl0IGF0IHlvdXIgdmltIGlu
c3RhbGwgcGF0aCBpZiB5b3UgZGlkbid0IHNldCAkVklNKQoKT24gMTAvOS8wNSwgR2F2aW4gU2lu
Y2xhaXIgPGdzaW5jbGFpckBnbWFpbC5jb20+IHdyb3RlOgo+IEtpcmsgSGFpbmVzIHdyb3RlOgo+
ID4gSSB1c2UgdmltIGEgbG90LCBidXQgaGF2ZSBuZXZlciBhc2tlZCBpdCB0byBpbmRlbnQgbXkg
Y29kZSBmb3IgbWUuICBBbmQKPiA+IGp1ZGdpbmcgYnkgdGhlIHJlc3VsdHMgdGhhdCBJIGp1c3Qg
Z290LCBJIHdvbid0IGRvIHNvIGFnYWluIGFueSB0aW1lIHNvb24uCj4gPiBBdCBsZWFzdCBvbiBt
eSBtYWNoaW5lLCB3aXRoIHdoYXQgaXMsIGFkbWl0dGVkbHksIHByb2JhYmx5IG9sZCBhbmQgb3V0
ZGF0ZWQKPiA+IFJ1Ynkgc3VwcG9ydCwgdGhlIHJlc3VsdHMgYXJlIGF3ZnVsLgo+Cj4gV2l0aCBh
IG1vZGVybiBzZXR1cCwgYmFkIHJlc3VsdHMgYXJlIGFuIGV4Y2VwdGlvbiByYXRoZXIgdGhhbiBh
IHJ1bGUuCj4KPiBHYXZpbgo+Cj4KPgo=
 
K

Kirk Haines

With a modern setup, bad results are an exception rather than a rule.

The machine(s) are hardly ancient. One is a Gentoo box that has not had any
vim related stuff updated for about 6 months, and the other is an RHEL3 box.
Hardly non-modern. Just not cutting edge. The results are worse than bad
when I ask vim to indent the same code on either.


Kirk Haines
 
K

Kirk Haines

With a modern setup, bad results are an exception rather than a rule.

Okay. After spending some time with it, here's the secret:

Download a recent version of vim-ruby from Rubyforge:

http://rubyforge.org/frs/?group_id=16&release_id=3083

Follow the instructions found there or on the vim-ruby page:

http://vim-ruby.rubyforge.org

to install, and then make sure your .vimrc is setup to make use of what you
just installed.

The last bit is important. Otherwise, you get really nasty looking
indentation.

After having followed those steps, the indentation looks pretty good. I've
tested it on half a dozen files of varying lengths and code complexities, and
it seems to do what it should.

Kirk Haines
 
M

Martin DeMello

Kirk Haines said:
Follow the instructions found there or on the vim-ruby page:

http://vim-ruby.rubyforge.org

to install, and then make sure your .vimrc is setup to make use of what you
just installed.

The last bit is important. Otherwise, you get really nasty looking
indentation.

To be explicit, you need at least:

set nocompatible
syntax on
filetype on
filetype indent on
filetype plugin on

martin
 
D

Doug Kearns

Vim's Ruby indentation has actually worked very well for me, although
the syntax coloring gets confused at times.

If you report them as bugs someone might try and fix them. ;-)

<snip>

Regards,
Doug
 
J

Jacob Quinn Shenker

V2VsbCwgSSBqdXN0IHVwZGF0ZWQgYW5kIG1vc3QgaWYgbm90IGFsbCB3ZW50IGF3YXkhIEkgYW0g
YSByZWFsbHkKaGFwcHkgY2FtcGVyIG5vdy4gQnV0ISEhLi4uIGlmIEkgZG8gZmluZCBhbnkgKnJl
YWwqIGJ1Z3MgKGkuZS4gbm90CmZyb20gMiB5cnMgYWdvKSBJJ2xsIGxldCBzb21lb25lIGtub3cg
Zm9yIHN1cmUgOy0pLgoKSmFjb2IKCk9uIDEwLzEwLzA1LCBEb3VnIEtlYXJucyA8ZG91Z2tlYXJu
c0BnbWFpbC5jb20+IHdyb3RlOgo+IE9uIE1vbiwgT2N0IDEwLCAyMDA1IGF0IDAyOjIwOjQ2QU0g
KzA5MDAsIEphY29iIFF1aW5uIFNoZW5rZXIgd3JvdGU6Cj4gPiBWaW0ncyBSdWJ5IGluZGVudGF0
aW9uIGhhcyBhY3R1YWxseSB3b3JrZWQgdmVyeSB3ZWxsIGZvciBtZSwgYWx0aG91Z2gKPiA+IHRo
ZSBzeW50YXggY29sb3JpbmcgZ2V0cyBjb25mdXNlZCBhdCB0aW1lcy4KPgo+IElmIHlvdSByZXBv
cnQgdGhlbSBhcyBidWdzIHNvbWVvbmUgbWlnaHQgdHJ5IGFuZCBmaXggdGhlbS4gOy0pCj4KPiA8
c25pcD4KPgo+IFJlZ2FyZHMsCj4gRG91Zwo+Cj4K
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,235
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top