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

Hi Alfonso,

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

Regards,
Robert

···

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?

--
Robert Spielmann

Codecentric GmbH

Quoting Alfonso <euoar@yahoo.es>:

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.

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

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

David

···

On Wed, 15 Nov 2006, Alfonso wrote:

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

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.

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

···

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?

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 Vallner

* 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

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

···

On 11/14/06, Alfonso <euoar@yahoo.es> 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 Robert: This sort of things should be casual, it's just habit. You might
get used to either 2 spaces or 4 spaces.

···

On 11/14/06, Robert Spielmann <spielmann@codecentric.de> wrote:

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 Spielmann

Codecentric GmbH
http://www.codecentric.de

At the risk of being labeled a bully, "When in Rome..." :wink:

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

···

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

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.

Quoting Morton Goldberg <m_goldberg@ameritech.net>:

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,

···

In message "Re: ruby indentantion" on Wed, 15 Nov 2006 01:30:05 +0900, "Trans" <transfire@gmail.com> 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.

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?

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

···

On Wed, 15 Nov 2006, James Edward Gray II wrote:

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

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

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

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

···

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

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

Yukihiro Matsumoto wrote:

Hi,

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

···

In message "Re: ruby indentantion" > on Wed, 15 Nov 2006 01:30:05 +0900, "Trans" <transfire@gmail.com> writes:

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 15 Nov 2006, at 02:50, Trans wrote:

Yukihiro Matsumoto wrote:

Hi,

In message "Re: ruby indentantion" >> on Wed, 15 Nov 2006 01:30:05 +0900, "Trans" >> <transfire@gmail.com> 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.

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

T.

Morton Goldberg escribio':

···

On Nov 14, 2006, at 6:34 PM, Yukihiro Matsumoto 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

Even while I don't have any problem with my eyes, 2 spaces is also hard to me. Also, I think I'm going to stay with 3 spaces (a compromiss between 2 and 4), at least while I don't get used to the 2 spaces...

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

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 15, 2006, at 7:47, Sebastian Reid wrote:

On 15 Nov 2006, at 02:50, Trans wrote:

Yukihiro Matsumoto wrote:

Hi,

In message "Re: ruby indentantion" >>> on Wed, 15 Nov 2006 01:30:05 +0900, "Trans" >>> <transfire@gmail.com> 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.

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

T.