Ruby indentantion

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

Am Mittwoch, den 15.11.2006, 00:39 +0900 schrieb 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?

Hi Alfonso,

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

Regards,
Robert


Robert S.

Codecentric GmbH

Quoting A. [email protected]:

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.

Hi –

On Wed, 15 Nov 2006, Alfonso wrote:

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 :slight_smile:

David

To Robert: This sort of things should be casual, it’s just habit. You
might
get used to either 2 spaces or 4 spaces.

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

On Nov 14, 2006, at 10:39 AM, Alfonso wrote:

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

Alfonso wrote:

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.

On Nov 14, 2006, at 1:13 PM, Morton G. wrote:

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…” :wink:

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

Quoting Morton G. [email protected]:

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

Hi –

On Wed, 15 Nov 2006, James Edward G. II wrote:

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…” :wink:

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 :slight_smile: But it blends very well with the standard
library and so forth in style.

David

Hi,

In message “Re: ruby indentantion”
on Wed, 15 Nov 2006 01:30:05 +0900, “Trans” [email protected]
writes:

|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.

Alfonso wrote:

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?

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).

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 :stuck_out_tongue_winking_eye:

David V.

Yukihiro M. wrote:

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.

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?

On Nov 15, 2006, at 7:47, Sebastian Reid wrote:

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.

On Nov 14, 2006, at 6:34 PM, Yukihiro M. wrote:

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

I’m lazy and don’t like tabs so I use 2 space chars :slight_smile:

Sebastian Reid wrote:

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.

Over time, for better or worse, tabs have come to mean eight spaces in
fixed-point font displays, with little flexibility about it. And spaces
have continued to mean the tautology of one character space. So people
have
come around to using blocks of multiple spaces as though they were
adjustable tabs.

The drawbacks are obvious. The advantage is that, if you use spaces,
your
intentions will be carried across into many more environments than if
you
use tabs in any non-standard way. And eight spaces is just too much.

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.

Well, you can always swap the tabs for a chosen number of spaces at the
end
of your development work, before publishing your code, just to get along
with the rest of us. :slight_smile:

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, you use a beautifier to do it for you. Also, must programming
editors
repeat the indent of the prior line when you hit Enter, so you don’t
often
have to space or tab out to the required indentation.

On 11/14/06, Alfonso [email protected] wrote:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs