ruby indentantion

A

Alfonso

I have just started with ruby, and something that I have observed is
that most of the code is indented with 2 spaces. Having programmed some
time with C# and python, it's very strange to me so few identation. To
me 4 spaces is much better to read. Is there something like a style
guide in ruby that says that you should use 2 spaces or is it all right
using 4 or 3 spaces? What do you use in your code?

Thank you for your answers


______________________________________________
LLama Gratis a cualquier PC del Mundo.
Llamadas a fijos y móviles desde 1 céntimo por minuto.
http://es.voice.yahoo.com
 
R

Robert Spielmann

--=-0FaM8XbQ0qfMm8ZFnp2O
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

Am Mittwoch, den 15.11.2006, 00:39 +0900 schrieb Alfonso:
I have just started with ruby, and something that I have observed is=20
that most of the code is indented with 2 spaces. Having programmed some=20
time with C# and python, it's very strange to me so few identation. To=20
me 4 spaces is much better to read. Is there something like a style=20
guide in ruby that says that you should use 2 spaces or is it all right=20
using 4 or 3 spaces? What do you use in your code?
=20

Hi Alfonso,

indentation with 2 spaces is a non-written standard in Ruby. I use 2
spaces.

Regards,
Robert

--=20
Robert Spielmann

Codecentric GmbH
http://www.codecentric.de


--=-0FaM8XbQ0qfMm8ZFnp2O
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: Dies ist ein digital signierter Nachrichtenteil

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQBFWeO/PekiMvb4uacRAs1lAKCvsvulaBpBtBZio+DGjR1OAwLdPwCgjusL
J21nMne7wSb9ehwmUr6vXAs=
=2Qmw
-----END PGP SIGNATURE-----

--=-0FaM8XbQ0qfMm8ZFnp2O--
 
B

Brad Tilley

Quoting Alfonso said:
I have just started with ruby, and something that I have observed is
that most of the code is indented with 2 spaces. Having programmed some
time with C# and python, it's very strange to me so few identation. To
me 4 spaces is much better to read.

I use 2, 3 and 4 depending on who I'm working with an what I'm working on. I
think readability studies have shown that 4 is the best for most people, but
some companies (like Google I believe) have style guides that require 2.
 
P

Patrick Spence

I'd say it's mostly a personal preference thing. I started using
2-spaces when I started coding in Clipper many years ago.
 
D

dblack

Hi --

I have just started with ruby, and something that I have observed is that
most of the code is indented with 2 spaces. Having programmed some time with
C# and python, it's very strange to me so few identation. To me 4 spaces is
much better to read. Is there something like a style guide in ruby that says
that you should use 2 spaces or is it all right using 4 or 3 spaces? What do
you use in your code?

I use 2 spaces, which is the Ruby tradition and custom. I don't think
there's anything in the standard library that does otherwise.
Whatever language I'm using, I don't like to have my code look like
David Black code, but rather like anonymous, standard, boring code
that uses the most common stylistic conventions for that language. If
you're more into making a statement about your own sense of style, you
can indent more :)


David

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 
T

Trans

Alfonso said:
I have just started with ruby, and something that I have observed is
that most of the code is indented with 2 spaces. Having programmed some
time with C# and python, it's very strange to me so few identation. To
me 4 spaces is much better to read. Is there something like a style
guide in ruby that says that you should use 2 spaces or is it all right
using 4 or 3 spaces? What do you use in your code?

This is just an guestimation but I think Ruby went 2-space because the
most common keyword that takes a block is only three letters long:
'def'. So any more than 2 spaces of indention and you're past the end
of that word, which makes it look a little off kilter.

T.
 
M

Morton Goldberg

I have just started with ruby, and something that I have observed
is that most of the code is indented with 2 spaces. Having
programmed some time with C# and python, it's very strange to me so
few identation. To me 4 spaces is much better to read. Is there
something like a style guide in ruby that says that you should use
2 spaces or is it all right using 4 or 3 spaces? What do you use in
your code?

Use whatever indentation looks right to you, and don't let anyone
bully you into doing it otherwise. I indent by three spaces and don't
intend to change.

Regards, Morton
 
J

James Edward Gray II

Use whatever indentation looks right to you, and don't let anyone
bully you into doing it otherwise. I indent by three spaces and
don't intend to change.

At the risk of being labeled a bully, "When in Rome..." ;)

James I-Use-To-Use-Four-Spaces Gray
 
B

Brad Tilley

Quoting Morton Goldberg said:
Use whatever indentation looks right to you, and don't let anyone
bully you into doing it otherwise. I indent by three spaces and don't
intend to change.

I agree. Use what you like on your personal projects and of course follow your
company's indentation policies when coding at work. I think this is common.
I've heard that Guido Van Rossum (of Python fame) indents 4 spaces for his own
personal programs and 2 spaces when coding for Google.

Best of luck,
Brad
 
D

dblack

Hi --

At the risk of being labeled a bully, "When in Rome..." ;)

That's what it's really about: that there is a traditional coding
style -- a "Rome". I think it's useful for it to be mentioned, not so
that no one ever deviates from it, but so that people who prefer to
use a language's traditional style will know that Ruby has one. That
sometimes gets overlooked amidst the excitement of how liberal the
parser is, etc.

One of my favorite things about Rails, and I think one of the
shrewdest things the Rails team has done, is that it's written for the
most part in a very vanilla coding style. Not all the code is
vanilla, of course :) But it blends very well with the standard
library and so forth in style.


David

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 
D

David Vallner

--------------enig97E9E0E60351E271ED2DC3B6
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
I have just started with ruby, and something that I have observed is
that most of the code is indented with 2 spaces. Having programmed some=
time with C# and python, it's very strange to me so few identation. To
me 4 spaces is much better to read. Is there something like a style
guide in ruby that says that you should use 2 spaces or is it all right=
using 4 or 3 spaces? What do you use in your code?
=20

For another take on the situation:

I'd go out and blame blocks for using only two spaces. Since you tend to
do stuff using a block so often, you end up with more levels of nesting
in a method than in other languages. Non-loop blocks aren't really
nesting, so you don't run into the readability issues with those so
badly, but you'd get a larger chunk of whitespace on the left for code
with the same levels of cyclomatic complexity (I -think- that's the term)=
=2E

And either way, the convention -seems to- be two, so I'd observe that
for publically released code. For personal code, whatever, for work
code, keep it consistent in the team, while remembering that the wiser
man yields.

Weirdly enough, I never found indentation or brace style nearly as
important enough as to warrant trying to ever go against predominant
custom in a language. However, there was one time someone asked me to
stop using IDE shortcuts (even for copy-paste) when typing out some
example code for him, and that's when your code-grinding brain -really-
breaks ;P

David Vallner


--------------enig97E9E0E60351E271ED2DC3B6
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFWktxy6MhrS8astoRAnnGAJ9OhYrW/uukf9Hy/dRGpoY3hqkjkQCcDPYc
pRIllYjZs4qbxTeiz8XyoJY=
=a1SH
-----END PGP SIGNATURE-----

--------------enig97E9E0E60351E271ED2DC3B6--
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: ruby indentantion"

|This is just an guestimation but I think Ruby went 2-space because the
|most common keyword that takes a block is only three letters long:
|'def'. So any more than 2 spaces of indention and you're past the end
|of that word, which makes it look a little off kilter.

The real reason of 2 space indentation is that it is smallest
distinguishable indentation. 1 space is too small for eyes.

matz.
 
M

Morton Goldberg

The real reason of 2 space indentation is that it is smallest
distinguishable indentation. 1 space is too small for eyes.

Some eyes are not as good as others. I have impaired vision. Even two-
space indentation is hard for me, which is why I prefer three-space
indentation.

Regards, Morton
 
T

Trans

Yukihiro said:
Hi,

In message "Re: ruby indentantion"

|This is just an guestimation but I think Ruby went 2-space because the
|most common keyword that takes a block is only three letters long:
|'def'. So any more than 2 spaces of indention and you're past the end
|of that word, which makes it look a little off kilter.

The real reason of 2 space indentation is that it is smallest
distinguishable indentation. 1 space is too small for eyes.

And a smaller memory footprint to go with it (since tabs are
inceasingly shunned these days and for good reason).

T.
 
S

Sebastian Reid

Which good reason is this? Are they dramatically more memory intensive?

It seems to me that tabs would make more sense, since they would
allow the reader to set their chosen length of indentation in the
editor. That way it's two spaces to you, four to someone else, with
it defaulting to the language custom.

In any case hitting tab is far too much of a habit to break, whether
that uses real tabs or fills in a number of actual spaces. What if
you were trying to line up to a four-level nested line on a 4-space
indented language? Are you going to hit space 16 times?
 
M

Matthew Smillie

Which good reason is this? Are they dramatically more memory
intensive?

They're dramatically good at breaking formatting, is what they are.
It seems to me that tabs would make more sense, since they would
allow the reader to set their chosen length of indentation in the
editor. That way it's two spaces to you, four to someone else,
with it defaulting to the language custom.

If people could be relied upon to set their tabs correctly, you'd be
right. In reality, there's always someone who wants two-space
indentation, but has four-space tabs, which gives this horrible
mixture of tabs and spaces. What would one tab + two spaces look
like if you opened the file without knowing your co-workers preference?

Of course, it's usually not all that bad to correct, but since using
spaces doesn't allow for the same kind of error, why waste even that
small amount of time with the dreaded \t?
In any case hitting tab is far too much of a habit to break,
whether that uses real tabs or fills in a number of actual spaces.
What if you were trying to line up to a four-level nested line on a
4-space indented language? Are you going to hit space 16 times?

No, I'm going to hit 'tab' once and let emacs figure out how many
spaces to insert. I'm pretty sure any useful editor would have a
similar method, and if your favourite editor doesn't at least have a
'use spaces for tabs' option that lets you use the tab key to insert
spaces up to the next tab stop, well, you should probably pick a new
favourite.

m.s.
 
J

Josef 'Jupp' Schugt

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

* Alfonso, 11/14/2006 04:39 PM:
I have just started with ruby, and something that I have observed
is that most of the code is indented with 2 spaces. Having
programmed some time with C# and python, it's very strange to me
so few identation. To me 4 spaces is much better to read. Is there
something like a style guide in ruby that says that you should use
2 spaces or is it all right using 4 or 3 spaces? What do you use
in your code?

The number of spaces for indentation depend on at least four factors:

- - the length of an average token

- - wether the token may contain spaces (this is a rare feature but
some languages actually allow this)

- - the kind of font used to display the code (it can have a narrow,
square or wide shape which means aspect ratios significantly less
than, approximately equal to, or significantly larger than one).

- - your perception

In other words: Use the indentation that seems appropriate. I'd only
suggest to stick to a certain indentation to avoid inconsistencies
when merging code. Transforming one indentation into another one is a
Ruby one-liner.

Jupp
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iD8DBQFFWvGTrhv7B2zGV08RAnphAKC8RjwVBiSes5HmJJbTl/2QEhQgBACghGN7
N7cBFCYUEL9f5EuPUoAZfpk=
=QoEX
-----END PGP SIGNATURE-----
 
A

Alder Green

I have just started with ruby, and something that I have observed is
that most of the code is indented with 2 spaces. Having programmed some
time with C# and python, it's very strange to me so few identation.

I'm working simultaneously on several Ruby and Python projects.

For Ruby I'm using 2 spaces, for Python I use 4 spaces, as those are
the conventions for each language.

I've never noticed any problems for me or people who work with me.

In Python I need to remember that argument-less methods must have a
terminating (), and many statements must have a colon; compared to
those and many other differences, the different indentation is
trivial, not to mention that both Emacs and Vim just do the right
thing automatically based on the file-type, so you don't even have to
remember anything...

The important thing is that the code you produce is readable by the
community of language users.

-Alder
 
J

James Edward Gray II

It seems to me that tabs would make more sense, since they would
allow the reader to set their chosen length of indentation in the
editor.

Using spaces is the only way to guarantee they see the same content
you do.

James Edward Gray II
 

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
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top