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
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
pts
attr :b
def initialize(*opts)
@opts = opts.hashify
b = B.new opts, :key => 'overridden'
end
end
class B
attr
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'
===============================================================================