recommended way to indent

S

Simon Strandgaard

At several occasions I wonder how to best indent Ruby code.
I am curious to how _you_ indent Ruby code ?

For instance converting Array into variables, with
many/long variables it can get unreadable.

( node,
@input,
@parent_nodes,
@registers ) = @resume_stack.pop


The opposite push operation could be indented like this:

@resume_stack.push([
node,
input,
@parent_nodes.map_clone,
@registers.clone
])


Classes which has to be initialized with a bunch of
arguments:

def initialize(
number_of_registers,
input,
integrity_iterator=nil)
 
G

Gavin Sinclair

At several occasions I wonder how to best indent Ruby code.
I am curious to how _you_ indent Ruby code ?
For instance converting Array into variables, with
many/long variables it can get unreadable.
( node,
@input,
@parent_nodes,
@registers ) = @resume_stack.pop

I would tend to encapsulate the four values in an object rather than
splatter assignments all over the place.
Classes which has to be initialized with a bunch of
arguments:
def initialize(
number_of_registers,
input,
integrity_iterator=nil)

def initialize(nregisters, input, integrity_iterator=nil)

That's not too long :) Any longer and I'd consider an alternative
strategy.

Regarding your actual indenting, though, I would do it the same way.

Gavin
 
A

Ara.T.Howard

Date: Mon, 29 Dec 2003 11:09:16 +0100
From: Simon Strandgaard <[email protected]>
Newsgroups: comp.lang.ruby
Subject: recommended way to indent

At several occasions I wonder how to best indent Ruby code.
I am curious to how _you_ indent Ruby code ?

For instance converting Array into variables, with
many/long variables it can get unreadable.

( node,
@input,
@parent_nodes,
@registers ) = @resume_stack.pop


i typically do

node, input, parent_nodes, registers =
resume_stack.pop

note the lack of '@', i try hard to never use it outside of initialize and
instead use

attr :node
attr :input

etc. to avoid it. not that fastest - but easier to read
The opposite push operation could be indented like this:

@resume_stack.push([
node,
input,
@parent_nodes.map_clone,
@registers.clone
])

resume_stack.push node,
input,
parent_nodes.map_clone,
registers.clone

or, if args too long/too indended:

args =
node,
input,
parent_nodes.map_clone,
registers.clone

resume_stack.push *args


Classes which has to be initialized with a bunch of
arguments:

def initialize(
number_of_registers,
input,
integrity_iterator=nil)

i do this a _lot_ for initializers with lots of args:

class Array
def hashify
inject({}){|h,o| h.update o}
end
end


class C
attr :eek:pts
attr :foo
attr :bar

def initialize(*args)
@opts = args.hashify
@foo = opts[:foo]
@bar = opts[:bar]
end
end

opts = Hash[
:foo => 42,
:bar => 42.0,
]

c = C.new opts
c = C.new opts, :foo => 'note that foo is overridden here!!!'

though i do _not_ always put #hashify in Array, sometimes it is a class or
instance method...

this is really useful when you want to pass many of the args to initialize to
subsequent calls (deep class structure), since you can then simply:

class C
attr :eek:pts
attr :b

def initialize(*opts)
@opts = opts.hashify
b = B.new opts, :key => 'overridden'
end
end

class B
attr :eek:pts
def initialize(*opts)
@opts = opts.hashify
...
end
end

this comes in _really_ handy when you decide, after much developent, that you
need a new var deep in a series of calls - with this method, you simply add it
to the top level. most of all i like how long initializers tend to read
though:

c = C.new :label => 'value',
:width => 42,
:height => 42.0,
:border => 'raised',
:ipad => 2.0,
:color => 0x00ff00,

if you get the 'Align' macro for gvim lining up the '=>' is super easy too...

etc.
etc.


-a
--

ATTN: please update your address books with address below!

===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| STP :: http://www.ngdc.noaa.gov/stp/
| NGDC :: http://www.ngdc.noaa.gov/
| NESDIS :: http://www.nesdis.noaa.gov/
| NOAA :: http://www.noaa.gov/
| US DOC :: http://www.commerce.gov/
|
| The difference between art and science is that science is what we
| understand well enough to explain to a computer.
| Art is everything else.
| -- Donald Knuth, "Discover"
|
| /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done'
===============================================================================
 
J

Josef 'Jupp' SCHUGT

Hi!

* Simon Strandgaard:
At several occasions I wonder how to best indent Ruby code.
I am curious to how _you_ indent Ruby code ?

( node,
@input,
@parent_nodes,
@registers ) = @resume_stack.pop

(
node,
@input,
@parent_nodes,
@registers
) = @resume_stack.pop

This calls for a font that makes it easy to distinguish between
ordinary parentheses and curly braces. I sometimes indent the
assignment but not always. Depends on how obvious the RHS is.
@resume_stack.push([
node,
input,
@parent_nodes.map_clone,
@registers.clone
])

I sometimes do it in the same way but I also use

@resume_stack.push(
[
node,
input,
@parent_nodes.map_clone,
@registers.clone,
]
)

depending on how important it is that the data is an array.

Please note the colon after @registers.clone - I did add it on
purpose...
def initialize(
number_of_registers,
input,
integrity_iterator=nil)

usually:

def initialize( number_of_registers,
input,
integrity_iterator=nil
)

Space. The final frontier. These are the adventures of the U.S.S.
Ruby that boldly indents what noone has indented before >;->

Josef 'Jupp' SCHUGT
 

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

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top