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

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)

···


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

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 :slight_smile: Any longer and I’d consider an alternative
strategy.

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

Gavin

···

On Monday, December 29, 2003, 9:16:48 PM, Simon wrote:

Date: Mon, 29 Dec 2003 11:09:16 +0100
From: Simon Strandgaard neoneye@adslhome.dk
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 :opts
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 :opts
attr :b

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

end

class B
attr :opts
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

···

On Mon, 29 Dec 2003, Simon Strandgaard wrote:

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 :: Solar-Terrestrial Physics Data | NCEI
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’
===============================================================================

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

···


http://oss.erdfunkstelle.de/ruby/ - German comp.lang.ruby-FAQ
http://rubyforge.org/users/jupp/ - Ruby projects at Rubyforge