Emacs Ruby mode & indentation with the tab key wish/bug

Discussion in 'Ruby' started by Moritz Bunkus, Jun 17, 2004.

  1. Heya,

    I have a wish for the indentation with the tab key in the Ruby mode. I'm
    currently using

    (defconst ruby-mode-revision "$Revision: 1.74.2.1 $")

    from Debian/unstable's ruby1.8-elisp package. So here's where (IMHO !) the
    indentation with the tab key is not working properly:

    1) Continued statements should be indented with only ruby-indent-level
    spaces. At the moment it produces something like this:

    myvar = 2 + 3 +
    4 + 1

    I'd like to have it behave more like

    myvar = 2 + 3 +
    4 + 1

    assuming that ruby-indent-leve is 2. This might be desputable, so maybe it
    should be an option.

    2) Indentation with { } blocks is wrong. It does work nicely with e.g.
    upto:

    0.upto(2) { |i|
    print("i is #{i}\n")
    }

    but not with "if...":

    if (i == 2) {
    asdasd
    }


    After this passage tab indents to column 3, so this case is definitely
    broken. Replacing { and } with begin/then and end fixes this, of course,
    but I prefer { }.

    Thanks for considering it.

    Mosu

    --
    If Darl McBride was in charge, he'd probably make marriage
    unconstitutional too, since clearly it de-emphasizes the commercial
    nature of normal human interaction, and probably is a major impediment
    to the commercial growth of prostitution. - Linus Torvalds
     
    Moritz Bunkus, Jun 17, 2004
    #1
    1. Advertising

  2. Moritz Bunkus

    alex fenton Guest

    Hi

    Moritz Bunkus wrote:
    > 1) Continued statements should be indented with only ruby-indent-level
    > spaces. At the moment it produces something like this:
    >
    > myvar = 2 + 3 +
    > 4 + 1
    >
    > I'd like to have it behave more like
    >
    > myvar = 2 + 3 +
    > 4 + 1


    I think there's an already option for what you want, I think; the
    option's called ruby-deep-arglist, which you'll want to switch OFF to
    use your preferred style. Try the following in your .emacs

    (add-hook 'ruby-mode-hook
    '(lambda ()
    (custom-set-variables
    '(ruby-deep-arglist 0))
    ))



    > 2) Indentation with { } blocks is wrong. It does work nicely with e.g.
    > upto:


    ....

    > but not with "if...":
    >
    > if (i == 2) {
    > asdasd
    > }
    >
    >
    > After this passage tab indents to column 3, so this case is definitely
    > broken.


    Um, the case is broken, but I don't think it's the syntax highlighter
    :). You don't enclose conditional blocks in { } in Ruby - the 'if' (or
    'unless' or 'elsif' or 'else') marks the beginning of the block itself,
    and you must use 'end' to mark the end of the block. You also don't
    usually need parens round the conditional statement, unlike Perl. So:

    if i == 2
    puts "foo"
    end

    Aah ... isn't that lovely and readable?

    Cheers
    alex
     
    alex fenton, Jun 17, 2004
    #2
    1. Advertising

  3. --/9DWx/yDrRhgMJTb
    Content-Type: text/plain; charset=us-ascii
    Content-Disposition: inline
    Content-Transfer-Encoding: quoted-printable

    Hi,

    > I think there's an already option for what you want, I think; the=20
    > option's called ruby-deep-arglist, which you'll want to switch OFF to=20
    > use your preferred style.


    Doesn't help. In fact I don't see a difference with these three cases
    whatever I set that variable to:

    mylist =3D ["1", "2",
    "3", "4"]

    if (("a" =3D=3D "b") and
    ("c" =3D=3D "d")) then
    end

    myvar =3D 3 + 4 +
    1 + 5

    So I guess that either it's not working correctly or it's not supposed
    to change these cases in which case I'd still wish for it to be
    implemented :)

    > Um, the case is broken, but I don't think it's the syntax highlighter=20
    > :). You don't enclose conditional blocks in { } in Ruby


    Ok. I'm obviously a Ruby n00b ;) Anyway, it's kinda confusing to be able
    to use { } for blocks in one case but not the other. What's the reason
    behind that?

    > You also don't usually need parens round the conditional statement,
    > unlike Perl.


    That's just a matter of style / preference. I'll probably continue using
    them 'cause I'd like to keep my programming style at least partially
    equal no matter if I code in C/C++, Perl or Ruby.

    > if i =3D=3D 2
    > puts "foo"
    > end
    >=20
    > Aah ... isn't that lovely and readable?


    Sure it is. It's just nice having Emacs highlight the brace the current
    one corresponds to. A kind of 'visual theres-the-start-of-this-block
    marker'.

    Anyway, thanks for the help.

    Mosu

    --=20
    If Darl McBride was in charge, he'd probably make marriage
    unconstitutional too, since clearly it de-emphasizes the commercial
    nature of normal human interaction, and probably is a major impediment
    to the commercial growth of prostitution. - Linus Torvalds

    --/9DWx/yDrRhgMJTb
    Content-Type: application/pgp-signature; name="signature.asc"
    Content-Description: Digital signature
    Content-Disposition: inline

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

    iD8DBQFA0X3qTJ9OsraNuaERAqvhAJ4puD68GAzshvPOh9Uw++gloRJobgCglALo
    IdvoyDuqRg9SCJH/+Wyhsek=
    =Vou+
    -----END PGP SIGNATURE-----

    --/9DWx/yDrRhgMJTb--
     
    Moritz Bunkus, Jun 17, 2004
    #3
  4. Moritz Bunkus

    alex fenton Guest

    Moritz Bunkus wrote:

    >>I think there's an already option for what you want, I think; the
    >>option's called ruby-deep-arglist, which you'll want to switch OFF to
    >>use your preferred style.

    >
    > Doesn't help. In fact I don't see a difference with these three cases
    > whatever I set that variable to:
    >
    > mylist = ["1", "2",
    > "3", "4"]
    >
    > if (("a" == "b") and
    > ("c" == "d")) then
    > end
    >
    > myvar = 3 + 4 +
    > 1 + 5


    Sorry, my mistake. The variable you want is ruby-deep-indent-paren. Set
    it to false to get the style you're looking for. That definitely works
    for me on your first and third examples.

    >>Um, the case is broken, but I don't think it's the syntax highlighter
    >>:). You don't enclose conditional blocks in { } in Ruby

    >
    >
    > Ok. I'm obviously a Ruby n00b ;) Anyway, it's kinda confusing to be able
    > to use { } for blocks in one case but not the other. What's the reason
    > behind that?


    Someone else will be able to give you a better explanation than me, but
    as I understand it:

    1) There's "blocks" of code that start with keywords like "begin", "if",
    "else", "rescue" that are just that - code that is executed on the given
    condition / exception / whatever. This is similar to blocks as I know
    'em in Perl.

    2) Then, there are ruby blocks marked with 'do .. end' or { .. } that
    are actually arguments to methods called on objects. This is a
    distinctive and delightful feature of Ruby. A classic method that
    receives a ruby block is Array#each, which calls the associated block
    for each member of the array successively. You can write your own
    methods that receive blocks, either by using 'yield' or by receiving
    them as the last argument in the method definition.

    class Array
    def each_in_reverse
    reverse.each { | item | yield item }
    end
    end

    a = [ 1, 2, 3 ]
    a.each_in_reverse { | x | p x * 3 } # 9, 6, 3

    This is a pretty inane example, but hopefully gives you an idea. Take a
    look at the chapter 'Containers, Blocks and Iterators', in the Pragmatic
    Programmers' "Programming Ruby" book for a good discussion.

    The choice between do .. end and { ..} is largely a matter of taste.
    There is a difference in precedence, and a very common idiom is to
    prefer do .. end for multi-line blocks and { .. } for single-line ones.

    >>You also don't usually need parens round the conditional statement,
    >>unlike Perl.

    >
    > That's just a matter of style / preference.


    Not in Perl it isn't :)

    > I'll probably continue using
    > them 'cause I'd like to keep my programming style at least partially
    > equal no matter if I code in C/C++, Perl or Ruby.


    That's fine - I'm sure no-one from c.l.r. will be coming round your
    house late at night to check :)

    > It's just nice having Emacs highlight the brace the current
    > one corresponds to. A kind of 'visual theres-the-start-of-this-block
    > marker'.


    Sure. I tend to rely more on the indentation to do that for me, but YMMV.

    Happy rubying - cheers
    alex
     
    alex fenton, Jun 17, 2004
    #4
  5. --U+BazGySraz5kW0T
    Content-Type: text/plain; charset=us-ascii
    Content-Disposition: inline
    Content-Transfer-Encoding: quoted-printable

    Hi,

    > Sorry, my mistake. The variable you want is ruby-deep-indent-paren. Set=

    =20
    > it to false to get the style you're looking for. That definitely works=20
    > for me on your first and third examples.


    Works like a charme here, too. Thanks :)

    > Someone else will be able to give you a better explanation than me, but=

    =20
    > as I understand it:


    =2E..

    Ok that makes sense. Thanks for the explanation & the examples.

    > >That's just a matter of style / preference.=20

    >=20
    > Not in Perl it isn't :)


    Of course not, I meant Ruby ;)

    > That's fine - I'm sure no-one from c.l.r. will be coming round your=20
    > house late at night to check :)


    Not!? Damn! All the money I've already spent on my security
    measurements... totally wasted...

    Mosu

    --=20
    If Darl McBride was in charge, he'd probably make marriage
    unconstitutional too, since clearly it de-emphasizes the commercial
    nature of normal human interaction, and probably is a major impediment
    to the commercial growth of prostitution. - Linus Torvalds

    --U+BazGySraz5kW0T
    Content-Type: application/pgp-signature; name="signature.asc"
    Content-Description: Digital signature
    Content-Disposition: inline

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

    iD8DBQFA0aaoTJ9OsraNuaERAkvGAKCf3M+YsjdJUMNBeZ3Xresl2wGvnwCeIbKt
    D541Re5K1Us2tgCBUhdpq+M=
    =nKrC
    -----END PGP SIGNATURE-----

    --U+BazGySraz5kW0T--
     
    Moritz Bunkus, Jun 17, 2004
    #5
    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. Hugo
    Replies:
    15
    Views:
    955
    Martin Thompson
    Jan 29, 2007
  2. M P
    Replies:
    1
    Views:
    554
  3. Ben Giddings

    Ruby mode indentation bug

    Ben Giddings, Oct 8, 2003, in forum: Ruby
    Replies:
    0
    Views:
    88
    Ben Giddings
    Oct 8, 2003
  4. Jeremy Henty
    Replies:
    5
    Views:
    137
    Jeremy Henty
    Dec 1, 2006
  5. HopfZ
    Replies:
    0
    Views:
    312
    HopfZ
    Jan 8, 2007
Loading...

Share This Page