ruby indentantion

Discussion in 'Ruby' started by Alfonso, Nov 14, 2006.

  1. Alfonso

    Alfonso Guest

    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
     
    Alfonso, Nov 14, 2006
    #1
    1. Advertising

  2. --=-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--
     
    Robert Spielmann, Nov 14, 2006
    #2
    1. Advertising

  3. Alfonso

    Brad Tilley Guest

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


    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.
     
    Brad Tilley, Nov 14, 2006
    #3
  4. I'd say it's mostly a personal preference thing. I started using
    2-spaces when I started coding in Clipper many years ago.
     
    Patrick Spence, Nov 14, 2006
    #4
  5. Alfonso

    Guest

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


    David

    --
    David A. Black |
    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
     
    , Nov 14, 2006
    #5
  6. Alfonso

    Trans Guest

    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.
     
    Trans, Nov 14, 2006
    #6
  7. 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
     
    Morton Goldberg, Nov 14, 2006
    #7
  8. 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.


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

    James I-Use-To-Use-Four-Spaces Gray
     
    James Edward Gray II, Nov 14, 2006
    #8
  9. Alfonso

    Brad Tilley Guest

    Quoting Morton Goldberg <>:

    > 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
     
    Brad Tilley, Nov 14, 2006
    #9
  10. Alfonso

    Guest

    Hi --

    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:
    >>
    >>> 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..." ;)


    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 |
    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
     
    , Nov 14, 2006
    #10
  11. --------------enig97E9E0E60351E271ED2DC3B6
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable

    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?
    >=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--
     
    David Vallner, Nov 14, 2006
    #11
  12. Hi,

    In message "Re: ruby indentantion"
    on Wed, 15 Nov 2006 01:30:05 +0900, "Trans" <> 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.
     
    Yukihiro Matsumoto, Nov 14, 2006
    #12
  13. 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
     
    Morton Goldberg, Nov 15, 2006
    #13
  14. Alfonso

    Trans Guest

    Yukihiro Matsumoto wrote:
    > Hi,
    >
    > In message "Re: ruby indentantion"
    > on Wed, 15 Nov 2006 01:30:05 +0900, "Trans" <> 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.
     
    Trans, Nov 15, 2006
    #14
  15. 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"
    >> <> 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.
    >
    >
    >
     
    Sebastian Reid, Nov 15, 2006
    #15
  16. 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 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"
    >>> <> 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.
    >>
    >>
    >>

    >
    >
     
    Matthew Smillie, Nov 15, 2006
    #16
  17. Alfonso

    zimbatm Guest

    I'm lazy and don't like tabs so I use 2 space chars :)
     
    zimbatm, Nov 15, 2006
    #17
  18. -----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-----
     
    Josef 'Jupp' Schugt, Nov 15, 2006
    #18
  19. Alfonso

    Alder Green Guest

    On 11/14/06, 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.


    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
     
    Alder Green, Nov 15, 2006
    #19
  20. On Nov 15, 2006, at 1:47 AM, Sebastian Reid wrote:

    > 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
     
    James Edward Gray II, Nov 15, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    235
  2. anne001
    Replies:
    1
    Views:
    584
  3. Phrogz
    Replies:
    4
    Views:
    260
    Austin Ziegler
    Sep 6, 2006
  4. roschler
    Replies:
    0
    Views:
    195
    roschler
    Oct 16, 2006
  5. Nicholas
    Replies:
    3
    Views:
    408
    Ryan Davis
    Jan 28, 2007
Loading...

Share This Page